Boring to rely on JavaEE from entry to abandon (two) basic grammar

table of Contents

1. Comments (single line, multiple lines, document)

Two. Identifier_Keyword_Hump Principle

3. Bug solution: Welcome.java:6: Error: Unmapped characters encoding GBK

Four. Constant constant

Five. Data Type

1. Integer

2. Floating point

3. Character type

4. Boolean boolean _if statement use points _ boolean occupies space problem

Six. Operator

Seven. Construction method


Although I was quite familiar with it in my sophomore year, it has been so long after all, it is better to do it again. After all, I have to study systematically. Of course not too detailed.

1. Comments (single line, multiple lines, document)

//Single line comment

/*Multi-line

Comment*/ (Multi-line comments can also be placed between valid codes)

Documentation notes:

Documentation comments start with "/**" and end with "*/". The comments contain some descriptive text and some JavaDoc tags (when writing the project later, you can generate the project's API)-I will talk about it later

/**
 * test 文档注释
 * @author 深海鱼肝油
 * @version 1.0
 */
public class Welcome {
    public static void main(String[] args /*参数名称,可变*/){
        //单行注释
        System.out.println("hello world!");
    }
}
/*
多行啊
百无聊赖啊
 */

Two. Identifier_Keyword_Hump Principle

Identifier naming rules:

The identifier must start with a letter, underscore, and dollar sign $.
The other parts of the identifier can be
any combination of letters, underscore dollar sign "$" and numbers.
Java identifiers are case sensitive and have unlimited length.
Identifiers cannot be Java keywords.

Java does not use the ASCII character set used by ordinary languages ​​but uses a standard international character set such as Unicode. Therefore, the meaning of the letters here is not only English, but also Chinese characters and so on. But it is not recommended that you use Chinese characters to define identifiers!

The ASCII character set uses one byte to represent characters, so only 2^8=256 characters; while the Unicode character set uses two bytes to represent one character, so 2^16=65536 characters.

eg:

public class Welcome {
    public static void main(String[] args /*参数名称,可变*/){

        int 随便=123;//java用的是Unicode字符集,所以汉字可以作为变量名称
        int abc=12;
        int $salary=3500;
        int _year=2021;
        System.out.println("hello world!");
    }
}

Identifier representing the class name: the first letter of each word is capitalized, such as Man, GoodMan
represents the identifier of methods and variables: the first word is lowercase, and the first letter from the second word is capitalized. We call it the "camel case principle" ”, such as eat(), eatFood()

3. Bug solution: Welcome.java:6: Error: Unmapped characters encoding GBK

When compiling with javac file name on the command line, an encoding error is reported, and it is converted to GBK encoding under idea, because the command line uses the encoding of the local operating system.

Convert to GBK type in the lower right corner.

Of course, the command line is generally not used to compile, unless the code is written in notepad or notepad. .

Four. Constant constant

Modified with final becomes a constant, and the value cannot be changed after assignment, otherwise an error will be reported.

Five. Data Type

Unlike C++, the number of bytes of data types corresponding to different compilers may change accordingly, and Java is fixed.

When defining variables, it is recommended to define line by line to improve readability, not like int i,j;.

1. Integer

byte1 byte, short2 byte, int4 byte, long8 byte

Octal number: add 0 at the beginning, such as 016

Hexadecimal number: add 0x or 0X at the beginning, such as 0x16

Binary number: add 0b or 0B at the beginning, such as 0b1000101

Also, to declare as a long type must be followed by L or l

eg:

        int a=100;
        int b=5030303;
        int c=05;//八进制
        int d=0x15;//十六进制
        int e=0b101011;//二进制
        long f=5555555;
        long f2=55555555555555L;//将整数常量定义为long类型

2. Floating point

Because decimals are infinite, so float4 bytes and double8 bytes are definitely not enough, as many digits are not enough, because decimals are infinite! Therefore, there is an error in the storage of decimals.

The value of the float type has a suffix F or f, and the floating-point value without the suffix F/f defaults to the double type. You can also add the suffix D or d after the floating-point value to make it clear that it is a double type.

eg:

Code:

        double d1=3.14;
        double d2=3.14e2;//科学计数法表示小数
        System.out.println(d2);

        //浮点数是不精确的,尽量不要直接进行比较
        float d3=0.1F;
        double d4=1.0/10;
        System.out.println(d4);
        System.out.println(d3==d4);
        float f5=1234567412356L;//long转float
        float f6=f5+1;//如果f5和f6都是整型性的话肯定不相等
        System.out.println(f5==f6);

Output result:

314.0
0.1
false
true

3. Character type

Can be converted to int type to view the serial number of the character: (int)c

Unicode has an encoding from 0 to 65535, and they are usually represented by the sixteenth mechanism from'\u0000' to'\uFFFF', and the prefix u means Unicode.

Code:

        char c1='a';
        char c2='中';
        //也可以通过每个字符对应的编码来输出字符
        char c3='\u0061';//0061的十六进制就是97
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);

        //java中的字符串不是基本数据类型,而是一个独立定义的类
        String str ="我爱中国";
        System.out.println(str);

        //转义字符
        System.out.println("a\nb\nc\nd\te\tf\tg,\",\',\\");

Output result:

a
a
我爱中国
a
b
c
d	e	f	g,",',\

4. Boolean boolean _if statement use points _ boolean occupies space problem

Description of a few bytes of boolean type:
In the description of the book "Java Virtual Machine Specification": "Although this boolean data type is defined, but it only provides a non
very limited support for the Java virtual machine. There is no bytecode instruction dedicated to the
boolean value. The boolean value manipulated by the Java language expression is replaced by the int data type in the Java virtual machine after compilation, and the boolean array will be encoded into the Java
virtual machine byte array. Each element of boolean element occupies 8 bits". That is to say, the JVM specification points out that boolean is treated as int,
which is 4 bytes
, and boolean array is treated as byte array. In this way, we can conclude that the boolean type occupies 4
bytes for single use , and it is a certain word in the array. Section .

Suggest:

Don't write like this: if(flag==true), only novices write that. The key is also easy to write if(flag=true) wrong, so it becomes the assignment of flag to true instead of judgment! The veteran's writing is if (flag) or if (!flag)

Sample code:

        boolean b1=true;
        boolean b2=false;
        if (b1){
            System.out.println("正确");
        }
        else{
            System.out.println("错误");
        }

Six. Operator

Integer operation:
If one of the two operands is long, the result is also long. If
there is no long, the result is int. Even if the operands are all short, byte, the result is an int.

eg:

int a=5;
long b=7;
long c=a+b;//要用long类型来接受a与b的和,否则报错类型不兼容


Floating point operations:
If one of the two operands is doubler, the result is double.
Only when both operands are float, the result is float.

Modulo operation:

The operand can be a floating-point number. In general, integers are used, and the result is "remainder". The sign of "remainder" is the same as that of the left operand, such as: 7%3=1, -7%3=-1, 7%-3=1.

eg:

        int a=1;
        Long b=2L;
        boolean flag=b instanceof Long;//注意是Long(包装类)
        System.out.println(flag);
        long c=a+b;
        System.out.println(c);
        double d=31/3;//先31/3=10(整型运算),之后,再将10赋给double型
        System.out.println(d);

Output:

true
3
10.0

relationship:

&&, || contrast &, | means that the former is a short-circuit, that is, the first half does not meet the conditions and the second half does not need to be read!

Shift:

        int a=5<<2; //相当于5*2*2=20
        System.out.println(a);
        System.out.println(40>>3); //相当于40/2/2/2=8

String concatenation:

        String a="3";
        int b=4;
        System.out.println(a+b);
        //条件是String,不是char,为char时,仍然是加号。不是连接符
        char c1='h';
        char c2='i';
        System.out.println(c1+c2);
        System.out.println(""+c1+c2);//通过加""+,将整个运算转化为字符串连接操作

Output:

34
209
hi

Conditional operator:

        int score=90;
        String a=score<60?"不及格":"及格";
        System.out.println(a);

priority:

There is no need to deliberately remember these priorities, the parentheses are preferred to organize expressions!! The priority of
logical AND, logical OR, and logical NOT must be familiar! (Logical NOT>Logical AND>Logical prestige). Such as:
the operation result of allb&c is: al(b&c), not (alb)&c

Automatic type conversion:

Automatic type conversion means that a data type with a small capacity can be automatically converted to a data type with a large capacity, as shown in the figure below:

The solid line indicates the type conversion without data loss, and the dashed line indicates that the conversion may have a loss of accuracy!

Integer constants can be directly assigned to type variables such as byte, short, char, etc., without the need for forced type conversion, as long as it does not exceed the number of tables.

        //从容量小的类型可以自动转化为容量大的类型
        int a = 2345;
        long b=a;
        //int c= b; //long类型不能自动转为int
        double d = b;
        float f= b;
        //特例:整型常量是int类型,但是可以自动转为: byte,short,char。只要不超过对应类型的表数范围。
        byte h1 = 123;
        //byte h2 = 1234; //1234超过 了byte的表数范围[-128,127]。
        char h3 = 600;
        //char[0,65535]之间
        System.out.println(h3);

Output:

ɘ

Forced type conversion:

        double a = 3.9415;
        int b = (int)a;//b:3.浮点数强制转成整数,则直接丢失小数部分(不会四舍五入)
        System.out.println(b);
        int c=97;
        System.out.println((char)c);
        //强制转型,超过了表数范围,则会转成一个完全不同的值。
        byte d = (byte)300;
        System.out.println(d);

Output:

3
a
44

Common conversion overflow problems (L problems):

Int is used when the long integer should be used

        //溢出问题
        int money = 1000000000; //10亿
        int years = 20;
        //返回的total是负数,超过了int的范围
        int total = money*years;
        System.out.println(" total= " + total);
        //返回的total仍然是负数。默认是int ,因此结果会转成int值,再转成long。但是已经发生了数据丢失
        long total1 = money*years;
        System.out.println(" total1="+ total1);
        //返回的total2正确:先将一个因子变成long ,整个表达式发生提升。全部用long来计算。
        long total2 = (money*(long)years);
        //long total2 = 1L*money*years;
        System.out.println(" total2=" + total2);
        //人的寿命是80岁,经历了多少次心跳
        long num = 100L *60*24*365*80; //注意这里100L是长整型
        System.out.println("人的一-生心跳次数: " +num);

Output:

 total= -1474836480
 total1=-1474836480
 total2=20000000000
人的一-生心跳次数: 4204800000

The default value of the member variable:

Integrity: 0

Floating point type: 0.0

Character type:'\u0000'

Boolean: false

All reference types: null

Seven. Construction method

Regardless of whether you write a constructor with no parameters or with parameters, the system will no longer automatically generate a constructor.

The constructor is called by the new keyword!!
Although the constructor has a return value, the return value type cannot be defined (the type of the return value must be this type), and you cannot use return in the constructor to return a certain value.
If we do not define a constructor, the compiler will automatically define a constructor with no parameters. If it is defined, the compiler will not automatically add it!
The method name of the constructor must be the same as the class name!

 

 

OOP: Oriented-Object project object-oriented design

OOA: Oriented-Object analysis

Understand Module as a sub-project. Module can also be set up with a separate JDK.

 

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/115005714