EX. String s = “Tutorialink”;
String s1= “Welcome”; // no new object will be created.
2.Using new keyword : JVM will create a new string object in normal heap memory and the literal “welcome” will be placed in the string constant pool.
EX. String s= new String(“Tutorialink”); // creates new objects
Concatenation: in java string objects are immutable simply means un-modifiable or unchangeable.
Ex. Public static void main(String args[ ])
{
String s=“Indian”;
s=s.concat(“Cricketer”);// concat() method append String at the end
System.out.println(s);
}
Output:
Indian Cricketer
concat() method concates the specified string to the end of current string.
Class ConcatenateDemo3
{
public static void main(String args[])
{
String s1=“indian”;
String s2=“Cricketer”;
String s3=s1.concat(s2);
System.out.println(s3); //Indian Cricketer
}
}
Output:
Indian Cricketer
Ask Question