Introduction of today's content
1. Variables
2. Operators
Variables
1.1. Overview of variables
we have learned the constants before, and then we will learn the variables. In Java, the application of variables is much more than the application of constants. So variables are especially important points of knowledge!
What is a variable? A variable is an in-memory small box (small container), what is a container? Life also has a lot of containers, such as water cups are containers, used to load water; Your home's wardrobe is a container for clothes, and a lunch box is a container for loading meals. So what does a variable load? The answer is data!
Conclusion: A variable is a small box that loads data in memory, and you can only use it to store data and fetch data.
1.2 Computer storage Unit
The
variable is a small container in memory that is used to store data. So how does computer memory store data? The "bit ( bit ) " We also call it " bit ", usually in lowercase letters b byte b 8 bits of the composition.
When the program needs to use storage space, the minimum operating system is assigned to the program 1 1 bits. You might say that if the program only needs 1 bits of space, system allocation cannot be assigned only 1 bits? The answer is NO! It's like you just need 1 cigarettes, you go to the store to buy cigarettes, the smallest unit of the store allocation is 1 box ( 20 1 cigarette.
you might think that1 bytes (8 bits) can store very large values,1 bits max is 9 then 8 The maximum bit value is 99999999. You're wrong, because computers are stored in two, not in the usual decimal in our lives. So the maximum data stored in 1 bytes is the binary number of the 11111111 .
In addition to the bytes there are some commonly used storage units, we may be more familiar with, we look together:
1B (bytes) = 8bit
1KB = 1024B
1MB = 1024KB
1GB = 1024MB
1TB = 1024GB
1PB = 1024TB
1.3
Basic Types of 4 classes 8 kinds
A wardrobe cannot be used to load water, nor can a cup be used to load a garment. This means that different containers load different items. Variables are also created by specifying the data type of the variable, such as integer variables, floating-point variables, and so on.
Conclusion: A variable must have a definite type and what type of variable to load what type of data.
Water cups are used to fill water, so how much water can the cup fill? a ton? We know that the cup was created not only to determine the water (data type) to be loaded, but also to determine how much water can be loaded (specific types of data). Variables are also required to specify what type of data the variable can load, as well as how much data the variable can load.
There are 4 basic types in Java , the 4 classes are expanded after a total of 8 Basic types. we use this in future programming 8 basic types rather than 4 classes, these 8 Basic types specify a range.
1.4 Constants and Types
we said earlier that the number is an integer constant, but it is a byte,short,int, What kind of a long? Let's talk about this constant type problem.
integer constants can determine types based on their scope, such as100in the-128~127between, so he wasbytetype; -in the-32768~32767between, so it was Shorttype;100000in the-2147483648~2147483648between, so it wasinttype. the default integer type in Java is type int
you might think12345678901in the-263~263-1between, so it wasLongtype. Notice, this is wrong!!! In theJavaIf the integer constant is not-2147483648~2147483648must be added between theL"suffix (lowercase can also, but is recommended to use uppercase), in-2147483648~2147483648can also be added between the "L"suffix. Which means12345678901not in-2147483648~2147483648between, so it's inJavais the wrong constant, which you must write:12345678901L, this is the correct constant. So added the "LThe integer constant of the suffix isLongtype of, for example:100L,12345678901L's AllLonga constant of type.
constants for floating-point types can also use suffixes, All decimal places in Java that do not have suffixes and use the "D" suffix (lowercase, but are recommended to use uppercase) are double types; type constants must be added " F "suffix (lowercase can also be used, but uppercase is recommended ) the default floating-point type in Java is double type
3.14 has no suffix, so it is a double type;
5.28D is a double type;
1.26F is a float type.
1.5
defining variables (creating variables)
To define the syntax format for a variable:
Data type variable name = data value;int a = + ;
where int is a data type that specifies that the variable can only store integers and that the storage range is -2147483648~2147483648 .
where A is the variable name, the variable name is an identifier, which means that as long as a valid identifier can be used to make the variable name. Variable names can be used in the program to manipulate variables (small boxes in memory).
where "=100" is to assign a value to a variable, that is, to write to the a variables (the variable is a small box, now the small box is saved - ). Note that the value assigned to the variable must conform to the type, that is, the int type can only store integers, and must be at -2147483648~ An integer within the range of 2147483648. The Two conditions are satisfied, so it is correct.
Practice:
Variabe.java
/*variable definition Format: Data type variable name = variable value;*/ Public classVariable { Public Static voidMain (string[] args) {intA =Ten; Doubleb =3.14; Charc ='Z'; String s="I love Java"; A= -; System. out. println (a); }}1.6
Considerations for using Variables
When we use variables, we need to be aware that to satisfy the use rules of variables to use, we look at all the considerations.
Notes on use of variable L variables
The L variable can be assigned without assigning a value after it is defined. No assignment can be used.
Public Static void Main (string[] args) {int/// x assigns a value ofSystem. out. println (x); // read the value in the X variable, and then print }
L variables have scope restrictions when used.
Public Static void Main (string[] args) {int; { int - ;} System. out. println (x); // read the value in the X variable, and then print System. out. println (y); // failed to read the value in the Y variable, reason for failure, cannot find Y variable, because the y variable is outside the scope, so you cannot use the Y variable }
L variables cannot be defined repeatedly.
Public Static void Main (string[] args) { int; Double 5.5; // compilation failed, variable duplicate definition }
1.7
Data type Conversions
Can different types of variables work together? The answer is yes, but first the type conversion and then the operation. Let's take a look at the type conversion
In fact, the data that we are learning, its representation can be flexible, such as the operation of converting decimals into integers
During the conversion process, the data follows a principle:
A small range of data type values (such as byte) that can be converted directly to a large range of data type values (such as int);
A range of data type values (such as int) that cannot be converted directly to a data type value of a small range (such as byte)
So, do you remember how each type represents the range of data? Forget it does not matter, I tell you, the various data types according to the data range from small to large list:
byte Short int Long float Double
There are two ways to convert data types, so let's take a look at:
L Automatic type conversion
A data type that represents a small range is converted to a large range of data types, which is called automatic type conversion
Automatic type conversion format:
data types with large range variable = data type value with a small range;
Such as:
Double + ; or int; double d2 = i;
l Coercion of type conversions
A data type that represents a large range is converted to a small range of data types, which is called coercion type conversion
Forced type conversion Format:
data types with a small range variable = ( the data type with a small range ) has a large range of data type values ;
Such as:
int i = (int)6.718; // The value of I is 6 or Double 3.14 ; int I2 = (int) D; // the value of I2 is 3
Java basic Syntax (chapter I variables)