• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can someone help me with this

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am new to the group of ranchers...just wanted to clarify a doubt:

This code is there in the K & B book...chapter 3. The output for the program is :

same object
meaningfully equal
different objects
meaningfully equal

I am really confused as to how both Line A and B can be executed.

class Test
{
public static void main(String agrs[])
{
Integer i3 = 100;
Integer i4 = 100;
if(i3 == i4) System.out.println("same object");//line A
if(i3.equals(i4)) System.out.println("meaningfully equal");

Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");//line B
if(i1.equals(i2)) System.out.println("meaningfully equal");
}
}

Thanks in Advance.
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

read this article for detailed explanation.
[ January 09, 2008: Message edited by: Krishna Srinivasan ]
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

prints false
prints true;
prints false
 
reply
    Bookmark Topic Watch Topic
  • New Topic