What is String

String in java treated as a object, It is a group of characters.

String creation

We can create the string object using single quotes () or double quotes () or using new keyword.

Example

Using single and double quotes:

String singleQuotes = 'working single quotes';
System.out.println(singleQuotes);


String doubleQuotes = 'working double quotes';
System.out.println(doubleQuotes);

Using new keyword:

public class StringCreate {

   public static void main(String args[]) {
      char[] WorkArray = { 'W', 'o', 'r', 'k', 'i', 'n', 'g' };
      String WorkArray = new String(WorkArray);  
      System.out.println( WorkArray );
   }
}

Output:

working

If we are creating String object of both variables using quotes and new keyword with same name than new keyword variable works by default.

Some important points:

  1. String objects are immutable means once the value is assigned to the String variable we cannot change.
  2. String objects are thread safe.
  3. String objects store its value to String constant pool.
  4. String objects are fast.

 

NEXT PAGE

Facebooktwittergoogle_plusredditpinterestlinkedinmail
1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 5.00 out of 5)
Loading...