• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equals operator

 
Tushar Mishra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Vijitha / Seetharaman,

My sincere thanks to you for your prompt reply.

Regards,
Tushar
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha Kumara wrote:
Well, Not only for Integer but Short,Byte,Character also cache it.


yes . thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tushar Mishra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob !

Regards,
Tushar
 
salvin francis
Bartender
Posts: 1263
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic