Java Data Types Classification
Data types in Java can be classified into two categories: primitive data types and reference data types.
1. Primitive Data Types
The following eight are the primitive data types available in Java:
- byte: Occupies 1 byte, with a range of -128~127; used to represent integers.
- short: Occupies 2 bytes, with a range of -32768~32767; used to represent integers.
- int: Occupies 4 bytes, with a range of -2^31 ~ 2^31-1 (-2147483648~2147483647); used to represent integers.
- long: Occupies 8 bytes, with a range of -9223372036854775808 ~ 9223372036854775807; used to represent integers.
- float: Occupies 4 bytes, with a range of approximately ±3.4e-38~±3.4e38; used to represent floating-point numbers.
- double: Occupies 8 bytes, with a range of approximately ±1.7e-308 ~ ±1.7e308; used to represent floating-point numbers.
- char: Occupies 2 bytes, with a range of 0~65535; used to represent characters.
- boolean: Occupies 1 bit and has only two possible values:
trueorfalse; used to represent boolean values.
2. Reference Data Types
The following are the reference data types available in Java:
- Class (class): Used to define attributes and methods of an object.
- Interface (interface): Used to define behavior for objects.
- Array: Used to store multiple elements of the same type.
- Enum (enum): Used to define a fixed set of constants.
- Annotation (@annotation): Used to provide additional information about code.
Among these, classes, interfaces, arrays, enums, and annotations are defined using keywords such as class, interface, enum, and @interface.
In addition to the aforementioned data types, Java provides several wrapper classes (e.g., Integer, Long, Float, Double, Character, Boolean) that allow primitive data types to be converted into objects. These wrapper classes offer various methods for manipulating their corresponding primitive types. When a primitive type needs to be treated as an object, the appropriate wrapper class can be used for conversion.
Detailed Type Analysis
String
Strings are a commonly used data type in Java and represent sequences of characters. Strings are immutable; once created, their value cannot be modified. In Java, strings are objects located in the java.lang package, so they can be used directly without importing any packages.
Java strings have the following characteristics:
- Immutability: Java strings are immutable; after creation, they cannot be modified. If modification is required, a new string object must be created.
- Variable Length: String objects in Java support variable length and can have their lengths changed by calling relevant methods.
- Unicode Support: Strings in Java use the Unicode character set, allowing them to represent any character, including non-ASCII characters.
- Rich Operations: Java strings provide numerous common operation methods such as concatenation, substring extraction, searching, and replacement.
- String Pool: The string pool is a special object cache in Java used for storing frequently accessed string objects, which helps reduce memory usage and improve performance.
Strings in Java can be represented using double quotes (") or single quotes ('), but single quotes are only valid for representing individual characters, not strings.
The String class in Java provides numerous methods, including:
- length(): Returns the length of the string.
- charAt(int index): Retrieves the character at a specified position within the string.
- substring(int beginIndex, int endIndex): Extracts and returns a substring from this string. (Note: Original text listed
substring()generally; standard Java API uses two parameters for range or one for start-to-end). Based on context "获取字符串的子串", I will translate as the general concept while noting the specific method signature if implied, but sticking to the source's intent of retrieving a substring. - trim(): Removes leading and trailing whitespace from the string.
- toLowerCase() and toUpperCase(): Converts the entire string to lowercase or uppercase characters respectively.
- equals(Object anObject) and equalsIgnoreCase(String anotherString): Compares two strings for equality, with
equalsIgnoreCaseignoring case differences. - startsWith(String prefix) and endsWith(String suffix): Determines whether this String begins with the specified character sequence (
prefix) or ends with it (suffix). - indexOf(int ch), indexOf(String str), lastIndexOf(int ch), and lastIndexOf(String str): Finds the index of a specific character, substring, or its last occurrence within the string.