Java Chapter 2 Basic Syntax

Chapter 2 Basic Grammar

HERE

IDE integrated development environment is an application program used to provide a program development environment, generally including tools such as a code editor, a compiler, a debugger, and a graphical user interface.

Commonly used Java development tools are:

IntelliJ IDEA is charged by a Czech company

Eclipse developed by IBM is now donated to the eclipse foundation

Comment

1. Description of program function

2. Mark with a certain symbol

3. The program will not execute comments during operation

There are three annotation methods in Java language

  1. ​ //... Single line comment

  2. ​ /*… */ Multi-line comments

  3. ​ /**

    ​ *

    ​ */ Document comments are used to explain the functions of classes, methods, and attributes, and can prompt their function introduction when calling

Keyword

Definition: A special meaning is given by the Java language to modify, for example, classes, attributes, and methods.

Features: All letters in keywords must be lowercase.

Reserved word

The current Java version has not been used, but future versions may be used as keywords. It is reserved and cannot be used in naming.

For example: goto const

Identifier

The character sequence used when naming variables, classes, methods, etc. in Java.

In layman's terms: a name that you can name. (See the name and meaning)

1. It is composed of 26 uppercase and lowercase letters, 0-9, _ or $

2. Numbers cannot start

3. Keywords and reserved words are not allowed, but keywords and reserved words can be included.

4. Java is strictly case sensitive, and the length is unlimited.

5. The identifier cannot contain spaces.

variable

The basic storage unit in the program, the value can be changed during operation.

Essentially, a variable is an area in memory, accessed through the variable name.

Variables must be assigned values ​​before they can be used.

Its elements include variable name and variable type.

Variable classification:

​ From the data type perspective:

​ Basic type variable, reference type variable

​ From position:

​ Member variables/class attributes (defined in the class)

​ Local variables (defined in the method)

Java data types

Java language is a strong data type language, each data must specify the type, and guide JVM to allocate memory space.

type of data:

1. Basic types: only 8 types, easy to use.

2. Reference types: Except for the 8 basic types, the rest are reference types. Classes are used as types, and arrays are reference types.

Java basic data types

Integer type

byte 1 byte (1byte (1 byte) == 8bit)

short 2 bytes

int 4 bytes

long 8 bytes

Floating point type

Float 4 bytes due to different calculation methods in computers, 4 byte floating point is larger than 4 bytes, 8 byte integer.

The default value of floating-point number is double type. To declare a float value, you need to add f/F float f = 10.5f;

Decimals in computers are stored as approximate values.

Boolean logic type

Logical values ​​can only use true/false

char character

utf-8 is a variable length encoding table

Used to represent a "character" in the usual sense. A character is a single character enclosed in single quotes.

char 2 bytes (can participate in operations, use the ASCII code corresponding to the character)

The bottom of the computer is how the binary computer represents characters

Code table artificially defined

Characters have a corresponding decimal number in different encoding tables, and finally use this number to store on the computer.

The earliest encoding table ASCII can only represent English.

Different countries define their own country codes. For example: China GBK GB2312

Later, the unicode encoding table was born, which contained characters from various countries around the world.

Basic data type conversion

In addition to the Boolean type among the 8 basic types, all the other 7 types can be converted.

​ Default conversion

​ Types with small capacity are converted to data types with large capacity by default.

​ Large capacity can include small capacity.

​ When participating in mixed operations, small capacity will automatically be converted to large capacity.

​ Force conversion

​ Large capacity conversion capacity small.

​ long x = 10000L;

​ int y = (int)x; force x to be converted to int

(There will be problems, 1. Overflow 2. Reduced accuracy)

Operator

Arithmetic operators: +, -, *, /, %, ++, –

String concatenation: +

Comparison operator

==,!= Comparison between basic types of values

(>, <, >=, <=) Value and Value

Logical Operators

& Logical AND | Logical OR! Logical negation

&& short-circuit and || short-circuit OR^ logical exclusive OR

& | ^ The left and right operands are logical values ​​(true/false), otherwise they are bit operations

& false decision

| true decision

Assignment operator

=

Note: = The type on both sides matches the value

Basic data type conversion: default conversion, forced type conversion

+=,-=,*=,/=,%=

short s = 3; 3 is int, but the virtual machine supports assigning int type 3 to short, byte

s = s+2; x (error report)

s+ = 2; √ (implicit conversion)

= Assignment == equal to

Conditional operator

(Conditional expression)? Expression 1: Expression 2

If the result of the conditional expression is true, execute expression 1, otherwise execute expression 2

Bit operation

In daily development, bit operations are not commonly used, but clever use of bit operations can greatly reduce operating costs and optimize algorithms.

<< Left-shift and empty bits to fill 0, the shifted high bits are discarded, and the vacant bits fill 0.

(>>) move right

(>>>) Unsigned right shift does not need to care about the sign on the high bit (if the sign bit is affected, just add 0 to the sign bit)

(Move left and right in binary)

& Binary bit is used for & operation, when only 1&1, the result is 1, otherwise it is 0.

| Only 0|0 is 0, otherwise it is 1.

^ 1^1 =0,0^0=0

​ 1^0 = 1, 0^1=1
Invert positive numbers, invert each binary code according to the complement of each bit

​ Negative numbers are inverted, each binary code is inverted according to the complement of each bit

Guess you like

Origin blog.csdn.net/weixin_45636230/article/details/109039388