File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

valueOf

 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am really confused which class method valueOf takes string which doesnt....now what i have seen so far is that all wrapper objects take String and primitive value in thier contrcuotrs except charcahter which takes only character and not String....i thnk it is the smewith valueOf method....except character class all class's valueOf takes primitve value ad string

am i right?
 
Martin Sergeant
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The valueOf method is a static method of all Wrappers except Chraarcter (It wouldn't make much sense for a Character).
It take a String argument and a optional int which specifies the base of the number in the String. Obviously, the Boolean
valueOf() method does not have the optional int paramater (again this would not make sense)

The method returns the Wrapper class, so

Integer i = Integer.valueOf("23"); would be a valid use of valueOf()

However, just to confuse you, the Boolean class has another valueOf () method which takes a boolean primitive.

Hope this all makes sense

Martin
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dont confuse me...i have the exam on 31st.....want to start the new year with a happy note
 
Martin Sergeant
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't mean to confuse you, it was merely it was a turn of phrase meaning
that the API is not being consistent because for same strange reason the Boolean valueOf takes a primitive value
(as well as a String) where as the valueOf methods for all the other Wrapper classes just take a String.
I can see no logical reason for this,

Martin
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wish there was a method to remember all these.....
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Integer valueOf method takes String and a primitive too...only differecne is that Float valueOf takes only float or a string and not a double as its constrcuotr.....
do i haveto really remeemebr all this for the exam ?
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't tough to remember. All the wrapper classes' constructor takes a primitive and String as a value (except Character class which takes only a primitive, and Float which takes float, double and String in constructor). All the valueOf methods take primitive and String as a value (except for Character which takes only a primitive). Is this that hard to remember??
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no..... one more addition to that valueOf for Float takes only float and not double.....and how do you remember the other methods...like all primitive wrappers have a xxxValue but Boolean has only booleanValue and character has only charValue....i think your post and my post combined will be enough for rattifying(sorry that means learning by heart)



 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raju Champaklal wrote:one more addition to that valueOf for Float takes only float and not double.

You are making things complicated for yourself. valueOf method of all wrapper classes takes String and primitive (except Character which takes only primitive). You don't need to remember anything else. valueOf method of Float class is similar to other classes. Its the constructor of Float class which takes float and double, that is what you need to remember. As far as the xxxValue of method goes, again there are only two exceptions to the rule, so it shouldn't be hard to remember...
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but valueOf of Float takes only float and does not take a double
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raju Champaklal wrote:but valueOf of Float takes only float and does not take a double

I'm not denying that. So the valueOf method of Float class takes primitive and String value as other wrapper classes. So how is it different from valueOf methods of other classes??
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think now i am will never be able to forget this... actually my point was that the Float constrcuotr takes a float, double, string...whereas the valueOf takes only float or a string
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Raju,

Practice makes a man perfect... and I think that you are not doing your part in practice. Write a couple of lines of code on the discussions base on above...I am sure every thing will be clear as water for you after that. Try out this suggestion, it will not take much time and after it is clear to you, you are not going to forget it, for ever....I am sure things will work out for you....

All the best.....do well.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic