Forum Stats

  • 3,817,472 Users
  • 2,259,339 Discussions
  • 7,893,790 Comments

Discussions

valueOf vs parseInt

936584
936584 Member Posts: 13
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

Answers

  • 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.
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    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.
  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    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
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    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();
  • 936584
    936584 Member Posts: 13
    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.
  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    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
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    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.
  • Unknown
    edited Jun 5, 2012 11:11AM
    >
    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.
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    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.
  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    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
This discussion has been closed.