freetimestudys@gmail.com          +91-7097633380
  Login Here                        

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="freetimestudy"

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 freetimestudy
 {
public static void main(String gs[])
 {
String a="java";
System.out.println(a);
 }
}

intput and output

output:java

Using new keyword

class freetimestudy
{
public static void main(String gs[])
{
String a=new String("java");
System.out.println(a);
}
}

intput and 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);
  }
}

intput and 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("freetimestudy");
System.out.println(str);
 }
}

intput and output

output:hi