1, Definition of variables
The memory space used by a computer to store variable data.
/** * Syntax for declaring variables: * Variable data type variable name 1 [= variable value 1 [, variable name 2 [= variable value 2]...]] */
eg: int b1 = 88; System.out.println("b1");// Output string: B1 System.out.println(b1);// Output: 88
① Variable naming rules
It starts with letters, underscores and $. It can contain numbers, but can not contain special symbols, nor can it use Java keywords
② Variable naming convention
Hump nomenclature: the first letter is lowercase. If there are multiple words, the first letter is capitalized from the second word. For example: startGame
When using variables, pay attention to:
① Assignment cannot exceed the range that the data type can represent;
② If you define long type data, you need to add L or l after it, and f or F for float type.
2, Data type of variable
(1) Basic data types (8)
integer | Occupied space | Value range |
---|---|---|
byte | 8 bits (1 byte) | -2^7 ~ 2^7-1 |
short | 16 bits (1 byte) | 2^15 ~ 2^15-1 |
Int (default) | 32 bits (1 byte) | 2^31 ~ 2^31-1 |
long | 64 bit (1 byte) | 2^63 ~ 2^63-1 |
float | Occupied space | explain |
float | 32 bits (1 byte) | Single precision decimal (not commonly used) |
double (default) | 64 bit (1 byte) | Double precision decimal (common) |
③ Character type: char 16 bits (2 bytes) 0-65535
A printable single character enclosed in single quotation marks: 'a', 'B', 'I', etc
④ boolean type: boolean There are only two values true and false
A printable single character enclosed in single quotation marks: 'a', 'B', 'I', etc
Note: the maximum value and digits of the basic data type are calculated with the reference data type.
(2) Reference data types (2)
jdk encapsulated classes and custom java classes in java belong to reference data types.
Each basic data type corresponds to a reference data type (called the wrapper class of the basic data type).
Basic data type | Reference data type |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Note: these reference data types encapsulate methods and data that operate on basic data types. For example, the maximum, minimum and number of bits of a basic data type can be obtained by using the corresponding reference data type.
/** * Find the latest value and number of digits of basic data type */ public class Demo02 { public static void main(String[] args) { //Find the maximum value and number of bits of basic data types byte, short, int, long and char System.out.println("byte Maximum value of:"+Byte.MAX_VALUE); System.out.println("byte Minimum value of:"+Byte.MIN_VALUE); System.out.println("byte Number of digits:"+Byte.SIZE); //short, int and long are similar to byte System.out.println("char Maximum value of:"+(int)Character.MAX_VALUE); System.out.println("char Minimum value of:"+(int)Character.MIN_VALUE); System.out.println("char Number of digits:"+Character.SIZE); } }
Operation results:
3, Type conversion of variables
(1) Automatic type conversion (implicit type conversion) Small - > Large
byte b = 125 ;
int i = b ; // B of byte type is automatically converted to int type
(2) Cast (explicit type cast) Large - > small Use with caution
int i = 125 ;
byte b = (byte) i ; // An I of type int is forcibly converted to a byte type
Classic interview questions:
/** * Interview questions */ public class Demo01 { public static void main(String[] args) { // Cast type int i1 = 130; byte b1 =(byte) i1; System.out.println(b1); int i2 = -130; byte b2 =(byte) i2; System.out.println(b2); short s = 1; //For byte, short and char types, when an operation occurs, the operation result will be implicitly converted to int type // short s1 = s + s;// report errors //Can be changed to // short s1 = (short) (s+s); //Or change to int s1 = s +s; //S + = 1 < = = > equivalent to s = (type of s) (s+1); s += 1;//Equivalent to s = (short)(s+s); System.out.println(s); } }
Operation results:
Another example is:
int a = 500; byte b = (byte) a; System.out.println(b);
The answer is: - 12