Question

I have a couple of question related to these wrapper classes' methods.

Firstly, why does the method Long (or Integer) take a String as a parameter in the valueOf method? and instead it takes a numeric primitive in the toString method? (see the below examples)

Secondly, why does the second line of code listed below not work (by taking a String as first argument) whereas the first line works fine (by taking a long(or int) as first argument).

Both methods should return the value, respectively in String and in Long type, of the value stated in the first argument converted in the radix specified in the second argument (in this case 8).

String s = Long.toString(80,8)// takes a numeric primitive and it works fine.

Long l = Long.valueOf("80",8)// takes a string as first argument it does not compile,
                              //(as it was because in radix 8 cannot "read" the number 8
                              // and therefore it prompts an NumberFormatException.
Was it helpful?

Solution

Given it doesn't make sense to have multiple methods which do the same thing, it is logical and not at all surprising that different methods do different things with different arguments.

Firstly, why does the method Long (or Integer) take a String as a parameter in the valueOf method?

So it can parse the String and give you a Long or Integer as the docuemntation states.

and instead it takes a numeric primitive in the toString method?

valueOf turns a String into an object and toString takes a value and turns it into a String. Given these do almost the opposite things you would expect them to be the other way around.

Secondly, why does the first line of code listed below not work (by taking a String as first argument) whereas the second line works fine (by taking a long(or int) as first argument).

80 is a valid decimal which can be turned into an octal number. 80 is not a valid octal (or binary) so you cannot parse it as an octal.

OTHER TIPS

  1. These methods convert from, and to, String representations, respectively. What would you expect?

  2. The first line of code calls a method that does not take a String argument. It converts to a String, so why would that be input? it's output

"80" is not a valid octal number. 8 is not a digit in octal. The value 8 is "10".

Actually both methods are bound :

/** 
 * Returns the string representation of the <code>long</code> argument. 
 * <p> 
 * The representation is exactly the one returned by the 
 * <code>Long.toString</code> method of one argument. 
 * 
 * @param   l   a <code>long</code>. 
 * @return  a string representation of the <code>long</code> argument. 
 * @see     java.lang.Long#toString(long) 
 */ 
public static String valueOf(long l) { 
    return Long.toString(l, 10); 
}

It seems that Long.toString() takes a long as first argument, see the code above.

Long.valueOf(String, int) converts a String to a Long.

Long.toString(long. int) converts a long to a String.

Both functions take a radix which is used for the conversion.

I recommend having a look at the JavaDoc of Long.toString(long, int) and Long.valueof(String, int). The documentation is great and explains it very well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top