This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Assignment of wrappers to both objects and primitive values; plus a question on cached ranges

 
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I've been studying wrapper classes and I have two questions about them. I'll combine them in one post. Consider the following code:

1) It seems that there are five ways of creating an object of primitive value using the wrapper classes, namely constructor(primitive/String), valueOf(primitive/String), valueOf(primitive/String).<primitive>Value(), autoboxing and parse<Primitive>(String). Replace <primitive> with the relevant data type. I am aware that the rules for the Character class are different. My question: it seems to me that the return values of all of these five ways can be assigned to a wrapper object as well as to a primitive. I have added some examples in the code to illustrate this. Is this assumption correct? Is this because unboxing is implicitly happening when you assign one of these returned values to a primitive? Or am I overlooking anything here?

2) System.out.println(i3 == i4) prints false because, even though the values of both objects are 432, this number is outside the cached range and will therefore not be cached, making the objects unequal. Am I required to know the cached ranges for the different wrapper classes for the OCAJP 8 exam?

Thanks again for your help!

Regards,
Shane
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:My question: it seems to me that the return values of all of these five ways can be assigned to a wrapper object as well as to a primitive. I have added some examples in the code to illustrate this. Is this assumption correct? Is this because unboxing is implicitly happening when you assign one of these returned values to a primitive? Or am I overlooking anything here?


Yes, that's correct! The autoboxing feature (added in Java 5) makes sure you can assign a primitive to a wrapper object and the other way around.

If you would compile your class using a Java version before Java 5, you'll get the following output:
javac -source 1.4 WrapperClasses.java

WrapperClasses.java:7: error: incompatible types: Character cannot be converted to char
   char c3 = new Character('s');
             ^
WrapperClasses.java:8: error: incompatible types: Character cannot be converted to char
   char c4 = Character.valueOf('s');
                              ^
WrapperClasses.java:10: error: incompatible types: Integer cannot be converted to int
   int i1 = Integer.valueOf("320");
                           ^
WrapperClasses.java:14: error: incompatible types: int cannot be converted to Integer
   Integer i4 = Integer.valueOf("432").intValue();
                                               ^
WrapperClasses.java:21: error: incompatible types: short cannot be converted to Short
   Short s1 = Short.parseShort("3");
                              ^
WrapperClasses.java:29: error: incompatible types: Long cannot be converted to long
   long l2 = l1;
             ^
WrapperClasses.java:30: error: incompatible types: long cannot be converted to Long
   Long l3 = l2;
             ^
7 errors


Son in Java versions prior to Java 5, the developer was responsible to make sure that the return type of these methods and the variable type were a match made in heaven


Shane Jensen wrote:2) System.out.println(i3 == i4) prints false because, even though the values of both objects are 432, this number is outside the cached range and will therefore not be cached, making the objects unequal. Am I required to know the cached ranges for the different wrapper classes for the OCAJP 8 exam?


First of all please note that not every method you have mentioned in your first question (to create a wrapper class object) will use the cache and therefore will result in false when compared with the == operator (although they are in range). Most study guides mention the cached ranges for the different wrapper classes, so yes you need to know them for the exam.

This excellent topic contains everything you need to know for the exam (and probably more) about wrapper classes. Definitely worth reading!
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Thanks for the confirmation and the link, Roel. Very helpful.
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic