Java basic syntax

Posted by TobesC on Sun, 16 Jan 2022 11:08:13 +0100

Java basic syntax

notes

Annotation is a description of program functions, marked with a specific symbol, and the program will not execute annotation during operation.

//Single-Line Comments 

/*
multiline comment 
*/

/**
 *Document comments can be prompted when called
 */

keyword

Definition: a string given special meaning by the Java language and used for special purposes.
Features: all letters in the keyword are lowercase

identifier

Definition: the character sequence used by Java when naming various variables, methods, classes and other elements becomes an identifier.

Define legal identifier rules:
● it consists of 26 English letters in case, 0-9_ Or $cannot start with a number.
● keywords and reserved words cannot be used, but they can be included.
● Java is strictly case sensitive and has unlimited length.
● the identifier cannot contain spaces.
● note: when naming, in order to improve reading, try to be meaningful,
"See the name and know the meaning".

Naming conventions in Java:
Package name: all letters are lowercase: xxyyzz
Class name and interface name: all words are capitalized: XxYyZz
Variable name and method name: the first word starts with lowercase, and the second word starts with uppercase: xxYyZz
Constant name: all letters are capitalized, and multiple words are connected with an underscore_ YY_
ZZ

Java basic data type

Basic data type

  • Integer type: byte, short, int, long
  • Floating point type: float,double
  • Character type: char
  • boolean type: boolean

The integer constant of Java language is int by default. Declaring a long constant can be followed by 'l' or 'l'
The floating-point constant of Java language is double by default. Declaring a floating-point constant can be followed by 'f' or 'f'

public class Demo1 {
    public static void main(String[] args) {
        byte b = 12;//1 byte
        short s= 30;//2 bytes
        int i = 100;//4 bytes
        long l = 100L;//8 bytes
        float f = 100.8888F;//4-byte single precision  
        double d = 1000.5555;//8-byte double precision
        boolean t=true;//boolean type only allows values of true and false
        char c = 'a';//2 bytes, which can cover the characters of all written languages in the world
        System.out.println(c+2);//Char type can be used for operation, because char type right pairs the corresponding value in the character encoding table
    }
}

Basic data type conversion

You can transition from any primitive type to another primitive type in Java
Exception: boolean cannot be converted to other data types
When there are multiple types of data mixed operations, the system will automatically convert all data into the data type with the largest capacity, and then calculate
------------
Default conversion: data types with small capacity are converted to data types with large capacity by default. The data types are sorted from small to large as follows:
byte ,short, char-> int -> long ->float->double
Cast: when converting a data type with large capacity to a data type with small capacity, a cast character shall be added, but it may reduce the progress or overflow.

public class Demo3 {
    public static void main(String[] args) {
        byte b = 10;
        int i = b;//byte to int default conversion
        
        long l = c;
        float f = l;//float is greater than long 
        
        int x = 258;
        byte y = (byte)x;//Converting int to byte cast data may overflow

        float z = 10.5F;
        int ff = (int) z;//Convert float to int
        System.out.println(ff);//The accuracy is reduced

    }
}

Reference data type

  • class
  • Interface interface
  • Array ([])

Classl code

Unicode: a code that includes all the symbols in the world. The number ranges from 0x000000 to 0x10FFFF (hexadecimal). There are more than 1.1 million. Each character has a unique Unicode number. This number is generally written in hexadecimal, preceded by U +.

operator

The following operators are supported in the Java language

  • Arithmetic operators: +, -, *, /,%, + +, –
  • String concatenation operator:+
  • Relational (comparison) operators: >, <, > =, < =, = ==
  • Logical operators:!, &, |, & &||
  • Assignment operators: =, + =, - =, * =/=
  • Conditional operator
  • Bitwise Operators

&When the expression is false, the following expression will still be evaluated
&&When false occurs in the expression, the following expression will not be evaluated
|When the expression appears true, the following expression will still be evaluated
||When the expression appears true, the following expression will still be evaluated

public class Demo2 {
    public static void main(String[] args) {
        int a =10;
        int b=5;
        int c=7;
//      System.out.println(a<b & ++b<c);
//      System.out.println(b);// The following expression executes b=6
        System.out.println(a<b && ++b<c);
        System.out.println(b);//The following expression does not execute b=5



    }
}

Conditional operator
● (conditional expression)? Expression 1: expression 2;
● the result of conditional expression is true, and the result after operation is expression 1; Is false, and the result of the operation is expression 2;

public class Demo4 {
    public static void main(String[] args) {
        int a=10;
        int b=5;
        String res=(a>b)?"establish":"Not established";
        System.out.println(res);//The result is true
    }
}

Bitwise Operators

Console input

import java.util.Scanner;
/*
Input in the console only in Java learning
 */
public class Demo6 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("Please enter an integer");
        int num=scanner.nextInt();//IO blocking input from the console can only be an integer. Enter to end the input

        Scanner string=new Scanner(System.in);
        System.out.println("Please enter a string");
        String s= string.nextLine();

        System.out.println(num);
        System.out.println(s);
    }
}

Topics: Java Back-end