1.2 Variables and Data Types
1. Exam Points
Data types
- Primitive types: int, double, boolean
- Reference types: String, any classes you define
Output: statements to output information on the computer display:
- System.out.println()
- System.out.print()
- A
literal (字面值) is the code representation of a fixed value.
Declare an variable using the right data type.
- int age = 10;
- boolean isStudent = true;
- double price = 2.3;
2. Knowledge Points
(1) Variables
- A
variable is a storage location that holds a value, which can change while the program is running.
- Every variable has a
name and an associated data type.
Declare a variable(声明变量): type variableName
Assign(赋值) a value to a variable:
- Declare a variable and initialize(初始化) the variable in the same statement:
- int x = 10;
- double y = 3.14 ;
- String name = ”Annabelle” ;
Note:
- You must declare a variable before using it.
- You must initialize a variable before using it.
- You must not declare duplicate variables.
(2) Data Types
- A
data type determines the values a variable can hold, and the operations on the variable.
- Data types can be categorized as:
Primitive type(原生类型): int, double, boolean.
- int: int x = 10;
- double: double y = 20.3
- boolean(true or false): boolean isEven = true
Reference type(引用类型): any type other than the primitive types, such as String, Student, Dog, etc.
- In this course, we will only cover 3 primitive types: int, double, boolean.
3. Exercises