Java Articles - Page 211 of 745

Explain Regular Expression "A" Metacharacter in Java

Maruthi Krishna
Updated on 18-Nov-2019 11:57:12

281 Views

The subexpression/metacharacter “\A” matches the beginning of the entire string.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "\AHi";       String input = "Hi how are you welcome to Tutorialspoint";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;       }       System.out.println("Number of matches: "+count);    } }OutputNumber of matches: 1Example2Following Java program accepts a string ... Read More

Regular Expression "." (dot) Metacharacter in Java

Maruthi Krishna
Updated on 18-Nov-2019 10:29:25

1K+ Views

The subexpression/metacharacter “.” matches any single character except a newline.Example1import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchesAll {    public static void main( String args[] ) {       String regex = ".";       String input = "Hi how are you welcome to Tutorialspoint";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;       }       System.out.println("Number of matches: "+count);    } }OutputNumber of matches: 40Example 2The following Java program accepts 5 ... Read More

Regular Expression "$" (dollar) Metacharacter in Java

Maruthi Krishna
Updated on 18-Nov-2019 10:17:40

1K+ Views

The subexpression/metacharacter “$” matches the end of a line.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class EndWith {    public static void main( String args[] ) {       String regex = "Tutorialspoint$";       String input = "Hi how are you welcome to Tutorialspoint";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;          System.out.println("Number of matches: "+count);       }    } }OutputNumber of matches: 1Example 2The following Java program accepts ... Read More

Regular Expression "^" (caret) Metacharacter in Java

Maruthi Krishna
Updated on 18-Nov-2019 10:12:52

670 Views

The subexpression/metacharacter “^” matches the beginning of a line. If you use this in a regular expression, it matches the sentence succeeding it in the input string.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "^Hi how are you";       String input = "Hi how are you welcome to Tutorialspoint";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;     ... Read More

How to add/insert additional property to JSON string using Gson in Java?

raja
Updated on 19-Feb-2020 10:28:54

9K+ Views

The com.google.gson.JSonElement class represents an element of Json. We can use the toJsonTree() method of Gson class to serialize an object's representation as a tree of JsonElements. We can add/ insert an additional property to JSON string by using the getAsJsonObject() method of JSonElement. This method returns to get the element as JsonObject.Syntaxpublic JsonObject getAsJsonObject()Exampleimport com.google.gson.*; public class AddPropertyGsonTest {    public static void main(String[] args) {       Gson gson = new GsonBuilder().setPrettyPrinting().create(); // pretty print JSON       Student student = new Student("Adithya");       String jsonStr = gson.toJson(student, Student.class);       System.out.println("JSON String: " + jsonStr);       ... Read More

Difference between fail-fast and fail safe in Java

Teja Kolloju
Updated on 17-Apr-2025 18:59:29

4K+ Views

In this article, we will find the differences between Fail-Fast and Fail-Safe Iterators. They both describe how collections behave when they are modified during iteration. What is an iterator? An Iterator is an object in Java used to cycle through a collection, accessing or removing elements. It can be obtained by using the iterator() method of a collection. What is FailSafe? Fail-safe also called a Non-Fail-fast Iterator, does not throw ConcurrentModificationException. It works on a copy of the collection. Any changes made in the iterator only affect the copy but not the original collection. Example The following is an ... Read More

Difference between volatile and transient in java

Teja Kolloju
Updated on 17-Apr-2025 18:59:16

10K+ Views

In this article, we will find the differences between volatile and Transient. Both have a different purpose for specifying before a variable. These modifiers are important in determining the behavior and characteristics of variables in different cases. What is volatile? A volatile keyword is used in a multithreading environment where two threads reading and writing the same variable simultaneously. The volatile keyword flushes the changes directly to the main memory instead of the CPU cache.  Example of Volatile The following is an example of Volatile in Java: public class VolatileExmaple extends Thread { volatile boolean isRunning ... Read More

Difference between List and Set in Java

Alshifa Hasnain
Updated on 27-Mar-2025 19:34:11

3K+ Views

In Java, List and Set are both interfaces that belong to the Collection framework. Both interfaces extend the Collection interface. They are both used to store a collection of objects as a single unit. Before JDK 1.2, we used to use Arrays, Vectors, and Hashtable for grouping objects as a single unit. Difference Table The following are the key differences between List and Set − Sr. No. ... Read More

Difference between String buffer and String builder in Java

Teja Kolloju
Updated on 15-Apr-2025 19:13:12

16K+ Views

String buffer and StringBuilder both are mutable classes which can be used to do operation on string objects such as reverse of string, concating string and etc. We can modify a string without creating a new object of the string. A string buffer is thread-safe whereas string builder is not thread-safe. Therefore, it is faster than a string buffer. Also, a string concat + operator internally uses StringBuffer or StringBuilder class. Below are the differences. What is StringBuilder? The StringBuilder class in java is used to create and change a sequence of characters. In a regural string once the characters ... Read More

Difference between inheritance and composition in Java

Kiran Kumar Panigrahi
Updated on 28-Jul-2022 11:37:32

4K+ Views

In computer programming, the concept of reusable code refers to the utilisation of previously developed software in the construction of new software. Reusability of code is recognised as an essential component of productive functionality. Establishing associations between classes is one method that object-oriented programming uses to encourage this.In object-oriented programming, there are two primary ways to construct these relationships: inheritance and composition.In object-oriented programming (OOP), inheritance refers to the process through which an object can take on the properties of one or more other objects. In OOP, it is one of the most powerful concepts for establishing code reusability. When ... Read More

Advertisements