Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
why its giving true i mean i will have some other memory location and i2 have different it must give false ??

i have written this code why its give true and i what to know reason.can somebody know the answer then tellllllllllllllllllllllllllllllllllllllll me
Java
public class ADS {
  public static void main(String[] args) {
    int i=new Integer(10);
    int i2=10;
    System.out.println(i==i2);
  }
}


What I have tried:

hey anyone give reason why its giving true i mean i will have some other memory location and i2 have different it must give false ??
Posted
Updated 27-Feb-18 8:29am
v2
Comments
Richard Deeming 27-Feb-18 14:17pm    
In what system of mathematics is the number 10 not equal to the number 10?!
Jon McKee 27-Feb-18 14:21pm    
You beat me to it. Have a hypothetical +1

These are integer values, not pointers to integer values.
Thus, the comparison operator does a value comparison; since both values are equal, it returns true.
 
Share this answer
 
If you do:
Java
Integer i = new Integer(10);
Integer i2 = new Integer(10);
System.out.println(i==i2);
then you'll probably get false.

If you do:
Java
Integer i = Integer.valueOf(10);
Integer i2 = 10;
System.out.println(i==i2);
then you'll get true, because values between -128 and 127 are automatically cached:
Java gotchas - OWASP[^]
Chapter 5. Conversions and Promotions[^]

If you declare the variables as int rather than Integer, then you'll get true because you're performing a value equality test, not a reference equality test.
 
Share this answer
 
You have to understand that those 2 lines are 2 ways to say the same thing.
C++
int i=new Integer(10);
int i2=10;

The 2 lines are integers of value 10, so they are equal.
Use the debugger and see by yourself.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900