Java basic syntax (update package mechanism)

Java basic syntax

Note summary from

If you need source code or answer questions, please contact QQ: 1876504704

1, Comments, identifiers, keywords

notes

Writing notes is a very good habit

Comments are not executed. They are for the person who writes the code

There are three types of notes:

Single line comment: "/ /"

Multiline comment: "/ **/“

Document comment: "/ * * /"

You must pay attention to norms when writing code at ordinary times

identifier

keyword

All components of java need names. Class names, variable names, and method names are all called identifiers

be careful

1. All identifiers should be in letters and dollars( ) lower Row Line ( ) , open beginning 2. first word symbol of after can with yes word mother , beautiful element symbol ( )Underline (), Start 2 The first character can be followed by a letter or a dollar sign( )Underline (), start 2 The first character can be followed by a letter, a dollar sign () or an underscore () Or any character combination of numbers
3. Keyword cannot be used as variable name or method name**
4. Identifiers are case sensitive * *
5. It can be named in Chinese, but it is generally not recommended to use it or Pinyin, which is very LOW

package BasicGrammar;
/**
 * //                            _ooOoo_
 * //                           o8888888o
 * //                           88" . "88
 * //                           (| -_- |)
 * //                           O\  =  /O
 * //                        ____/`---'\____
 * //                      .'  \\|     |//  `.
 * //                     /  \\|||  :  |||//  \
 * //                    /  _||||| -:- |||||-  \
 * //                    |   | \\\  -  /// |   |
 * //                    | \_|  ''\---/''  |   |
 * //                    \  .-\__  `-`  ___/-. /
 * //                  ___`. .'  /--.--\  `. . __
 * //               ."" '<  `.___\_<|>_/___.'  >'"".
 * //              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * //              \  \ `-.   \_ __\ /__ _/   .-` /  /
 * //         ======`-.____`-.___\_____/___.-`____.-'======
 * //                            `=---='
 * //        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 * //                     Buddha bless no BUG!
 */
public class Demo1 {
    public static void main(String[] args) {    }
    //Output a Hello,World
    //Single line annotation: only one line of text can be annotated
        //System.out.println("Hello,World");

    //Multiline notes can annotate a piece of text
    /* notes */
        /*
        I'm a multiline comment
        I'm a multiline comment
        I'm a multiline comment
        I'm a multiline comment
         */
    //JavaDoc: document comment "/ * * * /"
    /**
     * @deprecated   HelloWorld
     * @Author  Crazy God says Java
     */


}

2, Data type

Strongly typed language

It is required that the use of variables should strictly comply with the regulations, and all variables must be defined before they can be used

Weakly typed language

The use of variables is required to comply with the regulations. All variables must be defined before they can be used

The data types of Java are divided into two categories

Basic type

value type
Integer type

byte -128-127(1 byte)

short -32768-32767(2 bytes)

int -21474368-214783647(4 bytes)

long (8 bytes)

Floating point type

float (4 bytes)

double (8 bytes)

Character type

char (2 bytes)

boolean type

It occupies 1 bit, and its value is only true or false

reference type

class

Interface

array

byte

The smallest storage unit of bit

The basic unit of data processing

1 B (byte) = 8 bit (bit)

Characters: letters, numbers, words and symbols used in computers

1 G = 1024 M

1 M = 1024 KB

1 KB = 1024 B

package BasicGrammar;

public class Demo3 {
    public static void main(String[] args) {
        //Integer extended binary decimal octal hexadecimal
        int i=10;
        int i2=010;//octal number system
        int i3=0x10;//Hex 0-9 A-F 16
        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        //==============================================================
        //Floating point expansion? What does banking mean? money
        //BigDecimal number toolbar
        //==============================================================
        //The rounding error of float finite discretization is approximately close to but not equal to
        //double
        //It is best to use floating point numbers for comparison
        //It is best to use floating point numbers for comparison
        //It is best to use floating point numbers for comparison
        float f = 0.1f ;
        double d =1.0/10;
        System.out.println(f==d);
        System.out.println(f);
        System.out.println(d);

        float   d1 =215316541651564156f;
        float d2 = d1 + 1;
        System.out.println(d1==d2);
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        //==============================================================
        //Character expansion
        //==============================================================
        char c1 = 'a';
        char c2 = 'in';
        System.out.println(c1);
        System.out.println((int)c1);
        System.out.println(c2);
        System.out.println((int)c2);
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        //All characters essentially look around numbers
        //Encoding Unicode 2 bytes 0 - 65536
        //U0000-UFFFF
        char  c3 ='\u0061';
        System.out.println(c3);
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        //Escape character
        //   \t tab
        //    \n line feed
        // ......

        String sa =new String("hello world");
        String sb = new String("hello world");
        System.out.println(sa==sb);
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        //Object from memory analysis
        String sc ="hello world";
        String sd = "hello world";
        System.out.println(sc==sd);
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        //Boolean extension
        boolean flag =true;
        if (flag == true){  }//Novice
        if (flag){  }//an old hand
        //Less is More!   The code should be concise and easy to read
    }

}

3, Type conversion

Because Java is a strongly typed language, type conversion is required for some operations

In the operation, different types of data are transformed into the same type first, and then the operation is carried out

Low ----- > High

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

Cast type

Automatic type conversion

public class Demo4 {
    public static void main(String[] args) {
        int i= 128;
        byte b = (byte)i ;//out of memory
        //Cast (type) variable name high -- > low
        //Auto convert (type) variable name low -- > High
        System.out.println(i);
        System.out.println(b);
        /**
         * Note:
         * 1.Boolean values cannot be converted
         * 2.Cannot convert object type to irrelevant type;
         * 3.Force conversion when converting high capacity to low capacity
         * 4.There may be memory overflow or accuracy problems during conversion
         *
         *
         */
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println((int)23.7);
        System.out.println((int)-45.89f);
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        char c='a';
        int d =c+1;
        System.out.println(d);
        System.out.println((char)d);
    }
public class Demo5 {
    public static void main(String[] args) {
        //When operating large numbers, pay attention to the overflow problem
        //jdk7 new features. Numbers can be separated by underscores
        int money =10_0000_0000;
        int years =20;
        int total =money*years;
        long total2 =money*years;
        long total3 =money*((long)years);

        System.out.println(total);//Calculation overflow
        System.out.println(total2);//The default is int, and there is a problem before the conversion
        System.out.println(total3);//First convert a number to Long
        //l  L
        }

4, Variable, constant

variable

java is a strongly typed language. Every variable must declare its type

Java variable is the most basic storage unit in the program. Its elements include variable name, variable type and * * scope**

type  varName [=value][{,varName[=value]}];
//Data type variable name = value: you can declare multiple variables of the same type separated by commas

be careful:

1. Each variable has a type, which can be either a basic type or a reference type
2. The variable name must be a legal identifier
3. Variable declaration is a complete statement, so each declaration must end with a semicolon

constant

The value cannot be changed after initialization! Value that will not change.

A constant can be understood as a special variable. After its value is set, it is not allowed to be changed during the operation of the program

Constant names generally use uppercase characters.

Variable naming rules

All variables, methods and class names: see the meaning of the name

Class member variables: initial lowercase and hump principle

Local variables: initial lowercase and hump principle

Constants: uppercase letters and underscores

Class name: initial capitalization and hump principle

Method name: initial lowercase and hump principle

5, Operator

Arithmetic operators: + - * /% + –

Assignment operator=

Relational operators > < < =? == instanceof

Logical operator & & |!

Bitwise operators & |^ ~ > < > > < > >

Conditional operator?:

Extended assignment operator + = - = * =/=

6, Package mechanism, JavaDoc

In order to better organize classes, Java provides a package mechanism to distinguish the namespace of class names

Format:

package pkg1[.pkg2[.pkg3...] ]

Generally, the inverted company domain name is used as the package name

In order to use the members of a package, you need to explicitly import the package in your Java program. Use the "import" statement to complete this function

Format:

import package1[.package2...].(classname |*);

javaDoc

/**
 *  @author   kuangshen Author name
 *  @version   1.0  edition
 *  @since   1.8   The earliest jdk version used
 *  @param   parameter
 *  @return Return value
 *  @throws Exception thrown
 */

//I use the javadoc parameter java file from the command line

//Learn how to find javaDoc documents produced by IDEA! For Baidu programming!

//Everything about the basic part is just that it will be used almost every day later

Keywords: Java

Added by Danny620 on Fri, 04 Mar 2022 06:05:46 +0200