Java Basic Grammar Niu Ke

navigation

DOS commands

start redis

redis-server.exe redis.windows.conf

Introduction to Java

  1. Java is dedicated to checking programs for errors at compile and run time.
  2. The Java virtual machine implements a cross-platform interface
  3. Type checking helps catch many mistakes that occur early in development.
  4. Java's own manipulation of memory reduces the possibility of memory errors.
  5. Java also implements true arrays, avoiding the possibility of overwriting data.
  6. Note that it is possible to avoid data overwriting, not the type of data overwriting

Memory

  1. The jvm heap is divided into: the new generation (generally one Eden area and two Survivor areas), and the old generation (old area).
  2. The constant pool belongs to PermGen (method area)

![](https://img-blog.csdnimg.cn/img_convert/68735d1f92bdd918d67bc1000ba68d7a.png#clientId=u7db6f9ff-c2c2-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u6b9cfc0d&margin=[object Object]&originHeight=546&originWidth=1108&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=uef9fd9ec-e0ad-47cc-97bc-086909e6b74&title=)

GC recovery mechanism

The advantage of the GC mechanism is that the system will automatically process the objects that are no longer referenced, and clean up when the CPU is idle or the storage space is insufficient, so that there is no need to figure out the memory management mechanism when programming, and programmers do not need to manage memory, which improves the efficiency of programming .

keywords

identifier rules

Rules [1] Java identifiers can only be composed of numbers, letters, underscore "_" or " " symbols and U Unicode character sets [2] Java identifiers must be composed of letters, underscores " " or " " symbols and Unicode character sets Composition [2] Java identifiers must start with letters, underscores "_" or "" Symbols and Unicode character sets [ 2 ] Java identifiers must start with letters and underscores "or " " symbol and the beginning of the Unicode character set
[3] Java identifiers cannot be Java keywords, reserved words (const, goto) and literal values ​​(true, false, null)
[4] Java identifiers are case-sensitive, yes case sensitive

  1. The elements of an identifier are letters (az, AZ), numbers (0~9), underscores (_) and dollar signs ($).
  2. Identifiers cannot start with a number.
  3. Java identifiers are strictly case-sensitive.
  4. Identifiers can be of any length.
  5. Keywords and null, true, false cannot be used for custom identifiers

final

final

  1. The final keyword can be used for member variables, local variables, methods, and classes.
  2. Final member variables must be initialized when declared or initialized in the constructor, otherwise a compilation error will be reported.
  3. You cannot reassign a final variable.
  4. Local variables must be assigned a value when declared.
  5. All variables in an anonymous class must be final variables.
  6. final methods cannot be overridden.
  7. final classes cannot be inherited.
  8. Final variables that are not initialized at the time of declaration are called blank final variables (blank final variable), and they must be initialized in the constructor, or initialized by calling this(). If you don't do this, the compiler will report an error "final variable (variable name) needs to be initialized".
  9. StringBuilder, StringBuffer, and String are all final.

volatile

  • order reordering is disabled
  • Guarantees the visibility of different threads operating on this variable, that is, a thread modifies a variable value, and this new value is immediately visible to other threads
  • Atomicity is not guaranteed (thread-unsafe)

note

Identifier Naming Rules

Literals and constants

Conversion

basic type

Eight basic data types

byte,char,short,int,long,float,double,boolean

int

Rule ① In any case, Integer will not be equal to new Integer. Will not go through the unboxing process.
② Both are non-new Integers. If the number is between -128 and 127, it is true, otherwise it is false.
When java compiles Integer i2 = 128, it is translated into -> Integer i2 = Integer.valueOf(128); and the valueOf() function will cache the number between -128 and 127
③ Both are new, both are false
④ The ratio between int and integer (whether new or not) is true, because Integer will be automatically unboxed to int and then compared

type conversion

rule

Rule 1. If the data types participating in the operation are different, first convert to the same type, and then perform the operation.
2. The conversion is carried out in the direction of increasing data length to ensure that the accuracy does not decrease. For example, in the operation of int type and long type, first convert the int value into long type before performing the operation.
3. All floating-point calculations are performed with double precision. Even if the expression contains only float single-precision calculations, it must be converted to double type before calculation.
4. When char type and short type participate in the operation, they must be converted into int type first.
5. In the assignment operation, when the data types on both sides of the assignment number are different, it is necessary to convert the type of the expression on the right to the type of the variable on the left. If the data type length of the expression on the right is longer than that on the left, part of the data will be lost, which will reduce the precision.
Java expression transformation rules convert from low to high:

  1. All byte, short, and char values ​​will be promoted to int;
  2. If one of the operands is long type, the calculation result is long type;
  3. If one of the operands is float type, the calculation result is float type;
  4. If one of the operands is double type, the calculation result is double type;
  5. The variable modified by fianl will not automatically change the type. When two final modifications are operated together, the result will be converted according to the type of the variable on the left.

String

  1. ==, if applied to a variable of a basic data type, directly compare whether the stored "value" is equal; if applied to a variable of a reference type, compare the address of the pointed object
  2. The String class rewrites the equals method to compare whether the strings stored in the pointed-to string object are equal.
  3. equals cannot be applied to variables of primitive data types.
  4. The addition of any character to a string is a string, but there is an order. The characters in front of the string are added in the original format, and the characters after the string are added in accordance with the string.

variable

  1. ++y is added first and then run, y++ is calculated first and then added.
  2. According to scoping rules, variables defined in functions can only be referenced in functions
  3. Variables defined in a function live for the entire execution of the program

Operators and Expressions

Excellent class of operators

Arithmetic and Logical Operators

The operator ">>" performs an arithmetic right shift, which uses the most significant bit to fill the vacant position on the left after the shift.
The result of the right shift is: for each bit shift, the first operand is divided by 2 once, and the number of shifts is determined by the second operand.
The logical right shift or unsigned right shift operator ">>>" only operates on bits and has no arithmetic meaning. It fills the empty bits on the left with 0.
Arithmetic right shift does not change the sign of the original number, while logical right shift does not guarantee this.
The shift operator reduces the operand on the right side. When the left operand is of type int, the right side is modulo 32; when the left side is long type, the right side is modulo 64.

control statement

switch

  1. Before Java7, switch could only support byte, short, char, int or their corresponding encapsulation classes and Enum types. In Java7, the long-awaited String support has finally been added.
  2. In the switch statement, the value of the expression cannot be null, otherwise a NullPointerException will be thrown at runtime. You cannot use null in the case clause, otherwise a compilation error will occur.

loop statement

method

Java memory partition

array

  1. When defining an array, numbers cannot appear on the left of the equal sign, that is, no matter what size the array is, it cannot appear on the left. The first one of
    the array is why the left side is not marked with size, but the right side needs to be marked with size? First, an object is arrayed, which does not belong to any class, and is created and inherited by the jvm in the heap during runtime, and the length attribute is added at the same time. Since the memory occupied by the array object is on the heap, you should clearly tell the jvm its own size when declaring it to facilitate allocation, and because the reference of the array object is in the stack, there is no need to mark the size on the left side when declaring, so it is written as The two brackets are to indicate that this reference points to a two-dimensional array in the heap. The second is why the array on the right can only declare a few rows, without declaring the size of no row? Probably jvm will allocate corresponding expandable space according to the number of rows during operation, so as to facilitate the expansion of each row. In fact, it can be understood in the same way as the C language. The row is actually a kind of reference, and the address at the beginning of the row represents a one-dimensional array. Welcome to supplement the second question.


Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/125784904