Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Integer Class

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one give explanation for the below code?

class Program {
public static void main(String[] args) {
Integer i1 = 20;
Integer i2 = 20;
Integer i3 = 201;
Integer i4 = 201;
System.out.println(i1 == i2);
System.out.println(i3 == i4);
}
}

it is giving an output

true
false

why so?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try executing it or somewhere posted in the website?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi souji that output is right because up to some value == results true after that limit it results false. i hope that range is int range. sorry i am not sure but that output is right no doubt about it. I am very sure because i tried in my system.
Regards,
JBande.
[ March 23, 2007: Message edited by: j bande ]
 
souji nutak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi VSSMK,

i executed this program, it is giving the output that i wrote there. i didn't understood why it is giving true in one case and false in another

could you please explain me that in detail
 
Srinivas Kumar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really doubt the initialization of the code
Integer i1 = 20;
Integer i2 = 20;
Integer i3 = 201;
Integer i4 = 201;

You cannot initialize an Interger wrapper object like that.There are 2 overloaded constructors.one that takes primitive int and the other that takes string.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

class Program {
public static void main(String[] args) {
Integer i1 = 20;
Integer i2 = 20;
Integer i3 = 201;
Integer i4 = 201;
System.out.println(i1 == i2);
System.out.println(i3 == i4);
}
}

it is giving an output

true
false

why so?




You executed this code in JDK 5.0. Please refer "Boxing" concept which is a new feature in 5.0

While boxing same reference is used if the range is from -128 to 127. Thats why i1 and i2 refers same Object. i3 and i4 are more than 127. So refers different objects.
 
j bande
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code fragment:
Integer i1 = 100;
Integer i2 = 100;
Integer i3 = 1000;
Integer i4 = 1000;
System.out.println(i1==i2);
System.out.println(i3==i4);
Can you guess what will be printed on the screen? If your answer is false--well, you're wrong.
In this case, J2SE 5.0 works differently. Certain ranges of values are stored as immutable objects by
the Java Virtual Machine. So, in this case, the output is:
true
false
Normally, when the primitive types are boxed into the wrapper types, the JVM allocates memory and
creates a new object. But for some special cases, the JVM reuses the same object.
The following is the list of primitives stored as immutable objects:
� boolean values true and false
� All byte values
� short values between -128 and 127
� int values between -128 and 127
� char in the range \u0000 to \u007F


May be this you will be more clear.
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic