• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Equals operator

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



yes . thanks
 
Marshal
Posts: 22450
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob !

Regards,
Tushar
 
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);


 
reply
    Bookmark Topic Watch Topic
  • New Topic