1

I have doubt in Following code object references in java. here is the code.

    public class Simple{
public static void main(String args[]){
 Integer a=150,b=150;
        System.out.println(a==b);

        Integer c=100,d=100;
        System.out.println(c==d);
}}

I can compile and execute the above code in online compiler and I got answers false true.And my doubt is Why i can't get second one is false? it's also object reference isn't it?. Please someone can help me to clear the doubt.Thanks in advance

hariGS
  • 60
  • 1
  • 2
  • 14
  • However, I have tried this in the online java compiler [(tutorialspoint)](https://www.tutorialspoint.com/compile_java_online.php) and I've got the same output. This could not be happened on the Java run-time. This is an issue with the site itself. – Senura Dissanayake Apr 10 '18 at 06:06
  • @user7328702 tutorialspoint may not be the most trustworthy resource to use. – Stultuske Apr 10 '18 at 06:07
  • If we assign values to integer objects while converting them java use `valueOf()`. The representation of value of method cache the values for better performance. See the documentation of valueOf() method. – Pavan Apr 10 '18 at 07:03
  • I have tried this without using an online compiler. I am getting the same result as you. The reason is Integer is an object. It unwraps the value as an int when you assign smaller numbers. see >> [Java Integer Comparison](https://senuradissanayake.wordpress.com/2018/04/10/java-integer-comparisons/) – Senura Dissanayake Apr 11 '18 at 07:52

0 Answers0