What is stored in Java program variables?
When declaring variables in a Java program, we need to specify the type and name of the variable. Here are some common Java variable types:
Java provides several basic data types, including integer, float, character, and boolean types.
Integer data types (int, short, long, byte): used to store integer values.
Floating point (float, double): Used to store decimal numbers, i.e. numbers with a fractional part.
Character type (char): Used to store individual characters.
Boolean: used to store logical values, can only be true or false.
2. Reference data types: In addition to basic data types, Java also has some reference data types that can be used to store complex data structures such as strings, arrays, and classes.
String: Used to store text data.
Array: a data structure used to store multiple pieces of data of the same type.
Class: used to define custom data types.
Sample code:
// 定义一个整型变量 int age = 25; // 定义一个浮点型变量 double height = 1.75; // 定义一个字符型变量 char grade = 'A'; // 定义一个布尔型变量 boolean isStudent = true; // 定义一个字符串变量 String name = "John Doe"; // 定义一个整型数组变量 int[] numbers = {1, 2, 3, 4, 5};
By using variables, we can store and manipulate different types of data in a program, making it more flexible and scalable.