Data types and variables
A program works with data: names, ages, prices, results. To keep them in memory we use variables. A variable is a named space that holds a value of a specific type. Java is statically typed: you must declare the type of every variable, and the compiler checks that you respect it before anything runs. This looks like extra work at first, but it prevents many silly mistakes.
The primitive types
Java has eight primitive types. You will use four of them almost always: int for whole numbers, double for numbers with decimals, boolean for true or false, and char for a single character. There are also byte, short, long and float. The long type is for when an integer does not fit in an int (for example, the world population or a very large identifier); when you write a long literal you add an L at the end.
public class Main {
public static void main(String[] args) {
int age = 30; // whole number
double price = 19.99; // decimal number
boolean active = true; // true or false
char grade = 'A'; // one character, single quotes
long population = 8_000_000_000L; // underscores help reading
String name = "Lucia"; // text: not primitive, a class
System.out.println(name + " is " + age + " years old");
System.out.println("Price: " + price + ", active: " + active);
System.out.println("Grade: " + grade + ", population: " + population);
}
}Notice two details. First, String starts with a capital letter because it is not a primitive but a class; text goes in double quotes, while a char goes in single quotes. Second, the + operator between text and another value concatenates: it converts the value to text and glues it on. The underscores in 8_000_000_000L do not change the value; they only make big numbers easier to read.
Declaring, assigning and var
Declaring means giving a type and a name; assigning means giving it a value with the = sign. You can do it in two steps or in one. Since Java 10 there is var: if you initialize the variable on the same line, the compiler infers the type for you. Careful: var does not turn Java into a dynamic language. The type is still fixed; you just save typing it when it is obvious.
public class Main {
public static void main(String[] args) {
int counter; // declared, no value yet
counter = 10; // assigned later
counter = counter + 5; // variables can change
System.out.println(counter);
var total = 2.5; // inferred as double
var title = "Java"; // inferred as String
total = total * 2;
System.out.println(title + " " + total);
final int MAX_USERS = 100; // constant: cannot change
System.out.println("Max: " + MAX_USERS);
int a = 7, b = 3; // several of the same type
System.out.println(a / b); // integer division
System.out.println(a / 2.0); // decimal division
}
}- final turns a variable into a constant; by convention it is written in UPPERCASE with underscores.
- A local variable must have a value before it is read; otherwise the compiler gives an error.
- 7 / 3 with integers gives 2 (the decimal part is lost); if one of them is a double, the result is decimal.
- You cannot use var without an initial value or with null.
Names and common mistakes
Variable names use camelCase (userName, totalPrice), start with a lowercase letter and cannot start with a digit or contain spaces. Java is case sensitive: age and Age are different variables. A very common mistake is storing a decimal in an int: int x = 3.5; does not compile, because information would be lost. The compiler forces you to be explicit. Another classic mistake is forgetting the semicolon at the end of the statement.
检验所学
第 1 / 1Which type would you use to store 19.99?
✎ 我的笔记
仅保存在此浏览器中。登录后可保存到账号。0/5000 字符