This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrappers - Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrappers - " Watch "Wrappers - " New topic
Author

Wrappers -

Masood Alam
Greenhorn

Joined: Jul 12, 2007
Posts: 8
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

}

}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
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.


Ping & DNS - my free Android networking tools app
Jared Folino
Greenhorn

Joined: Apr 27, 2007
Posts: 25
Just remember that it is only for wrappers between -128 and 127
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
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...


kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrappers -