This is the third day of my participation in the August More text Challenge. For details, see:August is more challenging
Java basic data types
- Java’s two main data types:
Built-in data type
The Java language provides eight basic types. There are six numeric types (four integers, two floats), one character type, and one Boolean type.
Byte:
- Byte The data type is an 8-bit, signed, binary complement integer.
- The minimum is -128 (-2^7);
- The maximum is 127 (2^7-1);
- The default value is 0;
- Byte is used to save space in large arrays, mainly instead of integers, because byte variables take up about a quarter of the space of int variables.
- Example: byte a = 100, byte b = -50.
Short:
- The short data type is a 16-bit, signed integer in binary complement
- The minimum is -32,768 (-2^15);
- The maximum is 32767 (2^15-1);
- The Short data type can also save space as byte does. A short is half the space of an int;
- The default value is 0;
- Example: short s = 1000, short r = -20000.
Int:
- Int is a 32-bit, signed, binary complement integer.
- The minimum is -2,147,483,648 (-2^31);
- The maximum is 2,147,483,647 (2^ 31-1);
- Generally, integer variables default to int;
- The default value is 0;
- Example: int a = 100000, int b = -200000.
Long:
- The long data type is a 64-bit, signed, binary complement integer;
- The minimum value is – 9223372036854775808 (2 ^ 63);
- Maximum is 9223372036854775807 (2 ^ 63-1);
- This type is mainly used in systems where large integers are required;
- The default value is 0L;
- Example: long a = 100000L, long B = -200000L. “L” is not case sensitive in theory, but if written as “L”, it will be confused with the number “1”, which is not easy to distinguish. So it’s better to capitalize.
Float:
- The float data type is a single-precision, 32-bit, IEEE 754 compliant floating point number.
- Float saves memory when storing large floating point arrays;
- Default is 0.0f;
- Floating-point numbers cannot be used to represent exact values, such as currencies;
- Example: float f1 = 234.5f.
Double:
-
The double data type is a double-precision, 64-bit, IEEE 754 compliant floating point number.
-
The default floating-point type is double;
-
Double also cannot represent an exact value, such as money;
-
Default is 0.0d;
-
Example:
double d1 = 7D ; double d2 = 7.; double d3 = 8.0; double d4 = 8.D; double d5 = 12.9867; Copy the code
7 is an int literal, while 7D, 7., and 8.0 are double literals.
Boolean:
- Boolean The data type represents one bit of information;
- There are only two values: true and false.
- This type is only used as a flag to record true/false cases;
- The default is false;
- Example: Boolean one = true.
Char:
- The char type is a single 16-bit Unicode character;
- The minimum is \u0000 (the decimal equivalent is 0);
- The maximum value is \ uFFFF (i.e., 65535);
- The char data type can store any character;
- Example: char letter = ‘A’; .
Reference types
- In Java, variables that reference types are very similar to C/C++ Pointers. A reference type points to an object, and a variable that points to an object is a reference variable. These variables are declared with a specific type, such as Employee, Puppy, and so on. Once a variable is declared, the type cannot be changed.
- Objects and arrays are reference data types.
- The default value for all reference types is NULL.
- A reference variable can be used to refer to any compatible type.
- Example: Site Site = new Site(“Runoob”).
public class learn_3 {
public static void main(String[] args) {
// byte
System.out.println("Basic type: byte + Byte.SIZE);
System.out.println("Wrapper class: java.lang.byte");
System.out.println("Minimum value: byte.min_value =" + Byte.MIN_VALUE);
System.out.println("Maximum: byte.max_value =" + Byte.MAX_VALUE);
System.out.println();
// short
System.out.println("Basic type: short binary bits:" + Short.SIZE);
System.out.println("Wrapper class: java.lang.Short");
System.out.println("Minimum value: short-.min_value =" + Short.MIN_VALUE);
System.out.println(Max: Short.MAX_VALUE=" + Short.MAX_VALUE);
System.out.println();
// int
System.out.println("Basic type: int binary bits:" + Integer.SIZE);
System.out.println("Wrapper class: java.lang.integer");
System.out.println("Minimum value: integer.min_value =" + Integer.MIN_VALUE);
System.out.println("Maximum value: Integer.MAX_VALUE=" + Integer.MAX_VALUE);
System.out.println();
// long
System.out.println("Basic type: long Binary bits:" + Long.SIZE);
System.out.println("Wrapper class: java.lang.long");
System.out.println("Minimum value: long.min_value =" + Long.MIN_VALUE);
System.out.println("Max: long. MAX_VALUE=" + Long.MAX_VALUE);
System.out.println();
// float
System.out.println("Basic type: float binary bits:" + Float.SIZE);
System.out.println("Wrapper class: java.lang.float");
System.out.println("Minimum value: Float.MIN_VALUE=" + Float.MIN_VALUE);
System.out.println("Max: Float.MAX_VALUE=" + Float.MAX_VALUE);
System.out.println();
// double
System.out.println("Basic type: double binary bits:" + Double.SIZE);
System.out.println("Wrapper class: java.lang.double");
System.out.println("Minimum value: Double.MIN_VALUE=" + Double.MIN_VALUE);
System.out.println("Max: Double.MAX_VALUE=" + Double.MAX_VALUE);
System.out.println();
// char
System.out.println("Base type: char binary bits:" + Character.SIZE);
System.out.println("Wrapper class: java.lang.Character");
// Output character.min_value to the console as a number rather than as a Character
System.out.println(Min: character.min_value ="
+ (int) Character.MIN_VALUE);
// Output character.max_value to the console as a number rather than as a Character
System.out.println(Max: character. MAX_VALUE="
+ (int) Character.MAX_VALUE); }}Copy the code