Java is an object-oriented programming language with rich data types. In Java, data types can be divided into two major categories: basic data types and reference data types. This article will provide a detailed analysis of these two categories and provide relevant code examples.
1. Basic data types
There are eight basic data types in Java, namely: byte, short, int, long, float, double, char and boolean. These basic data types can be used to declare variables and store simple data.
- byte: The byte data type is an integer data type. It occupies 1 byte and the value range is -128 to 127. For example, you can use the byte type to store the number of bytes representing the file size.
byte fileSize = 100;
Copy after login
- short: The short data type is also an integer data type. It occupies 2 bytes and the value range is -32768 to 32767. You can use the short type to store larger integer values.
short num = 1000;
Copy after login
- int: The int data type is the most commonly used integer data type, occupying 4 bytes, and the value range is -2147483648 to 2147483647. The int type can be used to store integer values.
int age = 20;
Copy after login
- long: The long data type is also an integer data type, occupying 8 bytes, and the value range is -9223372036854775808 to 9223372036854775807. You can use the long type to store larger integer values.
long population = 10000000000L;
Copy after login
- float: The float data type is a type of floating point data type, occupying 4 bytes and used to store values with decimal points. It should be noted that when declaring the float type, you need to add the letter "f" after the value to represent it as a floating point number.
float price = 3.99f;
Copy after login
- double: The double data type is also a type of floating-point data type, occupying 8 bytes and used to store a larger range of floating-point numbers. Unlike the float type, the double type can be declared without any modifiers.
double average = 80.5;
Copy after login
- char: The char data type is used to represent a single character, occupying 2 bytes, and the value range is 0 to 65535. Characters can be stored using the char type.
char grade = 'A';
Copy after login
- boolean: The boolean data type is used to represent Boolean values, with only two values: true and false. Used for logical judgment.
boolean isStudent = true;
Copy after login
2. Reference data types
Reference data types refer to non-basic data types, which are defined through classes or interfaces. Java's reference data types include: classes, interfaces, arrays, and enumerations.
- Class: Class is one of the most common reference data types in Java, and objects can be created through classes. For example, here is an example of a class representing a person:
class Person {
String name;
int age;
}
Copy after login
A Person object can be created in the following ways:
Person person = new Person();
person.name = "Tom";
person.age = 20;
Copy after login
- Interface: An interface is a special reference type , which defines a set of abstract methods that can be implemented by classes. For example, here is an example of an interface that defines a printing function:
interface Printable {
void print();
}
Copy after login
The interface can be implemented in the following way:
class Printer implements Printable {
public void print() {
System.out.println("Printing...");
}
}
Copy after login
- Array: An array is a type of A data structure that stores multiple elements of the same type. Arrays can be declared and initialized in the following ways:
int[] numbers = new int[5];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
Copy after login
- Enumeration: An enumeration is a special reference data type that defines a limited, named collection of values. Enumerations can be declared and used in the following ways:
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
Day day = Day.MONDAY;
Copy after login
Summary:
Java data types are divided into basic data types and reference data types. Primitive data types are suitable for storing simple data, while reference data types are suitable for more complex data structures. In actual applications, choosing the appropriate data type as needed can help improve program performance and efficiency.
The above is the detailed content of Parsing the classification of Java data types: exploring their main categories. For more information, please follow other related articles on the PHP Chinese website!