DefinitionStrings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.
The Java platform provides the String class to create and manipulate strings.
String str3=new String("Hello") //Using constructor.
If(str1 == str2)
System.out.println("Not Equal 1");
If(str1 == str3)
System.out.println("I am constructed using constructor, hence not interned");
If( str1.equals(str3) )
System.out.println("Not Equal 3");
Commonly asked interview questions on Constructors
1.What is String in Java? String is a data type?
Answer :String is a Class in java and defined in java.lang package. It’s not a primitive data type like int and long. String class represents character Strings. String is used in almost all the Java applications and there are some interesting facts we should know about String. String in immutable and final in Java and JVM uses String Pool to store all the String objects.
Some other interesting things about String is the way we can instantiate a String object using double quotes and overloading of “+” operator for concatenation.
2.What are different ways to create String Object?
Answer :We can create String object using new operator like any normal java class or we can use double quotes to create a String object. There are several constructors available in String class to get String from char array, byte array, StringBuffer and StringBuilder.
String str = new String(“abc”);
String str1 = “abc”;
3. In how many ways you can create string objects in java?
Answer : There are two ways to create string objects in java. One is using new operator and another one is using string literals. The objects created using new operator are stored in the heap memory and objects created using string literals are stored in string constant pool.
String s1 = new String(“abc”); //Creating string object using new operator
String s2 = “abc”; //Creating string object using string literal
4What is string constant pool?.
Answer :String objects are most used data objects in Java. Hence, java has a special arrangement to store the string objects. String Constant Pool is one such arrangement. String Constant Pool is the memory space in heap memory specially allocated to store the string objects created using string literals. In String Constant Pool, there will be no two string objects having the same content.
Whenever you create a string object using string literal, JVM first checks the content of the object to be created. If there exist an object in the string constant pool with the same content, then it returns the reference of that object. It doesn’t create a new object. If the content is different from the existing objects then only it creates new object.
5What is special about string objects as compared to objects of other derived types?.
Answer :One special thing about string objects is that you can create string objects without using new operator i.e using string literals. This is not possible with other derived types (except wrapper classes). One more special thing about strings is that you can concatenate two string objects using ‘+’. This is the relaxation java gives to string objects as they will be used most of the time while coding. And also java provides string constant pool to store the string objects.
1. Head First Java