GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Equals operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Equals operator" Watch "Equals operator" New topic
Author

Equals operator

Tushar Mishra
Greenhorn

Joined: Jan 27, 2006
Posts: 7
Hi,

I want to know the reason for this pattern. Request your help in understanding this concept of equals operator.
I appreciate you for taking out your time.



Thanks,
Tushar


Thanks,
Tushar
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

Since java 5.0, Wrapper classes (of primitives) cache the values between -128 & 127 so static valueOf method (which will be called when you invoke the method "method", that is passing primitives to a method which expects Wrapper type the auto-boxing needed to perform) will not return new Integer object but a cached one.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Vijitha Kumara wrote:Since java 5.0, Wrapper classes (of primitives) cache the values between -128 & 127


For only Integer . for other wrapper class ,valueOf will create new Object .
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

seetharaman venkatasamy wrote:For only Integer . for other wrapper class ,valueOf will create new Object .


Well, Not only for Integer but Short,Byte,Character also cache it.
Tushar Mishra
Greenhorn

Joined: Jan 27, 2006
Posts: 7

Hi Vijitha / Seetharaman,

My sincere thanks to you for your prompt reply.

Regards,
Tushar
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Vijitha Kumara wrote:
Well, Not only for Integer but Short,Byte,Character also cache it.


yes . thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Vijitha Kumara wrote:
seetharaman venkatasamy wrote:For only Integer . for other wrapper class ,valueOf will create new Object .


Well, Not only for Integer but Short,Byte,Character also cache it.

And Long as well. Only Float and Double do not have a cache.

Also, Character's cache only caches values from 0 to 127 (also known as ASCII characters), so this cache is twice as small as the other caches.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tushar Mishra
Greenhorn

Joined: Jan 27, 2006
Posts: 7
Thanks Rob !

Regards,
Tushar
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

If I am not wrong, this technique is related to java Tiger.

here, wrapper classes are automatically Typecasted to thier primitive equivalents at run time.


Basic ideology is

for primitive elements use:



and for Objects use:



Wrappers can be treated as primitive if you are use your client uses the latest version of java (1.5 to be more precise)

rather than:

int n=new Integer(200).intValue();

this is permissible:

int n=new Integer(200);



My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equals operator