group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
254 Members
Join this group to post and comment.
Shruthi Pillai
Shruthi Pillai
Information Technology
25 Jan 2019

Java String Class

There are two ways to declare a string in Java 

String name = "SHRUTHI"; 

char[] nameArray = {'A', 'l', 'e', 'x'}; 

concat() method can be used to attach strings .

trim() method is used to eliminates white spaces before and after a string. 

length() method is used to get the length of the string. 

Archana Priyadarshini
Archana Priyadarshini
Computer Science
1mo ago

There are two ways to create a String in Java

  1. String literal
  2. Using new keyword

In java, string is an immutable object which means it is constant and can cannot be changed once it has been created. 

Eg: String str="Hello World";

we have not created any string object using new keyword above. The compiler does that task for us it creates a string object having the string literal and assigns it to the provided string instances.But if the object already exist in the memory it does not create a new Object rather it assigns the same old object to the new instance.

String str=new String("Hello World");

In this case compiler would create two different object in memory having the same string.