File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper Classes

 
Hama Kamal
Ranch Hand
Posts: 144
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Prashanth Patha
Ranch Hand
Posts: 62
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prashanth Patha
Ranch Hand
Posts: 62
Eclipse IDE Java Oracle
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prashanth Patha
Ranch Hand
Posts: 62
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 62
    Eclipse IDE Java Oracle
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Henry Wong for your nice reply.............
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic