This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
How come the first condition is false and the second is true? I was sure they were both true.
System.out.println(Integer.toString(3) == "3");
System.out.println(Integer.parseInt("3") == 3);
Integer.parseInt converts a String to a primitive int and primitives can be compared with ==. However, Integer.toString produces a String object and == for objects checks that they are the exact same reference; use String#equals instead to compare the values of the Strings.
System.out.println(Integer.toString(3).equals("3"));
System.out.println(Integer.parseInt("3") == 3);
The above code outputs:
true
true
Related
This question already has answers here:
What is the difference between == and equals() in Java?
(26 answers)
Closed 2 years ago.
What is the difference between these two cases? Would the equality check be wrong? If not, which one is faster?
If you use wrapper class (Byte instead of byte), you should use equals method. The code below doesn't work (not likely to happen) as == compare reference not the value.
public static void main(String[] args) {
Byte b1 = new Byte((byte) 0);
Byte b2 = new Byte((byte) 0);
System.out.println(b1 == b2);
}
Some explanation here.
This question already has answers here:
How can I properly compare two Integers in Java?
(10 answers)
Closed 3 years ago.
I was coding an algorithm issue, below code can't pass case
public void pop() {
if (s1.pollFirst() == minStack.peekFirst())
minStack.pollFirst();
}
however below can,
public void pop() {
int tmp = s1.pollFirst() ;
if (tmp == minStack.peekFirst())
minStack.pollFirst();
}
the only difference is how I use s1,pollFirst() return result. I can't figure out real difference here.
Thanks
Comparing two Integer Objects with values less than -128 or bigger than 127 using == will always result in false. However, if you compare Integer with primitive int, it will give you true if the actual value is the same.
int n1=128;
Integer n2=127;
Integer n3=127;
Integer n4=128;
Integer n5=128;
System.out.println(n1==n2); //false
System.out.println(n2==n3); //true
System.out.println(n4==n5); //false
System.out.println(n1==n5); //true
In the second example you are assigning the value to the primitive int, therefore it is automatically unboxed.
This question already has answers here:
Getting strange output when printing result of a string comparison
(3 answers)
Behavior of String literals is confusing
(11 answers)
Closed 4 years ago.
String s5="Ram";
String s6="Ram";
System.out.println(" s5==s6 is " + s5==s6); // false
System.out.println(s5==s6); // true
Why the first line is false and the second line is true always?
Because + has a higher precedence than ==,
" s5==s6 is "+s5==s6
evaluates as
" s5==s6 is Ram"=="Ram"
which is obviously false. You could have avoided this particular problem by using parentheses:
" s5==s6 is "+(s5==s6)
Meanwhile,
s5==s6
evaluates as
"Ram"=="Ram"
which is true only because of automatic interning of string literals in Java. If one of those strings was evaluated rather than a literal, it would be false, as == compares object identity, and two different String objects would be evaluated as different even if they had the same content. Compare
String s5 = new String("Ram");
String s6 = new String("Ram");
System.out.println(s5==s6); //false
Use .equals for meaningful comparison of string contents:
System.out.println(s5.equals(s6)); //true
This question already has answers here:
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
Comparing boxed Long values 127 and 128
(4 answers)
Closed 5 years ago.
I'm trying to compare 4 values in a program.
Long val1 = 127l;
Long val2 = 127l;
Long val3 = 128l;
Long val4 = 128l;
Log.e("XXXX",(val1==val2)+" "+(val3==val4));
This gives me val1l == val2l is true and val3' == val4l is false. What is the reason behind this output. I think it's coming because of the wrapper class. Please help me to clear this.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Why is this ok? What exactly does it compare?
int i = 10;
char c = 10;
if( c == i)
System.out.println("We are Equal");
And the same in this situation:
String s1 = "Null";
String s2 = new String(s1);
if( s1 == s2)
System.out.println("We are Equal");
I get that we're not comparing the contents of the variable.
In the first example, the two literal values of the integers are being compared, I.e. 10 == 10.
In the second example, your are comparing String objects, meaning the value of the actual Objects, not its content, are getting compared. In order to compare the content of these string objects, I.e. "Blah" == "Blah", you should use the string method String.compare(String strToCompare
Strings are objects. You are comparing references for the strings. Char/ints are primitives so no objects.