English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Variables are memory allocations to store values. That is, when creating variables, memory space needs to be allocated in memory.
The memory management system allocates storage space for variables based on their type, and the allocated space can only be used to store data of that type.
Therefore, by defining different types of variables, integers, decimals, or characters can be stored in memory.
Java's two major data types:
Built-in data types
Reference data types
Java provides eight basic types. Six numeric types (four integer types, two floating-point types), one character type, and one boolean type.
byte:
The byte data type is8Bit, signed, integer represented in binary complement form;
The minimum value is -128(-2^7);
The maximum value is 127(2^7-1);
The default value is 0;
The byte type is used to save space in large arrays, mainly to replace integers, because the space occupied by a byte variable is only one-fourth of the int type;
Example: byte a = 100, byte b = -50.
short:
The short data type is 16 Bit, a signed integer represented by binary complement code
The minimum value is -32768(-2^15);
The maximum value is 32767(2^15 - 1);
The Short data type can also save space like byte. A short variable takes half the space of an int variable;
The default value is 0;
Example: short s = 1000, short r = -20000.
int:
The int data type is32Bit, a signed integer represented by binary complement code;
The minimum value is -2,147,483,648(-2^31);
The maximum value is 2,147,483,647(2^31 - 1);
Generally, integer variables are of int type by default;
The default value is 0;
Example: int a = 100000, int b = -200000.
long:
The long data type is 64 Bit, a signed integer represented by binary complement code;
The minimum value is -9,223,372,036,854,775,808(-2^63);
The maximum value is 9,223,372,036,854,775,807(2^63 -1);
This type is mainly used in systems that need to compare large integers;
The default value is 0L;
Example: long a = 100000L, Long b = -200000L.
"L" is theoretically case-insensitive, but if written as "l", it is easy to be confused with the number"1"Ambiguous, not easy to distinguish. Therefore, it is better to use uppercase.
float:
The float data type is single precision,32Bit, conforms to IEEE 754Standard floating-point number;
The float can save memory space when storing large floating-point arrays;
The default value is 0.0f;
Floating-point numbers cannot be used to represent exact values, such as currency;
Example: float f1 = 234.5f.
double:
The double data type is double precision,64 Bit, conforms to IEEE 754Standard floating-point number;
The default floating-point type is double type;
The double type cannot represent an exact value, such as currency;
The default value is 0.0d;
Example: double d1 = 123.4.
boolean:
The boolean data type represents a bit of information;
There are only two values: true and false;
This type is only used as a flag to record true/false condition;
The default value is false;
Example: boolean one = true.
char:
The char type is a single 16 Bit Unicode character;
The minimum value is \u0000 (which is 0);
The maximum value is \uffff (which is65,535);
The char data type can store any character;
Example: char letter = 'A';
For the value range of the basic types of numeric types, there is no need to memorize them forcibly, because their values are already defined as constants in the corresponding wrapper classes. Please see the following examples:
public class PrimitiveTypeTest { public static void main(String[] args) { // byte System.out.println("Basic types: byte Binary digits: ") + Byte.SIZE) System.out.println("包装类:java.lang.Byte") System.out.println("最小值:Byte.MIN_VALUE=") + Byte.MIN_VALUE) System.out.println("最大值:Byte.MAX_VALUE=") + Byte.MAX_VALUE) System.out.println(); // short System.out.println("基本类型:short 二进制位数:") + Short.SIZE) System.out.println("包装类:java.lang.Short") System.out.println("最小值:Short.MIN_VALUE=") + Short.MIN_VALUE) System.out.println("最大值:Short.MAX_VALUE=") + Short.MAX_VALUE) System.out.println(); // int System.out.println("基本类型:int 二进制位数:") + Integer.SIZE) System.out.println("包装类:java.lang.Integer") System.out.println("最小值:Integer.MIN_VALUE=") + Integer.MIN_VALUE) System.out.println("最大值:Integer.MAX_VALUE=") + Integer.MAX_VALUE) System.out.println(); // long System.out.println("基本类型:long 二进制位数:") + Long.SIZE) System.out.println("包装类:java.lang.Long") System.out.println("最小值:Long.MIN_VALUE=") + Long.MIN_VALUE) System.out.println("最大值:Long.MAX_VALUE=") + Long.MAX_VALUE) System.out.println(); // float System.out.println("基本类型:float 二进制位数:") + Float.SIZE) System.out.println("包装类:java.lang.Float") System.out.println("最小值:Float.MIN_VALUE=") + Float.MIN_VALUE); System.out.println("Maximum value: Float.MAX_VALUE="); + Float.MAX_VALUE); System.out.println(); // double System.out.println("Primitive 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("Maximum value: Double.MAX_VALUE="); + Double.MAX_VALUE); System.out.println(); // char System.out.println("Primitive type: char Binary bits: "); + Character.SIZE); System.out.println("Wrapper class: java.lang.Character"); // Output Character.MIN_VALUE to the console in numeric form instead of character form System.out.println("Minimum value: Character.MIN_VALUE="); + (int) Character.MIN_VALUE); // Output Character.MAX_VALUE to the console in numeric form instead of character form System.out.println("Maximum value: Character.MAX_VALUE="); + (int) Character.MAX_VALUE); } }
The compiled code output result is as follows:
Primitive type: byte Binary bits:8 Wrapper class: java.lang.Byte Minimum value: Byte.MIN_VALUE=-128 Maximum value: Byte.MAX_VALUE=127 Primitive type: short Binary bits:16 Wrapper class: java.lang.Short Minimum value: Short.MIN_VALUE=-32768 Maximum value: Short.MAX_VALUE=32767 Primitive type: int Binary bits:32 Wrapper class: java.lang.Integer Minimum value: Integer.MIN_VALUE=-2147483648 Maximum value: Integer.MAX_VALUE=2147483647 Primitive type: long Binary bits:64 Wrapper class: java.lang.Long Minimum Value: Long.MIN_VALUE=-9223372036854775808 Maximum Value: Long.MAX_VALUE=9223372036854775807 Primitive Type: float Binary Bits:32 Wrapper Class: java.lang.Float Minimum Value: Float.MIN_VALUE=1.4E-45 Maximum Value: Float.MAX_VALUE=3.4028235E38 Primitive Type: double Binary Bits:64 Wrapper Class: java.lang.Double Minimum Value: Double.MIN_VALUE=4.9E-324 Maximum Value: Double.MAX_VALUE=1.7976931348623157E308 Primitive Type: char Binary Bits:16 Wrapper Class: java.lang.Character Minimum Value: Character.MIN_VALUE=0 Maximum Value: Character.MAX_VALUE=65535
The minimum and maximum values of Float and Double are output in scientific notation, with "E" at the end+the number " before E represents that the number before E should be multiplied by10to the power of. For example3.14E3is3.14 × 103 =3140,3.14E-3 is 3.14 x 10-3 =0.00314.
In fact, there is another basic type void in JAVA, which also has its corresponding wrapper class java.lang.Void, but we cannot operate on them directly.
The following table lists the default values for each type in Java:
Data Types | Default Values |
---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | '\u0000' |
String (or any object) | null |
boolean | false |
public class Test { static boolean bool; static byte by; static char ch; static double d; static float f; static int i; static long l; static short sh; static String str; public static void main(String[] args) { System.out.println("Bool: ") + bool); System.out.println("Byte: ") + by); System.out.println("Character: ") + ch); System.out.println("Double: ") + d); System.out.println("Float: ", + f); System.out.println("Integer: ", + i); System.out.println("Long: ", + l); System.out.println("Short: ", + sh); System.out.println("String: ", + str); } }
Example output is:
Bool: false Byte: 0 Character: Double: 0.0 Float: 0.0 Integer: 0 Long: 0 Short: 0 String: null
In Java, reference type variables are very similar to C/C++pointers. Reference types point to an object, and the variables that point to the object are reference variables. These variables are specified with a particular type at declaration time, such as Employee, Puppy, etc. Once a variable is declared, its 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("w3codebox
Constants cannot be modified at runtime.
In Java, the final keyword is used to modify constants, with a declaration similar to that of variables:
final double PI = 3.1415927;
Although constant names can also be in lowercase, uppercase letters are usually used for constants to make them easily recognizable.
literals can be assigned to variables of any built-in type. For example:
byte a = 68; char a = 'A'
byte, int, long, and short can be represented in decimal,16number systems as well as8number systems to represent.
When using constants, the prefix 0 indicates 8 number systems, while the prefix 0x represents 16 number systems, for example:
int decimal = 100; int octal = 0144; int hexa = 0x64;
Like other languages, Java string literals are sequences of characters enclosed in double quotes. Here are examples of string literals:
"Hello World" "two\nlines" "\"This is in quotes\""
String literals and character literals can contain any Unicode character. For example:
char a = '\u000'1'; String a = "\u0001";
Java language supports some special escape character sequences.
Symbol | Character meaning |
---|---|
\n | New line (0x0a) |
\r | Carriage return (0x0d) |
\f | Form feed (0x0c) |
\b | Backspace (0x08) |
\0 | Null character (0x0) |
\s | Space (0x20) |
\t | Tab |
\" | Double quote |
\' | Single quote |
\\ | Backslash |
\ddd | Octal character (ddd) |
\uxxxx | 16Binary Unicode character (xxxx) |
Integral types, real types (constants), and character types can be mixed in operations. In operations, data of different types are first converted to the same type and then operated.
Conversion from low to high.
Low ------------------------------------High byte, short, char -> int -> long -> float -> double
Data type conversion must meet the following rules:
1. It is not possible to perform type conversion on boolean type.
2. It is not possible to convert an object type to an object of an unrelated class.
3. When converting a larger type to a smaller type, it is necessary to use explicit type conversion.
4. The conversion process may cause overflow or loss of precision, for example:
int i =128; byte b = (byte)i;
because byte type is 8 bits, the maximum value is127, so when int is forced to be converted to byte type, the value 128 will cause overflow.
5. The conversion from floating-point number to integer is obtained by discarding the decimal part, not rounding, for example:
(int)23.7 == 23; (int)-45.89f == -45
must satisfy that the number of bits of the data type before conversion is less than the number of bits of the data type after conversion, for example: the number of bits of short data type is16bits, which can be automatically converted to32of int type, and the number of bits for float data type is32, which can be automatically converted to64of double type.
public class ZiDongLeiZhuan{ public static void main(String[] args){ char c1='a';//Define a char type int i1 = c1;//char automatically converted to int System.out.println("The value of char automatically converted to int is equal to"+i1); char c2 = 'A';//Define a char type int i2 = c2+1;//char type and int type calculation System.out.println("The value of char type and int calculation is equal to"+i2); } }
The running result is:
The value of char automatically converted to int is equal to97 The value of char type and int calculation is equal to66
Parsing:c1 The value is a character a , Check the ASCII code table to find the corresponding int type value 97, A corresponds to 65So i2=65+1=66.
1. The condition is that the data type to be converted must be compatible.
2. Format: (type)value type is the data type to be cast to Example:
public class QiangZhiZhuanHuan{ public static void main(String[] args){ int i1 = 123; byte b = (byte)i1;//Casting to byte System.out.println("The value after casting int to byte is equal to"+b); } }
Running Result:
The value after casting int to byte is equal to123
1. The default type of integers is int.
2. Floating point types do not have this situation because when defining the float type, it must be followed by F or f at the end of the number.
This section explains the basic data types of Java. The next section will discuss different variable types and their usage.