JAVA basic syntax

Java basic syntax

notes

  • Single-Line Comments //
  • Multiline comment / **/
  • Document comments / * **/
//I'm a single line note
/*
I am
 multiline comment 
Love you!
 */
/**@data : 2022.1.3
 * @author :fengyifan 
 * 
 */

identifier

  • All identifiers should be in English letters (AZ or AZ), dollar sign ($), underscore () start.
  • Keywords cannot be used as variable or method names. (public,class...)
  • Identifiers are case sensitive. (String man is different from String man)
  • Class name, variable name and method name are all called identifiers.

data type

  • Basic data type

byte One byte, - 128 ~ 127

short 2 bytes, - 3268 ~ 32767

int 4 bytes,

long 8 bytes,

long num4= 6521L;//L should be added after long data type to distinguish

float 4 bytes

 float num5 = 0.1f;//f should be added to the float data type to distinguish it

double 8 bytes

Char 2 bytes

boolean type (boolean) 1-bit true false

1 byte = 8 bits; 1 byte=8 bit

  • Reference data type

class

Interface

array

  • Integer extension

Binary 0b octal 0 hex 0x

public class Demo02 {
    public static void main(String[] args) {
        int i = 10;
        int i1 = 010;
        int i2 = 0x10;
        int i3 = 0x11;
        System.out.println(i);
        System.out.println(i1);
        System.out.println(i2);
        System.out.println(i3);
    }
}

  • Floating point extension

Do not compare with floating point numbers

float num5 = 0.5f;//f should be added to the float data type to distinguish it
float num7 = 10/20;
System.out.println(num5==num7);

  • Character expansion

The essence of characters is still numbers

char ahead='in';
        char  moon = 'a';
        System.out.println(moon);
        System.out.println((int)moon);
        System.out.println(ahead);
        System.out.println((int)ahead);

  • Escape character

\t tab

\n line feed

\r enter and wrap

\\ \

\' '

  • Boolean extension
boolean flag = true;
//if(flag) is equivalent to if(flag==true)

Type conversion

  • Cast (high to low) (overflow problem, precision problem)
int a = 128;
byte b = (byte) a;
System.out.println(a);
System.out.println(b);//Overflow problem, byte type range - 128 ~ 127

int salary = 10_0000_0000;
        int years = 20;
        int total = salary * years;//overflow
        long total2 = salary * years;//Overflow before conversion
        long total3 = salary * (long)years;//Correct, first convert a data to long type
        System.out.println(total);
        System.out.println(total2);
        System.out.println(total3);

System.out.println((int)23.7);
System.out.println((int)-45.6521f);

char c1 = 'a';
        int c2 = c1+1;
        System.out.println(c2);
        System.out.println((char)c2);

  • Automatic conversion (low to high)

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

  • Boolean values cannot be data type converted

variable

  • Variable naming convention
  1. Try to see the name and know the meaning.
  2. Class member variables: initial lowercase and hump principle. (hump principle: the first word is capitalized after the first word)
  3. Local variables: initial lowercase and hump principle.
  4. Constants: uppercase or underscore MAX_VALUE
  5. Class name: initial capitalization and hump principle. GoodMan
  6. Method name: initial lowercase and hump principle. runRun()
  • Type of variable

The type of a defined variable can be either a basic type or a reference type. For example, int a=10;String name = "fengyifan";

  • Scope of variable
public class Demo04 {
    static int age1 = 21;//Class variable: outside the method, in the class. static + variable type + variable name, which can be output normally;
    String name;/*Instance variables, outside methods, in classes. You need lines 10, 11, 12 and 13 to output normally. If there is no self initialization, the default value of the value type is 0, the default value of the Boolean value is false, and the default value is null except for the basic type*/
    int age2;//Instance variable
    boolean flag;//Instance variable
    public static void main(String[] args) {
        int i = 11;//Local variables, within the method. Valid only in up and down {}
        System.out.println(i);
        System.out.println(age1);
        Demo04 demo04 = new Demo04();//10
        System.out.println(demo04.name);//11
        System.out.println(demo04.age2);//12
        System.out.println(demo04.flag);//13
    }
}

The difference between instance variables and class variables

  • constant

Constants are usually uppercase letters

Format: final + constant type + constant name = value;

For example: static final double PI = 3.141592653587932; Or final static double PI = 3.141592653587932

final and static are modifiers and do not distinguish between before and after.

operator

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

Relational operators: >, <, = == instance of,<=,>=

Assignment operator:=

Logical operators: & &, |, |,! (and or not)

One of the operands is long or double, and the result is long or double. Without long and double, the result is int.

The results returned by relational operators are only true and false.

int a = 10;
        int b = 20;
        System.out.println(a==b);
        System.out.println(a<b);

Self increasing and self decreasing

int a = 3;
        int b = a++;//Assign value first, and then increase by itself
        int b1 = a--;//Assign first and then subtract
        int c = ++a;//Self increment before assignment
        int c1 = --a;//Self subtraction before assignment
        System.out.println(a);
        System.out.println(b);
        System.out.println(b1);
        System.out.println(c);
        System.out.println(c1);

  • Logical operator
boolean a = true;
        boolean b = false;
        System.out.println("a&&b:"+(a&&b));//+Is a string connector '* * *' is a string
        System.out.println("a||b:"+(a||b));
        System.out.println("!a:"+!a);
        System.out.println("!b:"+!b);

&&And operation, both variables are true, and the result is true. 0 if there is 0

||Or operation, if one of the two variables is true, the result is true. If there is 1, 1

! Negative operation

  • Short circuit operation
//Short circuit operation,
        int c = 5;
        boolean d = (c<4)&&(c++<10);//After judging that C < 4 is false, it is not necessary to continue the subsequent operation to obtain that d is false.
		System.out.println(d);
        System.out.println(c);

  • Bitwise Operators

&, |, ~, ^ (XOR), < (move left), > > (move right)

XOR operation: 0 for the same, 1 for the different

Example:
How to efficiently calculate the value of 2 * 8

//< < just *; > > That's it/
        System.out.println(2<<3);//2*8=16
        System.out.println(4<<4);//4*16=64
        System.out.println(8>>1);//8/2=4
    }
}
/*
0000_0010       2
0000_0100       4
0000_1000       8
0001_0000       16
0100_0000       64
 */

  • String connector
int a = 10;
int b = 20;
System.out.println(""+a+b);//Both + are string connectors
System.out.println(a+b+"");//The first + is the operator and the second + is the string connector

  • Ternary operator
 public static void main(String[] args) {
        //Ternary operator x? Y: z if x==true, the result is y; otherwise, it is z.
        int score = 90;
        String type = score<60 ? "fail,":"pass";
        System.out.println(type);
    }

Package mechanism

It is generally named upside down with the company domain name

For example, Baidu www.baidu.com com

com.baidu.www com.baidu.wenku

Java Doc

/**
 * @author author
 * @version Version number
 * @since Indicates that the earliest jdk version 1.8 is required
 * @param Parameter name 
 * @return Return value
 * @throws Exception thrown
 */

Generate doc with command line

Generate with IDEA

  1. Create a new folder

  2. Open the toolbar in the IDEA and click generate JavaDoc

    1. Output directory select the folder you just created.

  3. The locale is zh_CN

  4. Other command line parameters are set to - encoding UTF-8 -charset UTF-8

  5. Click OK and open the index file in the folder to view it

Added by simulant on Wed, 05 Jan 2022 05:00:38 +0200