what is the difference between following equality checks? [duplicate] - java

This question already has answers here:
Java: Integer equals vs. ==
(7 answers)
Why do we use autoboxing and unboxing in Java?
(10 answers)
How can I properly compare two Integers in Java?
(10 answers)
Integer == int allowed in java
(6 answers)
Closed 4 years ago.
System.out.println(5 == new Integer(5)) output = true
Integer i31 = 2;
Integer i32 = new Integer(2);
System.out.println(i31 == i32); output = false
It rather seems since we are in function scope. hence different output.
Unable to grasp what can be different.

Related

concatenation and displaying a string [duplicate]

This question already has answers here:
Does concatenating strings in Java always lead to new strings being created in memory?
(3 answers)
Converting String to "Character" array in Java
(14 answers)
String to char array Java
(1 answer)
Closed 3 months ago.
a string created with String class can not be modified.
but when we use the += operator does it mean that the original string change?
exp:
String ch="hello"; ch+= "world";
another question:
why we don't use these instructions to display the string ?
for (int i=0;i<ch.length();i++) {System.out.println(ch[i]); }
i tried this
for (int i=0;i<ch.length();i++) {System.out.println(ch.charAt(i)); }
why it is not similar to
for (int i=0;i<ch.length();i++) {System.out.println(ch[i]); }

Integer.toString() vs. Integer.parseInt() [duplicate]

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

Java Integer Comparison [duplicate]

This question already has answers here:
How can I properly compare two Integers in Java?
(10 answers)
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
Closed 3 years ago.
Integer a = 100, b = 100;
Integer c = 1000, d = 1000;
System.out.println(a == b);
System.out.println(c == d);
The output of the above code is:
true
false
Can someone explain why so?

Difference between two given generic statements [duplicate]

This question already has answers here:
What is the point of the diamond operator (<>) in Java?
(7 answers)
Why diamond operator is used for Type Inference in Java 7?
(6 answers)
Closed 5 years ago.
Gen<Integer> y=new Gen(2); // Line1
Integer x=y.getOb(); //Line 2
Gen<Integer> y1=new Gen<>(2); // Line3
Integer x1=y1.getOb();//Line4
class Gen<T>
{
T val;
Gen(T ob)
{
val=ob;
}
T getOb()
{
return val;
}
}
I am not able to find any difference between y and y1 objects. Please help me in understanding this.
FYI- It is getting compiled and giving right output.

Value equation of wrapper classes in Java [duplicate]

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.

Categories