Java basic syntax



Comments, identifiers, keywords

Writing notes is a very good habit

  • Single-Line Comments
  • multiline comment
  • Documentation Comments
//This is a single line comment

/*
	This is a
	multiline comment 
*/

/**
 *This is a document comment, which can be @ followed by a lot of content that can be recognized by the program
 *@Description HelloWorld
 *@Author Accelerator
 */

Identifiers are case sensitive


data type

  • Strongly typed language - all variables must be declared before use, unlike Python

  • Weakly typed language

One byte = 8 bits

1byte = 8 bit


The data types of Java are divided into two categories

  • Basic type

    • value type

      • Integer type
        • byte: 1 byte - 128 ~ 127
        • short: 2 bytes - 32768 ~ 32767 30000
        • int: 4 bytes up to 2 billion
        • long: 8 bytes
      • Floating point type
        • float: 4 bytes
        • double: 8 bytes
      • Character type
        • char
        • String
    • boolean type: only one bit, true and false

  • reference type

    • class
    • Interface
    • array

When declaring data, pay attention to whether there will be cutting in the process of declaration

float f = 10.1F;	//Adding F here is to declare that this 10.1 is a float type, so it will not be cut when assigning a value to the float object
long l = 10L;		//Add L here to declare that this 10 is a long type

float f = 0.1F;     //0.1
double d = 1.0/10;  //0.1
//f is not equal to d

float d1 = 121212121212121212F;
float d2 = d1 + 1;
//d1 equals d2
//Equivalent to scientific notation, many decimal places are offset

/*
Escape character
/t	tab
/n	Line feed
*/

It's best to avoid using floating-point numbers entirely for comparison


Base system

  • Binary: 0b

  • Octal: 0

  • Hex: 0x

    For example, 010 is 8 and 0x10 is 16


Type conversion

size

Low ------------------------------------------ > High

byte,short,char —> int —> long —> float —> double

Saving decimals uses more digits than saving integers

//Cast type from high to low (because you need to cut)
int i = 128;
byte b = (byte)i;	//At this time, the value of b is - 128, and the memory overflows, because byte has only 1 byte, - 128 ~ 127

//Automatic type conversion from low to high
int i = 128;
double b = i;		//boolean cannot be converted,

char c = 'a';
int d = c + 1;
System.out.println(d);			//Output 98 automatic type conversion
System.out.println((char)d);	//Output b cast

int money = 10_0000_0000;	//1 billion
int years = 20;
//long total = money*years;  Wrong! Overflow before conversion
//Correct usage:
long total = money * (long)years

Variable, constant

variable

  • Class variable
  • Instance variable
  • local variable

constant

final Constant name = value;	//Constant names generally use uppercase characters

Naming conventions

  • Class member variables: first letter lowercase and hump principle: monthSalary except the first word, the following words are capitalized
  • Local variables: initial lowercase and hump principle
  • Constants: uppercase letters and underscores MAX_VALUE
  • Method name: initial lowercase and hump principle
  • Class name: initial capitalization and hump principle
package base;

public class Demo01 {

    //Class variable static: it belongs to a class and can be output directly below
    static double salary = 2500;

    //Attributes: Variables

    //Instance variable: it is subordinate to the object and does not initialize itself. The default value is 0.0 null
    String name;
    int age;

    //main method 
    public static void main(String[] args) {
        //Use class variable static: directly use
        System.out.println(salary);

        //Using instance variables
        Demo01 MyDemo = new Demo01();
        System.out.println(MyDemo.name);

        //local variable
        int i = 10;
    }

    //Other methods
    public void add(){}
}

operator

  • Arithmetic operators: +, -, *, /,%, + +, –
  • Assignment operator:=
  • Relational operators: >, <, > =, < =, = =,! =, instanceof
  • Logical operators: & &, ||,!
  • Bitwise operators: &, |, ^, ~, > >, <, > > >
  • Conditional operator:?:
  • Extended assignment operator: + =, - =, * =/=
//The difference between a + + and + + A
int a = 3;
int b = a++;	//Operation before self increment
int c = ++a;	//Self increment before operation

a+=b;	//a=a+b
a-=b;	//a=a-b


//Bit operation
/*
A = 0011 1100
B = 0000 1101
--------------------------------
A&B 0000 1100 Bitwise AND
A|B 0011 1101 Bitwise OR
A^B 0011 0001 Bitwise XOR (bitwise addition)
~B  1111 0010 Bitwise inversion

Move left:<<
Shift right: > >
---------------------------------
2*8 = 16 How to calculate quickly?
2<<3 that will do

//String connector + as long as there is a string and it is not at the end, it is converted to a string
int a = 10;
int b = 20;
System.out.println(a+b);	//Output 30
System.out.println(a+b+"");	//Output 30
System.out.println(a+""+b);	//Output 1020
System.out.println(""+a+b);	//Output 1020

//Ternary operator 
x ? y : z	//x If true, the result is y, otherwise z
*/

Package mechanism

The essence is similar to folders. Packages have been created in src before.

Generally, the company domain name is inverted as the package name: com yishif. www

When entering the package name, the point is used to define the hierarchy between packages

Package reference


javaDoc

Generate comment information into help document

Create a new Doc package

package com.base;
//Adding to a class is a class annotation
/**
 * @author yishif
 * @version 1.0
 * @since 1.8
 */
public class Doc {
    String name;
    //The method annotation is added to the method
    /**
     *
     * @param name
     * @return
     * @throws Exception
     */
    public String test(String name) throws Exception{
        return name;
    }
}

Method 1:

Right click the package, select file path -- > show in files, then open the command line at the current location and enter

javadoc -encoding UTF-8 -charset UTF-8 Doc.java

The middle code is to display Chinese, and then enter the name of the file to be generated

In the generated file, click index Html is enough


Method 2:

Tools --> generate javaDoc

Select the file output directory, etc. in order to process Chinese characters, fill in the column of Other command line arguments

-encoding utf-8 -charset utf-8

Tags: Java Programming

Posted by Dark Phoenix on Mon, 09 May 2022 01:16:14 +0300