Java String
The collection of characters is known as String. In java String is a class and it is a reference data type.
The strings are immutable i.e. It can’t be changed but a new instance is created every time if we perform concat( ) operation. Once we are allocated memory we can’t change. unchangeable nature is called immutability.
Immutable:
Example of string:
String s= “Java”;
String s2 = new String(“Java World”);
Immutable:
Non-changeable nature is called immutability.
Ex: String s1 = "java";
String s2= "world";
s1.concat(s2);
In this case, if we are trying to add new string s2 to this previous string s1. It is allocating new memory to s1 this nature is called as immutability.
Mutable: The changeable nature is called mutability.We can perform any operation can’t create a new instance every time.
Example: StringBuffer, StringBuilder.
String trim()
Return copy of the string , with leading and trailing whitespace omitted.
The difference between '==' and .equals( ) ?
The difference between '==' and .equals( ) ?
In java == is ment for reference(address) comparison and return true (or) false.
.equals() is used for content comparison and return true (or) false.
Program:
class Sample{
public static void main(String args[]){
String s="java"; //allocated memory to string
String s2="java2";
String s3= new String("java world");
s=s.concat(s2); //concatenation (new instance will be created)
System.out.println(s); //displaying string
System.out.println(s3);
}
}
Interview Questions:
Explain about String?
What is mean by immutable?
What is mutability and example of mutable class?
What is String concatenation?
What is the difference between String and StringBuffer?
What is the difference between String and StringBuffer and StringBuilder?
What is the difference between == and .equals() method in Java?
1 comments:
nice post for sharing Java article. It’s really helpful for me. keep sharing tutorials????????????????
EmoticonEmoticon