What will be the output of the following program code?
class LogicalCompare{
public static void main(String args[]){
String str1 = new String("OKAY");
String str2 = new String(str1);
System.out.println(str1 == str2);
}
}
class LogicalCompare{
public static void main(String args[]){
String str1 = new String("OKAY");
String str2 = new String(str1);
System.out.println(str1 == str2);
}
}
A. true
B. false
C. 0
D. 1
E. Displays error message
Answer: Option B
Join The Discussion
Comments ( 8 )
Related Questions on Strings
In Java, which class is used to represent a sequence of characters as a string?
A. String
B. StringBuilder
C. StringSequence
D. StringArray
What is the correct way to create a new empty String object in Java?
A. String emptyString = "";
B. String emptyString = new String();
C. String emptyString = " ";
D. String emptyString = null;
E. Both A and B
Which of the following methods is used to compare two strings for equality in Java?
A. equals()
B. compareTo()
C. equalsIgnoreCase()
D. compare()
public static void main(String[] args) {: This is the main method, which serves as the entry point of the program.
String s1 = new String("ok");: This line creates a new String instance with the content "ok" and assigns it to the variable s1.
String s2 = new String(s1);: This line creates a new String instance with the content of the s1 variable ("ok") and assigns it to the variable s2.
System.out.println(s1 == s2);: This line compares the references of s1 and s2 using the == operator. It checks whether both variables refer to the same memory location (i.e., if they are the same object).
Since you're using the new keyword to explicitly create new String instances, even if the content is the same, the instances will be stored in separate memory locations. Therefore, the comparison s1 == s2 will result in false.
Actually this gives compilation error as there is no quotation mark in str1
By using new keyword new string will be created in heap area . if we compare those strings using double equals operator it will compare references here references is not same so it will return false
== does comparison of objects, not its values. It will always return false unless both objects have same name. Thus inorder to compare equality of two string objects use equals() method.
== operator compares the address not the content present in str1 and str2.for the str1 we used new so it creates some space in non constant pool like wise in str2 also we used new it creates some space in non constant pool itself.But for both str1 and str2 have different address .
Because in non constant pool of string pool duplication is allowed so both the strings will be stored in different address and when we compare the address it will give us false.
In java, == operator compares references not values.
because str2 refers to instance created in nonpool and == operator compares references not value.
Why output of that question is false while hash code and content of both strings are same?