aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrapper Classes" Watch "Wrapper Classes" New topic
Author

Wrapper Classes

Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

hi guys,

i'm a little confused about below issues ,, could you please help?
1- from K&B book , page 240 ,The valueOf() Methods : it states :
The two (well, usually two) static valueOf() methods provided in most of the
wrapper classes give you another approach to creating wrapper objects. Both
methods take a String representation of the appropriate type of primitive as
their first argument
>>>>>>>>>>>>>>>>>>IF THE METHOD ONLY TAKES ONE ARGUMENT IT CAN BE OF TYPE OF THE PRIMITIVE BEING CONSTRUCTED AS WELL LIKE : Float f2 = Float.valueOf("3.14f"); it can be declared as Float f2 = Float.valueOf(3.14f); which means it is not only String which can be passed as argument !!
am i right?

2- why do i get run time error for the followin?


``Worry does not empty tomorrow of its sorrow; it empties today of its strength.''
Prashanth Patha
Ranch Hand

Joined: May 06, 2011
Posts: 62

Hama Kamal wrote:hi guys,

i'm a little confused about below issues ,, could you please help?
1- from K&B book , page 240 ,The valueOf() Methods : it states :
The two (well, usually two) static valueOf() methods provided in most of the
wrapper classes give you another approach to creating wrapper objects. Both
methods take a String representation of the appropriate type of primitive as
their first argument
>>>>>>>>>>>>>>>>>>IF THE METHOD ONLY TAKES ONE ARGUMENT IT CAN BE OF TYPE OF THE PRIMITIVE BEING CONSTRUCTED AS WELL LIKE : Float f2 = Float.valueOf("3.14f"); it can be declared as Float f2 = Float.valueOf(3.14f); which means it is not only String which can be passed as argument !!
am i right?



Hi Hama Kamal,

text in K&B means that usually there would be two valueOf() methods in most of the wrapper classes (such as Integer,Long,Short etc.. ) that takes the first argument ( it doesn't mean that it is the only argument) in String representation and second argument(if exists) takes radix.
And as you wrote, we can also send float literal to Float ,int literal to Integer, long literal to Long etc., instead of String representation.


Thanks & Regards
Prashanth

Prashanth Patha
Ranch Hand

Joined: May 06, 2011
Posts: 62

Hama Kamal wrote:hi guys,

2- why do i get run time error for the followin?


Here , the string "0xa" is not correct... we have to write it as Integer.valueOf("a",16) . This is because , it already requires second argument that shows radix.
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Prashanth Patha wrote:
Hama Kamal wrote:hi guys,

2- why do i get run time error for the followin?


Here , the string "0xa" is not correct... we have to write it as Integer.valueOf("a",16) . This is because , it already requires second argument that shows radix.



Many Thanks Prashanth Patha for such a perfect explanation..
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Prashanth Patha wrote:
Hama Kamal wrote:hi guys,

i'm a little confused about below issues ,, could you please help?
1- from K&B book , page 240 ,The valueOf() Methods : it states :
The two (well, usually two) static valueOf() methods provided in most of the
wrapper classes give you another approach to creating wrapper objects. Both
methods take a String representation of the appropriate type of primitive as
their first argument
>>>>>>>>>>>>>>>>>>IF THE METHOD ONLY TAKES ONE ARGUMENT IT CAN BE OF TYPE OF THE PRIMITIVE BEING CONSTRUCTED AS WELL LIKE : Float f2 = Float.valueOf("3.14f"); it can be declared as Float f2 = Float.valueOf(3.14f); which means it is not only String which can be passed as argument !!
am i right?



Hi Hama Kamal,

text in K&B means that usually there would be two valueOf() methods in most of the wrapper classes (such as Integer,Long,Short etc.. ) that takes the first argument ( it doesn't mean that it is the only argument) in String representation and second argument(if exists) takes radix.
And as you wrote, we can also send float literal to Float ,int literal to Integer, long literal to Long etc., instead of String representation.



Hello Prashanth

but i thinks for those which are smaller than integer it should be only String or variable like:

>> Compilation error

but we can write it in one of the follwing forms
1-
2-


am i right?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19067
    
  40

Hama Kamal wrote:
but we can write it in one of the follwing forms
1-
2-

am i right?


Sure. And this will also work.



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Prashanth Patha
Ranch Hand

Joined: May 06, 2011
Posts: 62

@Henry Wong,
Doubt :

as you said , the following line perfectly executes :



I have some doubt here , that previously in other thread you said something like " this type of line when compiled gets converted to something like this :



If this happens , i think it won't get compiled successfully.So, something different thing will be happened.. like :

" at first , the int literal 5 would be converted to short literal 5 and it will be replaced by : Short s=Short.valueOf(short literal 5 will be here); " .. am i right ? Because, if it is like : Short s= Short.valueOf(5) // it won't get compiled.





Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19067
    
  40

Prashanth Patha wrote:@Henry Wong,
I have some doubt here , that previously in other thread you said something like " this type of line when compiled gets converted to something like this :


As a reference, I believe the other thread is this one...

http://www.coderanch.com/t/545213/java-programmer-SCJP/certification/confusing-stuff-wrapper-classes

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19067
    
  40

Prashanth Patha wrote:@Henry Wong,
Doubt :
as you said , the following line perfectly executes :



I have some doubt here , that previously in other thread you said something like " this type of line when compiled gets converted to something like this :



If this happens , i think it won't get compiled successfully.So, something different thing will be happened.. like :

" at first , the int literal 5 would be converted to short literal 5 and it will be replaced by : Short s=Short.valueOf(short literal 5 will be here); " .. am i right ? Because, if it is like : Short s= Short.valueOf(5) // it won't get compiled.



See section 5.2 of the Java Language Specification, about 3 paragraphs down, the exact line is...

In addition, if the expression is a constant expression (ยง15.28) of type byte, short, char or int :

A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is :
  • Byte and the value of the constant expression is representable in the type byte.
  • Short and the value of the constant expression is representable in the type short.
  • Character and the value of the constant expression is representable in the type char.


  • So, for compile time constants (which int literals are), that fit into a value of a short (which 5 does), it will be implicitly narrowed before it is boxed to a Short.

    In the other thread, the example used was a Float -- and if you look at the specification, you'll notice that Float is not on the list of supported types for this rule.

    Henry
    Prashanth Patha
    Ranch Hand

    Joined: May 06, 2011
    Posts: 62

    Thank you Henry Wong for your nice reply.............
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Wrapper Classes