• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Wrappers -

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings,

In the below code //Line 1 results in "true" and the //Line results in "false"

Could someone please explain... its seems to be similar to "String Pool"

/**************************CODE*************************************/

class TestIntegers{

public static void main(String[] args){

Byte b1= 5;
Byte b2 = 5;
Byte b3 = new Byte("10");
Byte b4 = new Byte("10");
System.out.println("First Style " + (b1 == b2)); //Line 1
System.out.println("Second Style " + (b3 == b4)); // Line 2

}

}
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b1 and b2 are created using auto-boxing, which doesn't employ "new Byte(...)", but rather Byte.valueOf(...). The difference is that valueOf doesn't create a new object - it reuses an existing one. Hence the two objects are identical.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just remember that it is only for wrappers between -128 and 127
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Masood,

I think you are using jdk 1.5 for you compiling code.. try this code in 1.4 it wan`t allow you to compile this code...

what is going on here due to new feature added in 1.5 is auto bouncing.. so go thorough it..

And remember you are preparing for scjp 1.4 or scjp 1.5.. your ans vary based on this..

in 1.4 it give you compilation error...
in 1.5 it give wan`t give you any error and will work fine...
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic