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.
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.
So it can parse the String and give you a
LongorIntegeras the docuemntation states.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.
80is a valid decimal which can be turned into an octal number.80is not a valid octal (or binary) so you cannot parse it as an octal.