[crazy God talking about Java] learning note 8: basic Java syntax

Posted by dirkadirka on Sun, 16 Jan 2022 13:48:07 +0100

[crazy God talking about Java] learning note 8: basic Java syntax
[01] comment, identifier, keyword

[02] data type

[03] type conversion

[04] variable, constant

[05] operator

[06] package mechanism, JavaDoc

image

[01] comment, identifier, keyword
notes
Comments are remarks that will not be executed by the program.

The advantage of writing notes is to make it easy for yourself to recall and let others understand your code.

//	Annotation method 1: single line remarks.

/*
	Annotation method 2: multi line comments.
*/

/**	Annotation method 3: document annotation, with an asterisk on each line, the written document information will be recognized.
 * @Description HelloWorld
 * @Author Alvin.Wang
 */

Change the format and color of notes
image

Identifiers and keywords
The highlighted words in Java code are keywords with special meanings. Avoid using these words when naming variables,

Java keywords are basically the following.
image

Considerations for identifiers

Java is very case sensitive. Variable man and variable man are not the same thing.

For naming, try to use English instead of Chinese. For example, use name instead of mingzi.

image

[02] data type
Strongly typed language: strongly typed definition language. It requires that the use of variables should strictly comply with the specification, and all variables must be defined before they can take effect. For example, Java, C language, C + +, etc. High security, but the running speed will be slow.

Weakly typed languages: compared with strongly typed languages, the code requirements of vb or JavaScript are not so strict.

image

image

data type
Data types include: numeric type and reference type.

Numeric type: integer type, floating point type, character type, boolean type.

Reference type: class, interface, array.

	/*  Eight value types  */
    //  integer
    int num1 = 10;      //Most commonly used
    byte num2 = 20;
    short num3 = 30;
    long num4 = 30L;    //Long type usually adds L after the number, and the declaration is long type

    //  Floating point type. decimal
    float num5 = 50.1F; //F should be added at the end of float type
    double num6 = 3.141592653;

    //  character
    char name = 'A';
    //  String, combination and splicing of characters.
    String namea = "China";

    //  Boolean value, only true and false.
    boolean flag_yes = true;
    boolean flag_no = false;

byte
image

Development examples (including interview questions)
Integer extension case
//Integer expansion: hexadecimal writing. Binary 0b, octal 0, decimal, hexadecimal 0x,
int i = 10;
int i1 = 010; // Octal 0
int i2 = 0x10; // Hex 0x consists of 0-9 and A-F

    System.out.println("10 Base system i=" + i );      //  Decimal i=10
    System.out.println("8 Base system i1=" + i1 );     //  Octal i1=8
    System.out.println("16 Base system i2=" + i2 );    //  Hexadecimal i2=16

Floating point expansion case
Example 1: float f=0.1f, double d=1.0/10, but the two numbers are not equal. The values obtained are all 0.1

image

Example 2: float D1 = 231212314235125f, float d2=d1+1, but the two numbers are equal.

image

float is finite, discrete, and rounding error. Can not be completely equal to, will only be infinitely close.

Avoid comparing with floating point numbers!!!

Therefore, the banking system will use BigDecimal (large number type of Java and mathematical tool type)

Character expansion case
All characters are still numbers in nature. Text is encoded from numbers and encoded in Unicode.

image

Boolean extension
boolean flag = true;
if(flag == true) {} / / new writing method, re emphasize flag
if(flag) {} / / it is written by an old hand. There is no need to emphasize the authenticity of flag again.
//Less is More code should be concise and easy to read
[03] type conversion
image

Case 1
Cast: overflow occurs from high to low. Pour a 2L bucket of water into a mineral water bottle.

Precautions during conversion: 1 Boolean values cannot be converted; 2. Cannot be converted to irrelevant type; 3. High capacity to low capacity; 4. Pay attention to memory overflow or accuracy problems during forced conversion.

image

Case 2
image

[04] variable, constant
variable
image

    // Variable assignment
    // int a=1,b=2,c=3;   The readability of the program is poor. Don't write it like this during development
    int a =1;
    int b =2;
    String name = "wang";
    char x = 'X';
    double pi = 3.14;

Variable scope
Class variable: available globally. The static keyword must be added before it.

Instance variable: subordinate to the whole object / class, with a larger scope. No initialization is required and there are default values.

The default values are 0 (integer), 0.0 (floating point), false (Boolean), null (character)

Local variable: it is written in the method and acts inside the method. And the assignment must be declared and initialized.

image

public class Demo08 {
//Variable

// ● class 1 variable static must be added up
// In this way, you don't need new like instance variables.
static double salary = 2500;

// ● 2 ● instance variable: subordinate to the object. Call demo08 Name.
String name;
int age;

// main method 
public static void main(String[] args) {

    // ● 3 ● local variables: assignment must be declared and initialized.
    int i = 10;
    System.out.println(i);
	
    // Call of instance variable.
    // Variable type variable name = new Deme08(); Self reference type.
    //Tip: after entering new Demo08(), press Alt + Enter to automatically form the next line of declaration.
    Demo08 demo08 = new Demo08();
    System.out.println(demo08.name);
    System.out.println(demo08.age);

    //The class variable static can be called directly and output the result.
    System.out.println(salary);

}

// Other methods
public void add(){
    
}

}

constant
Value that will not change. From beginning to end is a value. Define with final. The general name is capitalized.

image

image

[05] operator
operator
image

Arithmetic operators: binary operations
//Binary operator
//Tip: Ctrl + D copies the current line to the next line.
int a = 10;
int b = 20;
int c = 25;
int d = 25;

    System.out.println(a+b);
    System.out.println(a-b);
    System.out.println(a*b);
	// The result of 10 / 20 is 0.5, but it is displayed as 0. At this time, change the type of a or b to double
    System.out.println((double)a/b);    
	// %: modulo operation. The remainder is 1
    System.out.println(21%20); 	// 1

Floating point binary operation
The floating-point calculation result is of type int by default. If there is a long type, it is of type long by default.

    //  Floating point binary operation
    //  Conclusion: the calculation result is of type int by default. If there is a long type, it is of type long by default.
    long a = 1231311L;
    int b = 123;
    short c = 10;
    byte d = 8;

    System.out.println(a+b+c+d);    //long type: 1231452
    System.out.println(b+c+d);      //int type: 141
    System.out.println(c+d);        //int type: 18
    System.out.println((double)c+d);//18.0 after forced conversion type double

boolean relational operator
//The result returned by the relational operator of boolean value
int a = 10;
int b = 20;
int c = 21;

    System.out.println(a>b);	//false
    System.out.println(a<b);	//true
    System.out.println(a==b);	//false
    System.out.println(a!=b);	//true

Self increasing and self decreasing operator, also known as unary operator.
//+ + and – self increasing and self decreasing operators.
// b=a++; In fact, b=a,a=a+1; Steps for.
// c=++a; In fact, a=a+1,c=a; Steps for.
//Difference: a + + (hand in the value of a first, and then complete self increment)++ A (self increase first, and then hand in the results.)
int a =3;
int b =a++; // Explanation: at this time, a first gives the value to B, b=3, and then a increases automatically, a=4.
System.out.println(a); // a=4
System.out.println(b); // b=3
int c =++a; // Explanation: at this time, a increases by a=5, and then gives the value to c,c=5.
System.out.println(a); // a=5
System.out.println©; // c=5

	//  Power operation 2 ~ 3 = 2 * 2 * 2 = 8
    //  Quick input: math Pow (2, 3) and then Alt+Enter.
    double pow = Math.pow(2, 3);
    System.out.println(pow);    //8.0

Logic operation and short circuit operation
//Logical operator
//And (A & &b) operation: two are true, one is true, one is false, and two are false.
//Or (a|b) operation: if both are true, it is true, if one is true, it is true, and if both are false, it is false.
//Non operation: the inverse of the result.
boolean a = true;
boolean b = false;

    System.out.println("Logic and operation a&&b: " + (a && b));          //false
    System.out.println("Logical or operation a||b: " + (a || b));          //true
    System.out.println("Logical non operation !(a&&b): " + (!(a && b)));    //true


    // Short circuit operation
    // In the and operation, & & if there is an error in the front, the one behind the symbol will not be calculated.
    int c=5;
    boolean d=(c<4)&&(c++<4);   //&&The calculation formula on the left is wrong, so the calculation on the right is not performed. c or 5
    System.out.println(d);      //false
    System.out.println(c);      //5

    boolean e=(c>4)&&(c++<4);   //&&The calculation formula on the left is right, followed by c + + on the right is 6, less than 4, and e is wrong.
    System.out.println(e);      //false
    System.out.println(c);      //c + + is calculated in 6, so c increases.

Bitwise Operators
/*Bit operators are basically computational problems involving computers
A = 0011 1100
B = 0000 1101
----------Bit operation----------
A & B = 0000 and 1100 bits: both are 1
A|b = 0011 or 1101 bit: as long as there is a 1, it is 1
A^B = 0011 0001 negative: 0 if the same, otherwise 1
~B = 1111 0010 inverse B.
*/

   /*   Example of bit operation: fast calculation of 2 * 8, i.e. 2 * 2 * 2
        0000 0000   0
        0000 0001   1
        0000 0010   2   1 Shift left 1 bit, 1 * 2
        0000 0011   3
        0000 0100   4   1 Shift left by 2 bits, 1 * 2 * 2
        0000 1000   8   1 Shift left 3 bits, 1 * 2 * 2 * 2
        0001 0000  16   1 Shift left 4 bits, 1 * 2 * 2 * 2 * 2
     So shift left < < yes * 2
        Shift right > > Yes / 2
    */
    System.out.println(2<<3);       //16    2*(2*2*2)
    System.out.println(16>>3);      //2     16/2/2/2

Extended logical operators and string splicing
//Extended logical operator
int a = 10;
int b = 20;

    a += b;     //a = a+b =30
    a -= b;     //a = a-b =-10
    System.out.println(a);

    //  String connector +, string
    System.out.println(a+b+"");     //30 first calculate a+b and then splice
    System.out.println(""+a+b);     //1020 recognizes the string symbols, and then performs number splicing 1020

Ternary operations: conditional operators
/*Ternary operators: conditional operators
X ? Y: Z if x is true, the result is y; otherwise, the result is Z
*/
int score = 55;
String type = score < 60? "Fail": "pass";
System.out.println(type); // fail,
[06] package mechanism, JavaDoc
Package mechanism
The essence of a package is a folder. Prevent namespace duplication.

Generally, the company domain name is flashed as the package name.

The defined package is called package, and the imported package is called import.

image

JavaDoc
Baidu can search JDK help documents. This link https://www.matools.com/api/java8

image

package operator;
//Class comments
/**

  • @author Alvin.Wang author
  • @version 1.0
  • @since 1.8 requires the earliest jdk version

*/

public class Doc {
String name;
//Method annotation. After the following code and class annotation are written, enter / enter to automatically generate the method annotation.
/
*
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception{
return name;

}

}
The role of JavaDoc
What is the difference between JavaDoc annotations and ordinary annotations?

First find the folder where the code is located.

image

Then find the folder, enter cmd and space in front, then enter the Dos interface, and enter Javadoc - encoding UTF-8 - charset UTF-8 doc java

Through this line of instruction, the text coding of Doc document is output in UTF-8, which is more suitable for Chinese environment.

image

At this time, it is found that many files are generated in the original folder. Click index HTML (index home page) you will open something much like an official document, which is your doc Java document version, Doc The parameter information of Java is also written in.

image

Topics: Java