Java Java Basics Perfecting the Prototype Parsing Integers

I am confused on how to interpret this. Does it mean that if a user were to type in 42, the output would b answertolife?

String answerToLife = "42"; int answer = Integer.parseInt(answerToLife);

1 Answer

Kent Åsvang
Kent Åsvang
18,814 Points
// first we declare a variable 'answerToLife' of type String and initialize it to "42":
String answerToLife = "42";

// then we declare a variable 'answer' of type int and initialize it to using a method provided by the
// Integer-class. 'Integer' is an object wrapper class for the primitive variable type 'int', 'int' by itself doesn't
// contain any methods, but the wrapper class have a method called parseInt, which looks inside a string variable
// for numbers, and if there are numbers, then the method can convert the type from a String to an int. 

int answer = Integer.parseInt(answerToLife);

// some examples: 

System.out.printf("%d", answer); 
// outputs --> 42

System.out.printf("%s", answerToLife);
// outputs --> "42"

Hope this helped.

Why does it return quotes when you put answerToLife and not when you put answer? Also, the lesson did not cover system.out.printf, only console.printf