What does this code do?
int age = 30;
String name = "Ana";Options
- Declares an int holding 30 and a String holding Ana (Correct option)
- Compares age with 30 and stores the result in name
- Creates two new classes that are called age and name
- Imports two packages that are called age and name
Answer
Declares two variables with their types: an int (whole number) holding 30 and a String (text) holding Ana
Why
Java is statically typed: every variable must have a declared type before you use it. int is a whole number, String is text, double a decimal number and boolean true or false.
A semicolon ; ends each statement. Text goes in double quotes.
You cannot put text into an int variable: the compiler rejects it, which catches mistakes before running.
Remember
Java: declare the type, then the name, then the value.