Java Study Notes (Day 2)

The Java language is an object-oriented programming language. The basic unit of a Java program is a class, and the class body contains two parts: attributes and methods. Every Java native application must contain a main method, and the class containing the main method is called the main class.

A Java application is composed of several classes. The Java class must be imported before it can be used by the current class. In the Java language, the corresponding class can be imported through the import keyword.

The properties of the class are usually called the global variables (or member variables) of the class, and the properties in the method are called local variables. Global variables are declared in the class body, and local variables are declared in the method body.

The main() method is the main method in the class body. The method starts with "{" and ends with "}", with the method body in the middle. public, static, and void are respectively the permission modifier, static modifier and return value type declaration of the main method. The main() method in a java program must be declared as public static void. String arg[] is an array of string type, which is the parameter of the main() method, and the main() method is the location where the program starts executing.

The main() method is the program entry method. It has a fixed grid, and the method parameter is also fixed as an array of strings.

The Java language provides three methods for adding comments, namely single-line comments ("//"), multi-line comments ("/* */") and documentation comments ("/** */").

JavaDoc Documentation Comments: "/**" and "*/" are the start and end tags of documentation comments. In the Java source file editor, select a member method or member variable, and then press Shift+Alt+J keys, Eclipse will automatically add the JavaDoc document comment structure. If a method is selected, parameter names are also added automatically.

The Java language uses the Unicode standard character set, which can identify up to 65535 characters.

Keywords in Java:

int public this finally boolean abstract
continue float long short throw throws
return break for static new interface
if goto default byte do case
strictfp package supper void try switch
else catch implements private final class
extends volatile while synchronized instanceof char
protecte importd transient implements there fault double

When declaring a variable, you can either not assign it or assign it directly to the initial value.

The memory of the system is roughly divided into three areas: the system area (OS), the program area (Program) and the data area (Data). When the program is executed, the program code is loaded into the program area in the memory, and the data is temporarily stored in the data area. In the Java language, it is allowed to use Chinese characters or other language characters as variable names.

Java uses the keyword final to declare constants, and constants can only be initialized once after the declaration. Constant names usually use uppercase letters and use the "_" underscore character to separate multiple words.

After a global constant (or member constant) is declared, it must be initialized immediately, or in all constructors of the class, otherwise the compiler will report an error.

Global variables (also known as member variables) are defined in the class body, and their effective scope is the code segment of the entire class. Local variables are variables defined in the method body and are only valid in the current code block (that is, inside curly braces).

There are 8 basic data types in Java used to store numeric, character and boolean values.

    Numeric types: integer types (byte, short, int, long) and floating point types (float, double)

    Character type: char

    Boolean: boolean

Decimal is the default number system of the Java language, and you cannot start a decimal number with 0 (except 0). The representation of octal is distinguished by the highest digit 0, and octal must start with 0. Hex must start with 0X or 0x.

Integer data type
type of data Storage bits Ranges
byte 8th place -128 to 127
short 16th place -32768 to 32767
int 32nd place -2147483648 to 2147483647
long 64th place -9223372036854775808 to 9223372036854775807

Java's default integer type is int. If you want to mark an integer as a long type, you must add the letter "L" at the end of the number.

floating point data type
type of data Memory space (8 bits equals 1 byte) Ranges
float 32nd place 1.4E-45 to 3.4028235E38
double 64th place 4.9E-324 to 1.7976931348623157E308

The Java language uses double-precision floating-point numbers by default. If you assign a real number to a variable of float type, you need to add an "F" letter to the end of the real number as a suffix

The character type (char) is used to store a single character, occupying 16 bits (two bytes) of memory space. Character types are represented by single quotes.

When data is converted from a type that occupies a small storage space to a data type that occupies a large storage space, no explicit type conversion (ie, automatic type conversion) is required, otherwise, a forced type conversion must be performed.

Variable assignment: When the data type assigned to a variable is inconsistent with the variable type, and the level of the assigned data type is lower than the level of the variable type, automatic data type conversion will automatically convert the assigned data to the type of the variable.

Method invocation: Pass a value to the method parameter, and the data type of this value is lower than the data type of the parameter variable of the method.

When a high-level data type is forcibly converted to a low-level data type, if the value is out of the range of the low-level data type, the value will be intercepted, resulting in data loss and incompleteness.

boolean boolean values ​​cannot be cast to other data types and vice versa.

Symbols that represent different operations are called operators, and the data on which the operations are performed are called operators. Operators and operators form a meaningful sequence of symbols in a certain grammatical form to become an expression.

Since the result of the right-hand expression is obtained when processing the assignment operator "=", if there are more than two "=" operators in an expression, the processing will start from the rightmost "=". .

The increment and decrement operators are unary arithmetic operators that can be used as prefixes or suffixes. Its role is to increase or decrease the value of a variable by 1. The auto-increment and auto-decrement operators placed in front of the variable, such as "++x", will first increase the value of the variable x by 1, and then refer to the value of the variable to participate in the operation. The auto-increment and auto-decrement operators placed after the variable, such as "x++", will first refer to the value of the variable to participate in the operation of the expression, and then add 1 to the variable.

Comparison operators, also known as relational operators, are binary operators whose operation results are of type boolean: >, <, ==, >=, <=, !=

Java logical operators include "&&" logical AND, "||" logical OR and "!" logical NOT.

Bitwise operators are used to deal with integer or character operators. Bit operations are operations that operate entirely on the binary bit (bit) unit. ~ (not, bitwise inversion), & (and, bitwise and), | (or, bitwise or), ^ (exclusive or, bitwise exclusive or), << (left shift), >> ( right shift), >>> (unsigned right shift). Among the bitwise operators in Java, the ~ NOT operator is a unary operator, and the others are binary operators.

Left shift is to shift the binary data of the left operand in the memory to the left by the number of bits specified by the right operand, and the vacated part of the left shift is filled with 0, and the right shift is more complicated. If the highest bit is 0, the left shift is vacated The part is filled with 0. If the highest bit is 1, the part vacated by the right shift is filled with 1. Java also provides an unsigned right shift operator (>>>), regardless of whether the highest bit is 0 or 1, the bits vacated by the left shift are filled with 0.

The data types used by the shift operators are byte, short, char, int, and long

Shifting can achieve the effect of dividing or multiplying an integer by the nth power of 2. Shifting a number to the left by n places means multiplying the number by 2 to the nth power, and shifting a number to the right by n places means dividing the number by 2 to the power of n .

Ternary operator: conditional? Value 1: Value 2. If the conditional expression evaluates to true, the entire expression takes the value 1, otherwise it takes the value 2

operator precedence
priority describe operator
high brackets ()
| plus or minus sign + -
| unary operator ++ -- !
| multiply and divide * / %
| Addition and subtraction + -
| shift operation << >> >>>
| Comparison of size > < >= <=
| compare for equality == !=
| bitwise AND &
| Bitwise XOR ^
| bitwise OR |
| logical AND operation &&
| logical or operation ||
Ternary operator ?:
Low assignment operator = += -= *= /+ %=

Coding specification: Each statement occupies a separate line, and a command must end with a semicolon. When declaring variables, try to keep the declaration of each variable on a separate line, and for local variables, initialize them at the same time as the declaration. Class and interface names require not only meaningful words but also all words with the first letter capitalized. All letters of the constant are all uppercase, and multiple words are connected by "_" underscore.

A byte consists of 8 bits (bit), the bit is the smallest unit, the bits in the byte are sorted from low to high, and the index subscript is bit 0 to bit 7.

if条件判断语句中,如果省略条件语句后的复合语句,需要在if语句末尾添加分号“;”,还可以使用空的复合语句。

对于if...else语句可以使用三元操作符对语句进行简化。

switch语句根据一个表达式的结果,执行特定的操作。其中表达式、case常量是必要的参数,而break和default是可选的参数。

switch语句中的表达式类型的级别必须低于int整数类型,即表达式的值只能是byte、short、char或int类型,不能使用浮点类型或long长整型。case子句中的常量必须和表达式的类型兼容,而且每个case子句的常量值都不能相同。switch语句首先计算表达式的值,如果表达式的值和某个case后面的常量值相同,则执行该case语句后的若干个语句直到遇到break语句为止。如果case语句中没有break语句,则继续执行下一个case语句,直到遇到break语句为止。若没有一个case子句的常量与表达式的值相同,则执行default分支的语句。如果defgaul语句不存在,则switch语句不做任何处理。break语句用来跳出switch语句。switch语句的所有功能都可以使用if...else语句实现。多个相连的case分支省略break语句可以实现多个条件执行同一业务处理的效果。

case常量表达式的值可以是字符,但一定不能使字符串。

在编程时,有时会遇到使用for循环的特殊语法格式来实现无限循环,语法格式为:for(;;){循环体},对于无限循环可以通过break语句跳出循环。while语句是“当型”循环语句,当条件表达式成立时就执行循环体中的内容。在执行完循环体中的语句后,重新判断条件表达式的返回值,直到表达式返回的结果为假时退出循环。do...while循环语句与while循环语句类似,它们之间的差别是while语句先判断条件是否成立在执行循环体,而do...while语句是先执行一次循环,在判断条件是否成立。do...while语句与while语句的一个明显区别是,它在结尾处多了一个分号(;).

println()方法在输出字符串时,自动在字符串的末尾添加"\n"换行字符,print()方法同样用于输出字符串到控制台,该方法输出字符串后不会自动换行。

Java语言定义的break语句和continue语句用于循环的跳转,它们可以实现上述功能。

continue语句只能应用于for、while和do...while循环语句中,用于结束本次循环直接跳过循环体剩余的语句,进行下一次循环。continue 标号;标号是可选参数,标号有合法标识符和“:”字符组成。

不带标号跳转:将结束本次循环,跳过循环体中剩余的没有被执行的语句,根据循环的条件判断去执行下一次循环,或者结束循环。如果是for循环,则还要执行表达式3,改变循环控制变量的值。

带标号跳转:不是跳过当前循环语句的一次循环,而是跳过标号指定的循环语句的一次循环,然后判断指定标号的循环条件,以决定是否执行标号指定的循环。

break语句可以应用在for、while和do...while循环语句中,实现强行退出循环。break 标号;不带标号的break语句可以终止break语句所在的循环。带标号的break语句可以终止标号指定的循环,它常用语跳出多层循环的情况。

break语句和continue语句的区别在于break语句将终止整个循环语句,而continue语句只结束本次循环。

数组是具有相同数据类型的一组数据的集合。数组作为对象允许使用new关键字进行内存分配,在使用数组之间,必须先定义数组变量所属的类型,即声明数组。数组类型 数组名[];或数组类型[] 数组名;声明数组后,还不能访问其中的任何元素。因为声明数组仅仅是给出了数组名称和类型,要想真正使用数组还要为它分配内存空间。在为数组分配内存空间时必须指明数组的长度。数组名字  = new 数组类型[数组长度];

数组的下标是从0开始的。使用new关键字为数组分配内存时,整型数组中的各个元素的初始化值都为0。也可在声明数组的同时为数组分配内存;数组元素类型 数组名 = new 数组元素类型[数组元素的个数];

数组的初始化就是包括在花括号之内用逗号分开的表达式列表。逗号分开了数组元素的值。对于整型二维数组,创建成功之后系统会给数组中的每个元素初始化值0.

length()方法用于获取数组的长度,array.length()方法判断第一维数组的长度,array[0].length方法判断下标为0的第二维数组的长度。

foreach()主要用于执行遍历功能的循环

for(类型 var:集合){

    循环体

}

在eclipse中输入“fore”,然后按ALT+/键,在弹出菜单中选择foreach系列的模板选项。

java.util包的Arrays类包含用来操作数组的各种方法,可通过Arrays类的静态方法fill()来对数组中的元素进行替换。既可以填充整个数组也可以填充部分数组。通过Arrays类的静态sort()方法实现对数组排序.Arrays类的copyOf()方法与copyOfRange()方法可实现对数组的复制。copyOf()方法是复制数组至指定长度。copyOfRange()方法则将指定数组的指定长度复制到一个新数组中。对于copyOf()方法,如果新数组的长度大于数组arr的长度,则用0填充(根据复制数组的类型来决定填充的值,整型数组用0填充。char型数组则会使用null填充);如果复制后的数组长度小于数组arr的长度,会从数组arr的第一个元素开始截取知道满足新数组长度为止。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325770177&siteId=291194637
Recommended