Introduction to basic Java variables and introduction to basic java Variables
Variable description and usage
Concept:
A variable is a storage area in the memory. It must have its own name (variable name), type (data type ), data in this region can change values continuously within the same data type;
Notes for using variables:
Variables in Java must be declared before they can be used;
Variable scope: Valid range in a pair;
Variables must be initialized before they can be used;
Variable definition:
Data type variable name = initialization value;
Variables are used to access the stored values in the memory area pointed to by the variable name.
Data Type:
Basic data type (from large to small ):
Numeric type:
Integer: long (-263 ~ 263-1), int (-231 ~ 231-1), short (-215 ~ 215-1), byte (-128 ~ 127)
Long naming rules: long (data type) myLong = 100L; L or l must be added after the value
Float: double (double), float (single precision)
Float naming rules: float myFloat = 100.2F; add F or f to the value
Character Type: char can only point to one character (English, Chinese, punctuation marks, Japanese and other language and escape characters), such as: a but AB multi-character is not;
Boolean: boolean
Reference data types: class, interface, and array
The class has a very common type: String is a String type. It is a defined class in Java and can be directly used.
Calculation between variables (boolean type not considered ):
Automatic type conversion: when the data type with a small capacity and the data type with a large capacity are computed, the data type with a small capacity is automatically converted to the data type with a large capacity.
From small to large: char, byte, short --> int --> long --> float --> double
When the char and numeric types are computed, the char type is converted to the corresponding int type number according to the ASCII code table;
Byte, short, and char are automatically converted to int type data by default;
The sample code is as follows:
1 class TestVeriable {2 public static void main (String args []) {3 int i1 = 10; 4 short s1 = 2; 5 int i2 = i1 + s1; 6 7 float f1 = 12.5F; 8 float f2 = f1 + i2; 9 10 long l = 12L; 11 float f3 = l; 12 13 char c1 = 'a '; 14 char c2 = 'a'; 15 int i3 = c1 + 1; 16 int i4 = c2 + 1; 17 18 // The calculation results between short, byte, and char are automatically converted to the int type 19 short ss1 = 12; 20 byte bb1 = 1; 21 char cc0 = 'a '; 22 int ii1 = ss1 + bb1 + PC3; 23 24} 25}
Forced type conversion: to convert a data type with a large capacity to a data type with a small capacity, use the forced type conversion Symbol :()
Example: long l1 = 100L;
Int i1 = (int) l1;
Precautions for using forced type conversion: Data Precision loss
For example, byte b1 = (byte) l1;
The operation between the string and the basic data type: only the join operation can be performed between the two data types. When the two data types are spliced together, the result is still a string type data;
The sample code is as follows:
1 class TestVeriable1{2 public static void main(String args[]){3 String str1 = "abc";4 int i1 = 123;5 String str2 = str1+i1;6 System.out.println(str2);7 }8 }