Dabai became the fourth day of Java software siege lion (literal value, variable, data type basis, coding method)

Java language foundation

1. Literal value
About literal value

— 10/100
— 3.14
—“abc”
—‘a’
—true、false

The literal value is the data.
The literal value is one of the components of the Java source program. Including the identifier and its keywords, they are all part of the Java source program.

Data is divided into categories in the real world, so data is also typed in computer programming languages: [data type]

10. 100 belongs to integer type
3.14 belongs to floating point type
true, false belongs to boolean type
"abc", "Zhang Guangrong" belongs to string type'A
','人' belongs to character type

Note:
All string literals in the Java language must be enclosed in double quotes, and double quotes are half-width.
All character literals in the Java language must be enclosed in single quotes, and single quotes are half-width.

2. Variable

1 What is a variable?

A variable is essentially a space in memory, this space "has a data type," "a name," and "a literal value."
Variables consist of three parts: data type, name, literal value [Data]
Variables are the most basic unit for storing data in memory.

2 The role of data types?

Different data types will allocate different sizes of space at the bottom.
The data type is to guide how much memory space should be allocated during the running phase of the program.

3 Variable requirements: The specific "data" stored in the variable must be consistent with the "data type" of the variable. When inconsistent, the compiler will report an error

4 The syntax format for declaring/defining variables:

Data type variable name
Data type: The concept is in the third part.
eg: int integer
variable name: as long as it is a legal identifier. The specification requires that the first letter is lowercase, and the first letter of each subsequent word is uppercase.

5 How to assign value after variable declaration?

Syntax format: variable name=literal value
Requirement: the data type of the literal value must be consistent with the data type of the variable.
= The equal sign is an operator, called an assignment operator. The assignment operator operates the expression on the right side of the equal sign first, and the result after the expression is executed is assigned to the variable on the left side of the equation.

6 Declaration and assignment can be done together.

int i=10

7 After the variable is assigned, it can be re-assigned, and the value of the variable can be changed:

int i=10;
System.out.println(i);
i=20;
System.out.println(i);
i=100;
System.out.println(i);

8 With the concept of variables, the memory space has been reused:

int i=10;
System.out.println(i);
System.out.println(i);
System.out.println(i);

9 Usually access to a variable includes two types of access:

The first type: read the specific data saved in the variable get/get the
second type: modify the specific data saved in the variable set/set
i=10; //set
System.out.println(i); //get

10 Variables can be declared multiple in one line

11 Variables in Java must be declared and then assigned before they can be accessed.

int i; //The program is executed here, the memory space has not been opened up, and the variable i has not been initialized. Therefore, it cannot be accessed before the assignment.

12 About the scope of Java variables

What is scope?
The scope of a variable actually describes the effective scope of the variable. In what range can be accessed, as long as the variable is out of this range, it cannot be accessed.

The scope of a variable only needs to remember one sentence: you won't recognize it after the braces.

13 About the classification of variables:

Classification on the location of variable declarations:
local variables: variables declared in the method body.
Member variables: The variables declared in the method body [within the class body] are called member variables.
In different scopes, variable names can be the same.
In the same scope, variable names cannot be the same.

3. Data type

1 What is the role of data types?

There are a lot of data in the program, and each data has a related type, and different data types occupy different amounts of space.
The function of the data type is to instruct the JVM to allocate space for the data when running the program.

2 Data types in Java include two

Basic data type
Reference data type

3 About basic data types

The basic data types include four types and eight types: the
first type: integer type (byte, short, int, long) the
second type: floating point (float, double) the
third type: boolean (boolean) the
fourth type : Character type (char)

4 The character string "abc" does not belong to the basic data type, but belongs to the "reference data type" , and the character belongs to the basic data type:

String use double quotation mark ""
character use single quotation mark ''

5 What is the space occupied by each of the eight basic data types?

Basic data type Occupied space size [unit: byte] Ranges
byte 1 -128~127
short 2 ·32768~32767
int 3 -2147483648~2147483647
long 8 ·263~263-1
float 4 Effective digits 6~7
double 8 15 effective bits
boolean 1 true,false
char 2 0~65535

6 The computer can only recognize binary in any case. For example: only know 010101010100101...

Text and binary are compared and converted by character encoding, the first to appear is ASCII code [using one byte encoding]

The encoding method that supports simplified Chinese is GB2312< GBK< GB18030
supports traditional Chinese: <big5>
Later, a way to unify all characters in the world with a larger capacity, this encoding method is called: Unicode encoding
unicode encoding methods have multiple specific The realization:

  • UTF-8
  • UTF-16
  • UTF-32

The encoding method adopted by the Java language is unicode encoding, so the "identifier" can be in Chinese.

Guess you like

Origin blog.csdn.net/qq2632246528/article/details/112353424