aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes valueOf 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 "valueOf" Watch "valueOf" New topic
Author

valueOf

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
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?


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Martin Sergeant
Greenhorn

Joined: May 20, 2003
Posts: 24
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

Joined: Dec 10, 2009
Posts: 521
dont confuse me...i have the exam on 31st.....want to start the new year with a happy note
Martin Sergeant
Greenhorn

Joined: May 20, 2003
Posts: 24
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

Joined: Dec 10, 2009
Posts: 521
i wish there was a method to remember all these.....
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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

Joined: Dec 10, 2009
Posts: 521
but valueOf of Float takes only float and does not take a double
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

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

Joined: Dec 10, 2009
Posts: 521
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

Joined: Apr 02, 2008
Posts: 823

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.


Regards, Sud.
SCJP 5 ScjpFAQ JLS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: valueOf