Google News
logo
Java Strings
String(java.lang.String)
String is nothing but a group of characters or character array. Once we are creating String object it is not possible to do the modifications on existing object called immutability nature.

Syntax:
String a="freetimelearning"
Creating Strings:
There are three ways of creating strings in java. They are: 

1. String Literal
2. Using new keyword
3. Using character array
String Literal
class freetimelearning
 {
public static void main(String gs[])
 {
String a="java";
System.out.println(a);
 }
}
Output :
output:java
Using new keyword
class freetimelearning
{ 
public static void main(String gs[]) 
{ 
String a=new String("java"); 
System.out.println(a); 
} 
}
Output :
output:java
Using character array
class Test 
{ 
public static void main(String[] args) 
{
char[] ch={'j','a','v','a'}; 
String str1=new String(ch); 
System.out.println(str3); 
char[] ch1={'j','a','v','a'}; 
String str4=new String(ch1,1,3); 
System.out.println(str4);
}
}
Output :
output:ava
String is immutability nature:-
Once we are creating string object it is not possible to do the modifications on the existing object is called immutability nature.
String is immutability
class Test 
{ 
public static void main(String[] args) 
{ 
String str="hi"; 
str.concat("freetimelearning"); 
System.out.println(str); 
} 
}
Output :
output:  hi