This discussion is archived
13 Replies Latest reply: Jun 5, 2012 8:58 AM by TPD-Opitz-Consulting-com RSS

valueOf vs parseInt

936584 Newbie
Currently Being Moderated
This is one of the questions of the 'Questions and Exercises:' section of the 'Numbers and Strings' chapter of the Java Tutorials:

>
What Integer method would you use to convert a string expressed in base 5 into the equivalent int? For example, how would you convert the string "230" into the integer value 65? Show the code you would use to accomplish this task.
>

This was my answer:
String base5String = "230";
int result = Integer.parseInt(base5String, 5);
And this is the answer given in the Java Tutorials:
String base5String = "230";
int result = Integer.valueOf(base5String, 5);
My question is, Is my answer worong? Why to use valueOf instead of parseInt?

Thanks in advance.

Edited by: 933581 on Jun 4, 2012 8:00 AM
  • 1. Re: valueOf vs parseInt
    maheshguruswamy Journeyer
    Currently Being Moderated
    933581 wrote:
    This is one of the questions of the 'Questions and Exercises:' section of the 'Numbers and Strings' chapter of the Java Tutorials:

    >
    What Integer method would you use to convert a string expressed in base 5 into the equivalent int? For example, how would you convert the string "230" into the integer value 65? Show the code you would use to accomplish this task.
    >

    This was my answer:
    String base5String = "230";
    int result = Integer.parseInt(base5String, 5);
    And this is the answer given in the Java Tutorials:
    String base5String = "230";
    int result = Integer.valueOf(base5String, 5);
    My question is, Is my answer worong? Why to use valueOf instead of parseInt?

    Thanks in advance.

    Edited by: 933581 on Jun 4, 2012 8:00 AM
    One returns an Object (in your case its auto converted into a primitive) and the other returns a primitive. The question did not give any specifics on Object vs primitive, performance etc, so I don't think your answer is wrong.
  • 2. Re: valueOf vs parseInt
    gimbal2 Guru
    Currently Being Moderated
    933581 wrote:
    My question is, Is my answer worong? Why to use valueOf instead of parseInt?
    Why not investigate the javadocs for both methods and find out for yourself? Saves you plenty of time when compared to having to post in a forum and wait for answers.
  • 3. Re: valueOf vs parseInt
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    gimbal2 wrote:
    933581 wrote:
    My question is, Is my answer worong? Why to use valueOf instead of parseInt?
    Why not investigate the javadocs for both methods and find out for yourself? Saves you plenty of time when compared to having to post in a forum and wait for answers.
    I think this is a good question because the tutorials answer relies on autoboxing wich I would consider a "bad smell"...

    bye
    TPD
  • 4. Re: valueOf vs parseInt
    gimbal2 Guru
    Currently Being Moderated
    T.PD wrote:
    gimbal2 wrote:
    933581 wrote:
    My question is, Is my answer worong? Why to use valueOf instead of parseInt?
    Why not investigate the javadocs for both methods and find out for yourself? Saves you plenty of time when compared to having to post in a forum and wait for answers.
    I think this is a good question because the tutorials answer relies on autoboxing wich I would consider a "bad smell"...
    Hm good point. The answer is a bit fishy, I would go as far as saying it is actually a mistake in the answers page. Judging by code samples in that area, it should have actually been this:
    int result = Integer.valueOf(base5String, 5).intValue();
  • 5. Re: valueOf vs parseInt
    936584 Newbie
    Currently Being Moderated
    Thank you all for your answers.

    >
    Why not investigate the javadocs for both methods and find out for yourself?
    >

    In fact, I did.

    I just wanted to make sure that my answer was not wrong, may be it is, but I don't see why, according to what I read in the javadoc.

    In fact, I'd choose parseInt rather than valueOf, but, if the Java Tutorials chose the valueOf method rather than the parseInt, it must have been for a good reason, or maybe I'm making a mountain out of a molehill.

    Thanks again.
  • 6. Re: valueOf vs parseInt
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    933581 wrote:
    [...] but, if the Java Tutorials chose the valueOf method rather than the parseInt, it must have been for a good reason,
    Even tutorial writers a simple humans making mistake every now and then... ;o)
    or maybe I'm making a mountain out of a molehill.
    Maybe, but it's hard to play soccer with too many molehills on the field...

    bye
    TPD
  • 7. Re: valueOf vs parseInt
    gimbal2 Guru
    Currently Being Moderated
    933581 wrote:
    In fact, I did.
    Good; then you have proven me wrong, you do have it in your system to look there ;)
    I just wanted to make sure that my answer was not wrong, may be it is, but I don't see why, according to what I read in the javadoc.
    valueOf has its minor benefits over parseInt() especially when you are working with objects in stead of primitives. But in the end the result is the same.
    In fact, I'd choose parseInt rather than valueOf, but, if the Java Tutorials chose the valueOf method rather than the parseInt, it must have been for a good reason, or maybe I'm making a mountain out of a molehill.
    You put too much faith in the web. Its too monstrously big to fill with correct information, you'll find falsehoods at every corner. In fact whole (Java related) websites exist that are completely filled with it.
  • 8. Re: valueOf vs parseInt
    rp0428 Guru
    Currently Being Moderated
    >
    In fact, I'd choose parseInt rather than valueOf
    >
    'parseInt' is going to be executed no matter which one you choose since 'valueOf' does nothing more than call 'parseInt' and use the returned primitive to create a new instance of Integer which it then returns.

    So your code is actually more efficient since the tutorial answer using 'valueOf' wastes the Integer instance by immediately extracting the int value.

    1.4 source code for Integer.java [ *suppressed by moderator* Don't post copyright code here]

    Edited by: EJP on 5/06/2012 10:49

    Edited by: rp0428 on Jun 5, 2012 8:10 AM
    Thanks EJP for catching that - I meant to just reference the Integer.java class.
  • 9. Re: valueOf vs parseInt
    gimbal2 Guru
    Currently Being Moderated
    rp0428 wrote:
    1.4 source code for Integer.java [ *suppressed by moderator* Don't post copyright code here]
    Why 1.4 source code when you try to prove a point? Then use something more recent. If you want to link to JDK code then simply Google for "ClassAndOptionallyMethodName source", you'll usually get a link to grepcode which has the more recent source code readily available and highlighted. Then simply post the link to it, I'm pretty sure that doesn't violate forum rules. (@EJP: right?)

    In any case performance when dealing with micro-optimizations is a weak argument at best and perhaps not the best thing to expose people to when they are learning. There are already too many people out there worrying about premature optimization.
  • 10. Re: valueOf vs parseInt
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    gimbal2 wrote:
    In any case performance when dealing with micro-optimizations is a weak argument at best and perhaps not the best thing to expose people to when they are learning. There are already too many people out there worrying about premature optimization.
    Sure.
    But accidental autoboxing is definitely something to look for: easy to avoid when writing but hard to find later.

    bye
    TPD
  • 11. Re: valueOf vs parseInt
    gimbal2 Guru
    Currently Being Moderated
    T.PD wrote:
    gimbal2 wrote:
    In any case performance when dealing with micro-optimizations is a weak argument at best and perhaps not the best thing to expose people to when they are learning. There are already too many people out there worrying about premature optimization.
    Sure.
    But accidental autoboxing is definitely something to look for: easy to avoid when writing but hard to find later.

    bye
    TPD
    Yes, agreed.
  • 12. Re: valueOf vs parseInt
    rp0428 Guru
    Currently Being Moderated
    >
    Why 1.4 source code when you try to prove a point? Then use something more recent.
    >
    I did that intentionally because T.PD had said that 'the tutorials answer relies on autoboxing' and you agreed.

    Since the two of you mentioned autoboxing I used 1.4 code because that shows that the code has been that way since before 'autoboxing' which was introduced in 1.5.

    In response to
    >
    So your code is actually more efficient since the tutorial answer using 'valueOf' wastes the Integer instance by immediately extracting the int value.
    >
    You replied
    >
    In any case performance when dealing with micro-optimizations is a weak argument at best and perhaps not the best thing to expose people to when they are learning.
    >
    The statement was not so much a performance argument as support for OPs answer being at least as correct as the tutorial answer. I believe that OP's answer is more correct technically speaking since it relies ONLY on the Integer method itself with no unboxing needed; so it will work for any Java version.

    You are correct that for a performance-related question the issue of primitive vs. Object in code like this would be far down (probably absolute end of) the list of things to look at.

    Re the tutorial answer you also said 'it is actually a mistake in the answers page'. I agree but for a different reason than what you cited.

    The first sentence of the tutorial question cited by OP was
    >
    What Integer method would you use to convert a string expressed in base 5 into the equivalent int?
    >
    It is reasonable to interpret the question as wanting an 'int' which is a primitive. Strictly speaking the valueOf method does not produce an 'int'; the answer relies on unboxing to do that. The parseInt method does produce an 'int' which further support's OPs answer as being correct.
  • 13. Re: valueOf vs parseInt
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    rp0428 wrote:
    >
    Why 1.4 source code when you try to prove a point? Then use something more recent.
    >
    I did that intentionally because T.PD had said that 'the tutorials answer relies on autoboxing' and you agreed.

    Since the two of you mentioned autoboxing I used 1.4 code because that shows that the code has been that way since before 'autoboxing' which was introduced in 1.5.
    Nobody said that the library uses autoboxing.

    bye
    TPD

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points