This question already has answers here:
Getting strange output when printing result of a string comparison
(3 answers)
Closed 2 years ago.
Can someone please explain to me why this code print only false (Not Both are equal: false)
String one = "length: 10";
String two = "length: " + one.length();
System.out.println("Both are equal:" + one == two);
System.out.println("Both are equal:" + one == two);
is evaluated as
System.out.println(("Both are equal:" + one) == two);
i.e. first one is appended to "Both are equal:" , which results in the String "Both are equal:length: 10", and then that String is compared to two, which results in false, so only false is printed.
What you want is
System.out.println("Both are equal:" + (one == two));
Just put the brackets here:
System.out.println("Both are equal:" + (one == two));
Related
This question already has answers here:
Getting strange output when printing result of a string comparison
(3 answers)
String equals and == with String concatenation [duplicate]
(4 answers)
Java string == Vs Equals giving true or false when combined with concatenation [duplicate]
(5 answers)
String concatenation and comparison gives unexpected result in println statement
(5 answers)
The expressions which evaluate to a boolean value can not be concatenated with a String in Java. Why? [duplicate]
(5 answers)
Closed 2 years ago.
String r1 = "hello";
String r2 = "hello";
System.out.println("Result: " + r1 == r2);
I tried running this code and i am not getting the String output in the console. The only value getting printed is the r1==r2 result i.e false.
The question here is I am expecting the output to be "Result: false" and why is false just getting printed.
Also i understand that .equals should be used, I just wanted to know why the result is such in the given scenario.
It would be great if some one can point to relevant documentation and help why this is the behaviour.
Indeed the output is "false" because first the concatenation is done so it is "Result: hello" and then it is compared to r2 ("hello") which returns false.
That's why you see "false" in console.
If you want to see "Result: true" you need to use equals instead of == because that's how we compare Strings in java. See below post to understand differences:
How do I compare strings in Java?
If you really want to compare them using "==" you need to add brackets so the comparison will be first before concatenation.
String r1 = "hello";
String r2 = "hello";
System.out.println("Result: " + r1 == r2); // output: "false"
System.out.println("Result: " + r1.equals(r2)); // output: "Result: true"
System.out.println("Result: " + (r1 == r2)); // output: "Result: true"
Because it was processed as
("Result: " + r1 ) == r2
which is false. It was processed in this way because + has a higher priority than ==.
What you need to do to get the string as well is
System.out.println("Result: " + (r1 == r2));
which will give you Result: true
To compare strings and print the way you want you should use method equals(). Like this:
String r1 = "hello";
String r2 = "hello";
System.out.println("Result: " + r1.equals(r2));
Data is not displaying inside the system.out.println statement
with parenthesis it is working and equals method
final String s1="all men are created equal:27";
final String s2="all men are created equal:"+s1.length();
System.out.println("all men are created equal:" + (s1==s2));
output: all men are created equal:false
final String s1="all men are created equal:27";
final String s2="all men are created equal:"+s1.length();
System.out.println("all men are created equal:" + (s1.equalsIgnoreCase(s2)));
System.out.println();
output:all men are created equal:false
final String s1="all men are created equal:27";
final String s2="all men are created equal:"+s1.length();
System.out.println("all men are created equal:" + s1==s2);
System.out.println();
output:false
Why is the "all men are created equal:" is not printing
The operators you are using have different precedences. The == operator has a lower precedence than the + operator (just like how + has a lower precedence than * in maths), so your expression
"all men are created equal:" + s1==s2
gets parsed this way:
("all men are created equal:" + s1) == s2
It is comparing whether "all men are created equal:" + s1 is equal to s2. The result of this expression is a boolean. Therefore, only false is printed.
By adding the parenthesis, you are making it clear that you want the s1==s2 part to be evaluated first, then the + operator. This is just like how 1+2*3 is different from (1+2)*3.
Also note that you should compare strings with equals, not ==:
"all men are created equal:" + s1.equals(s2)
The . operator has a very high precedence, higher than that of +, so you don't need the brackets in this case.
The last print is showing only false because of the operator precedence in java. At first it concats the "all men are created equal:" with string s1. Then it compares if the concated string is equal to s2 and prints the output. As you can see the concated string is not equal to s2.
Just because "all men are created equal:" + s1 and s2 isn't equal.
With the above expression you are checking whether "all men are created equal:" + s1 and s2 is equal or not. Since "all men are created equal:" + s1 and s2 isn't equal, the result will be false.
if you want to get the expected behavior, you should use parenthesis around s1==s2.
"all men are created equal:" + (s1==s2)
Add "()" to s1==s2.
Like this:
System.out.println("all men are created equal:" + (s1==s2));
The reason is following:
"all men are created equal:" + s1 => "all men are created equal:false"
"all men are created equal:false" == s2 => false
PRINT false
This question already has answers here:
I keep getting a "The operator == is undefined for the argument type(s) boolean, int" and have no idea how to fix it
(2 answers)
Closed 3 years ago.
I'm new to coding so sorry if this is a stupid question.
Why isn't the following code working?
I currently have int x = 100 and boolean b = false
if ( x == b)
{
System.out.println(x + " is equal to " + b);
}
else
{
System.out.println(x + "is not equal to " + b);
}
I expect the output to be "100 is not equal to false"
Sorry, late in the day. Got my logic backwards twice.
A 1 or any positive integer will be true. Think binary - 0 or 1.
The comparison is Boolean so 1 or 100 = true.
This question already has answers here:
String equals and == with String concatenation [duplicate]
(4 answers)
Closed 7 years ago.
String a = "abc";
String b = "abc";
System.out.println("Result .... " + a==b); // false
System.out.println(a==b); // true
1st print statement prints false and 2nd prints true, though ideally it has to be true. Why is it false in 1st print statement ?
System.out.println("Result .... " +a==b); -> the result string will be appended with 'a' and then compares with b so it results false.
Order of operations:
"Result .... " + a==b
is equivalent to
("Result .... " +a) == b
which will be false since the two strings are not the same reference.
The explanation for this is that the + addition operator has a higher precedence than == logical equivalence.
The expression a == b is returning true in your second statement due to interning, in which a and b actually refer to same string object.
Click here for a link to Oracle's table of operator precedence in Java.
Forgot checking equality by == in java. In Java this operation checks equality of object link. Additionally it is applicable for checking equality of simple numbers. You should use .equals method
String a = "abc";
String b = new String(a);
System.out.printLn(a == b);//false
System.out.println(a.equals(b));//true
Learn about operation order in java
It is because in "Result .... " +a==b it first add "Result .... " with a and then == to b. If you write like this "Result .... " +(a==b), then it will be OK.
In the first statement, you're comparing "Result .... " + a with b. In the second one, you're comparing a with b, hence the difference. Change your first statement as follows:
System.out.println("Result .... " + (a==b));
And keep in mind that strings should be compared using the equals() method instead of ==.
System.out.println("Result .... " +a==b);
String Result is appended with 'a' and then compares with b so it provides false. (Result .... a == b) which is false.
Follow this link to understand precedence of operators in java and this.
Operator + has more precedence than == operator.
try adding brackets, you will see the diff. Bracket is evaluated separately.
public static void main(String[] args) {
String a = "abc";
String b = "abc";
System.out.println("Result .... " + (a == b)); // false
System.out.println(a == b); // true}
}
output
Result .... true
true
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have this code which I cannot understand. In the beginning you can see two identical Strings, and when I compare them with use of operator == it says it is true, same as equals() method, but when I create two identical strings during runtime operator == says false. Why is this happening ?
Does it mean that when I hardcode identical strings they are placed in the same position in the memory and both references point to it? I found similar question, but there were no explicit answer.
public class StringTesting {
public static void main(String[] args){
String string1 = "hello"; //\
// } same place in the memory ?
String string2 = "hello"; ///
System.out.println(string1 == string2); //true
System.out.println(string1.equals(string2)); //true
String string3 = "hey";
String string4 = "he";
System.out.println(string3 == string4); //false
System.out.println(string3.equals(string4)); //false
string4 += "y";
System.out.println(string3 == string4); //false ????
System.out.println(string3.equals(string4)); //true
System.out.println(string3 + " " + string4); //hey hey
}
}
The following compound assignment operator:
string4 += "y";
performs String concatenation at runtime. Since the value of string4 is evaluated at runtime only. And String concatenation done at runtime creates a new object.
From JLS Section 3.10.5 (See towards the end of this section):
Strings computed by concatenation at run time are newly created and therefore distinct.
However if you perform concatenation of two string literals, it won't create a different objects. So the following code will return true:
"he" + "y" == "hey";
That JLS section contains code segment for various string concatenation example:
String hello = "Hello",
String lo = "lo";
System.out.print((hello == "Hello") + " "); // true
System.out.print((Other.hello == hello) + " "); // true
System.out.print((other.Other.hello == hello) + " ");// true
System.out.print((hello == ("Hel" + "lo")) + " "); // true
System.out.print((hello == ("Hel" + lo)) + " "); // false
System.out.println(hello == ("Hel" + lo).intern()); // true
string4 += "y"; creates a new object.
String literals however are placed in the same place in memory as an optimization (this is called string interning).
string1, string2, and string3 are all string constants. i.e., they appear as constant pool entries in the .class file. In Java, string constants are interned.
string4 is a new string, created by taking the string constant "he" and then appending "y". Therefore, it's not a string constant, and is not interned.
That is why string3 != string4.