Integer.ValueOf(line,16) converts string value line into an Integer object. In this case radix is 16.
intValue() gets the int value from the Integer object created above.
Furthermore, above two steps are equivalent to Integer.parseInt(line,16).
In order to get more INFO please refer Java API Documentation of Integer class.
Answer from Upul Bandara on Stack OverflowVideos
java - What is "Integer.valueOf().intValue()" supposed to do? - Stack Overflow
Whats the point of using Integer.valueOf(scanner.nextLine()); ?
int value = Integer.valueOf(scanner.nextLine()); Vs Int value = scanner.nextInt();
scanner.nextInt() will leave a '\n' after you press Enter. so if you use a nextLine() after an nextInt(), the nextLine() will read the '\n' and you won't be able to input anything on the console.
More on reddit.comInteger.valueOf() and parseint()
Hello, i have started learning java recently by mooc. But during this course, it uses Integer.valueOf while taking integer inputs, I use same way but whenever i do it intellij suggests me parseint(). And I wonder if it is better to use parseint?
Integer.ValueOf(line,16) converts string value line into an Integer object. In this case radix is 16.
intValue() gets the int value from the Integer object created above.
Furthermore, above two steps are equivalent to Integer.parseInt(line,16).
In order to get more INFO please refer Java API Documentation of Integer class.
Yes, this is equivalent to:
size = Integer.parseInt(line, 16);
Indeed, looking at the implementation, the existing code is actually implemented as effectively:
size = Integer.valueOf(Integer.parseInt(line, 16)).intValue();
which is clearly pointless.
The assignment to -1 in the previous line is pointless, by the way. It would only be relevant if you could still read the value if an exception were thrown by Integer.parseInt, but as the scope of size is the same block as the call to Integer.valueof, it won't be in scope after an exception anyway.
I genuinely don't know what the point of using anything other than Scanner variableName = scanner.nextLine(), if they both give the same output
example.
hello, I'm learning Java through Mooc and I also use other learning sources as a complement, I realized that data entry for variables is taught in two different ways two examples of integer-type inputs:
int value = Integer.valueOf(scanner.nextLine());
vs
int value = scanner.nextInt();
Can both be used together? which one is the best? what is the difference between both?
scanner.nextInt() will leave a '\n' after you press Enter. so if you use a nextLine() after an nextInt(), the nextLine() will read the '\n' and you won't be able to input anything on the console.
Read: The Scanner class and its caveats from the r/javahelp wiki.
In short, the combination of .nextInt and .nextLine is problematic without proper knowledge of the internal workings of Scanner.
Especially for the MOOC use only their way. They have checking for that implemented.