The variable is to apply for memory to store the value. That is, when creating a variable, you need to apply for space in memory.
The memory management system allocates storage space for variables according to the type of the variables. The allocated space can only be used to store data of this type.
Therefore, by defining different types of variables, you can store integers, decimals, or characters in memory.
The two major data types of Java:
- Built-in data types
- Reference data type
Built-in data types
The Java language 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 is an 8-bit, signed, integer represented in two's complement;
- 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. It mainly replaces integers because the byte variable takes up only a quarter of the int type.
- Example: byte a = 100, byte b = -50.
Short:
- The short data type is a 16-bit, signed integer represented in two's complement
- The minimum value is -32768 (-2^15) ;
- The maximum is 32767 (2^15 - 1) ;
- Short data types can also save space like byte. A short variable is one half of the space occupied by an int variable;
- The default value is 0 ;
- Examples: short s = 1000, short r = -20000.
Int:
- The int data type is a 32-bit, signed integer represented in two's complement;
- The minimum value is -2,147,483,648 (-2^31) ;
- The maximum value is 2,147,483,647 (2^31 - 1) ;
- Integer variables are generally of type int by default;
- The default value is 0 ;
- Examples: int a = 100000, int b = -200000.
Long:
- The long data type is a 64-bit, signed integer represented in two's complement;
- 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 on systems that require large integers;
- The default value is 0L ;
- Examples: long a = 100000L, Long b = -200000L.
"L" is theoretically case-insensitive, but if it is easy to confuse "l" with the number "1", it is not easy to distinguish. So it's best to capitalize.
Float:
- The float data type is a single-precision, 32-bit, floating-point number conforming to the IEEE 754 standard;
- Float saves memory 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 a double-precision, 64-bit, floating-point number that conforms to the IEEE 754 standard;
- The default type of float is double;
- Double types also cannot represent exact values, such as currency;
- The default value is 0.0d ;
- Example: double d1 = 123.4.
Boolean:
- The boolean data type represents one bit of information;
- Only two values: true and false;
- This type only serves as a flag to record true/false conditions;
- 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 (that is, 0);
- The maximum is \uffff (that is, 65,535);
- The char data type can store any character;
- Example: char letter = 'A';.
Examples
For the range of values of primitive types of numeric types, we do not need to force them to remember because their values have been defined in the corresponding wrapper class as constants. Please see the following example:
Examples
public class PrimitiveTypeTest {
public static void main ( String [ ] args ) {
// byte
System . out . println ( " Basic type: byte binary digits: " + Byte . SIZE ) ;
System . out . println ( " Wrapper class: java.lang.Byte " ) ;
System . out . println ( " Minimum: 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 digits: " + Short . SIZE )
System . out . println ( " Wrapper class: java.lang.Short " ) ;
System . out . println ( " Minimum: Short.MIN_VALUE= " + Short . MIN_VALUE ) ;
System . out . println ( "Maximum: Short.MAX_VALUE = " + Short . MAX_VALUE ) ;
System . out . println ( ) ;
// int
System . out . println ( " Basic type: int binary digits: " + Integer . SIZE );
System . out . println ( " Wrapper class: java.lang.Integer " ) ;
System . out . println ( " Minimum: Integer.MIN_VALUE= " + Integer . MIN_VALUE;
System . out . println ( "Maximum value: Integer.MAX_VALUE= " + Integer . MAX_;
System . out . println ( ) ;
// long
System . out . println ( " Basic type: long binary digits: " + Long . SIZE ) ;
System . out . println ( " Wrapper class: java.lang.Long " ) ;
System . out . println ( " Minimum: Long.MIN_VALUE= " + Long . MIN_VALUE ) ;
System . out . println ( " Maximum: Long.MAX_VALUE=" + Long . MAX_VALUE ) ;
System . out . println ( ) ;
// float
System . out . println ( " Basic type: float binary digits: " + Float . SIZE );
System . out . println ( " Wrapper class: java.lang.Float " ) ;
System . out . println ( " Minimum: Float.MIN_VALUE= " + Float . MIN_VALUE ) ;
System . out . println ( "Maximum: Float.MAX_VALUE = " + Float . MAX_VALUE ) ;
System . out . println ( ) ;
// double
System . out . println ( " Basic type: double binary digits: " + Double . SIZE;
System . out . println ( " Wrapper class: java.lang.Double " ) ;
System . out . println ( " Minimum: Double.MIN_VALUE= " + Double . MIN_VALUE )
System . out . println ( "Maximum: Double.MAX_VALUE = " + Double . MAX_VALUE );
System . out . println ( ) ;
// char
System . out . println ( " Basic type: char binary digits: " + Character . SIZ;
System . Out . println ( " Wrapper class: java.lang.Character " ) ;
// Will be numeric instead of character Character.MIN_VALUE
//output to the console
System . out . println ( " Minimum: Character.MIN_VALUE = " + ( int ) Character . MIN_VALUE ) ;
// Output Character.MAX_VALUE to the console as a numeric value
//instead of as a character
System.out.println("Maximum value: Character.MAX_VALUE= "+(int)Character . MAX_VALUE);
}
}
Compile the above code output as follows:
Basic type: byte Number of bits: 8 Wrapping: java . lang . Byte minimum: Byte . MIN_VALUE =- 128 Maximum: Byte . MAX_VALUE = 127 Basic type: short Number of bits: 16 Wrapper class: java . lang . Short Minimum: Short . MIN_VALUE = - 32768 Maximum: Short . MAX_VALUE = 32767 Basic types: int number of bits: 32 packaging: Java . Lang . Integer Min: Integer . MIN_VALUE = - 2147483648 maximum: Integer . MAX_VALUE = 2147483647 Base type: long Number of bits: 64 Wrapper class: java . lang . Long Minimum value: Long . MIN_VALUE =- 9223372036854775808 Maximum value: Long . MAX_VALUE = 9223372036854775807 Basic types: a float number of bits: 32 packaging: Java . Lang . The Float minimum: the Float . MIN_VALUE = 1.4E-45 maximum: the Float . MAX_VALUE = 3.4028235E38 Basic type: double Number of bits: 64 Wrapping: java . lang . Double Minimum: Double . MIN_VALUE = 4.9E-324 Maximum: Double . MAX_VALUE = 1.7976931348623157E308 Base type: char Number of bits: 16 Wrapper class: java . lang . Character minimum: Character . MIN_VALUE = 0 Maximum: Character . MAX_VALUE = 65535
Both Float and Double's minimum and maximum values are output in scientific notation. The ending "E+number" indicates how many times the number before E is multiplied by 10. For example, 3.14E3 is 3.14 × 10 3 = 3140, and 3.14E-3 is 3.14 x 10 -3= 0.00314.
In fact, there is another basic type, void, in Java. It also has a wrapper class java.lang.Void, but we can't directly manipulate them.
Reference type
- In Java, reference-type variables are very similar to C/C++ pointers. The reference type points to an object, and the variable that points to the object is a reference variable. These variables are specified as a specific type at the time of declaration, such as Employee, Puppy, and so on. Once the variable is declared, the type cannot be changed.
- Objects, 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").
Java constants
Constants cannot be modified while the program is running.
Use the final keyword in Java to decorate constants in a manner similar to variables:
final double PI = 3.1415927 ;
Although constant names can also be used in lowercase, constants are usually represented in uppercase letters for ease of identification.
Literals can be assigned to any built-in type of variable. E.g:
byte a = 68;
char a = 'A'
Both byte, int, long, and short can be represented in decimal, hexadecimal, and octal.
When using constants, the prefix 0 represents octal, and the prefix 0x represents hexadecimal, for example:
int decimal = 100 ;
int octal = 0144 ;
int hexa = 0x64 ;
Like other languages, Java's string constants are also sequences of characters that are enclosed in two quotes. The following is an example of a string literal:
"Hello World" "two\nlines" "\"This is in quotes\""
Both string and character constants can contain any Unicode character. E.g:
Char a = '\u0001' ;
String a = "\u0001" ;
The Java language supports some special sequences of escape characters.
symbol | Character meaning |
---|---|
\n | Newline (0x0a) |
\r | Enter (0x0d) |
\f | Page break (0x0c) |
\b | Backspace (0x08) |
\0 | Null characters (0x20) |
\s | String |
\t | Tabs |
\" | Double quotes |
\' | apostrophe |
\\ | Backslash |
\ddd | Octal character (ddd) |
\uxxxx | Hexadecimal Unicode characters (xxxx) |
Automatic type conversion
Integer, real (constant), and character data can be mixed. In operation, different types of data are first converted to the same type, and then operations are performed.
Conversion from low to high.
Low ------------------------------------> High Byte , short , char —> int —> long —> float —> double
Data type conversion must meet the following rules:
- 1. Cannot cast a boolean type.
- 2. Cannot convert object types to objects of unrelated classes.
- 3. Mandatory type conversion must be used when converting a large-capacity type to a small-capacity type.
- 4. Overflow or loss of precision may occur during conversion, for example:
Int i = 128 ; byte b = ( byte ) i ;
Because the byte type is 8 bits and the maximum value is 127, when the int is cast to a byte type, a value of 128 causes an overflow. - 5. The conversion of floating-point numbers to integers is obtained by discarding decimals rather than rounding off, for example:
( int ) 23.7 == 23 ; ( int )- 45.89f == - 45
Automatic type conversion
Must satisfy the data type before conversion is lower than the converted data type, for example: The short data type has 16 bits, it can automatically convert the int type with 32 bits, and the same number of bits in the float data type. For 32, it can be automatically converted to a 64-bit double type.
Examples
Public class ZiDongLeiZhuan { public static void main ( String [ ] args ) { char c1 = ' a ' ; // Define a char type
Int i1 = c1 ; // char is automatically converted to int
System . Out . println ( " The value of char type converted to int is equal to " + i1 ) ;
char c2 = ' A ' ; // Define a char type
Int i2 = c2 + 1 ; // char type and int type calculation
The System . OUT . The println ( " a value obtained by calculation is equal to a char and int " + I2 ) ;
} }
The result of the operation is:
The value of char type converted to int is equal to 97 char type and the value of int is equal to 66
Analysis: The value of c1 is the character 'a'. Checking the ascii code table shows that the corresponding int type value is 97, and the value of 'A' is 65, so i2=65+1=66.
Mandatory type conversion
- 1. The condition is that the converted data type must be compatible.
- 2. Format: (type) value type is the type of data type to be cast:
Examples
public class QiangZhiZhuanHuan { public static void main(String[] args) { int i1 = 123; byte b = (byte) i1; // cast to byte System.out.println(" Int casts a byte to a value equal to " + b); } }operation result:Int cast to byte after the value is equal to 123
Implicit cast
- The default type of an integer is int.
- 2. This is not the case for floating-point type, because you must follow the number with F or f after defining the float type.
This section explains Java's basic data types. The next section discusses the different types of variables and their usage.
Comments
Post a Comment