+ 3
Can any body explain output to the following piece of code in decription
if("String".toString() == "String") System.out.priqntln("Equal"); else System.out.println("Not Equal");
2 Respostas
+ 7
When talking about objects (which a String is) == compares the reference. If "String" has the same reference as "String" then output: Equal. else Not Equal. [toString does not change the reference].
Consider using .equals to compare strings, as it checks character by character to see if they're indeed the same.
Lets say I had:
String first = "String";
String second = new String("String");
if(first == second)
System.out.println("Equal");
else
System.out.println("Not Equal");
The output is: Not Equal. While if I had:
if(first.equals(second))
The output would be: Equal.
+ 1
you are giving a conditional statement:
if the string value "String" is .toString-ed and is equal to "String", then output should be Equal. else it will output Not Equal.