Java Strings Basics

 Strings in java

  • Java strings are immutable (it cannot be changes within it self)
  • The group of characters is known as string
  • It string represent with the text inside the double quotes
  • The index number start from 0 (zero) upto length-1
  • All the strings stores in string constant pool which returns the same object for the strings until the we  assign a new instance for the string

1)Assigning a String to the variable:

In java we can assign a string in different ways

Ex:

  • String name=""; name="word";
  • String name="word";
  • char ch={'w','o','r','d'}; String name=new String(ch);
  • String name=new String("name");

Ex;- 

class StringAssignment{

public static void main ( String []args){

String word1 ="Daniel";

String word2="Daniel";

//Here both references with return same literals

// == used to check the literals and .equals is used to chech the values

String word3=new String ("Daniel");

System.out.print(word1==word2);//true

System.out.print(word3==word1);//false

}

}

Comments

Popular posts from this blog

Constructors in java