javaSE Basic Grammar for java Development

Keywords: Java REST

javaSE Basic Grammar for java Development

1. Keyword and Reserved Word

Keyword Definition: Strings (words) given special meaning by the Java language for specific purposes.

Features: All letters in keywords are lowercase

Java Retention Words: Existing Java versions are not yet in use, but future versions may be used as keywords. Avoid using these reserved words byValue, cast, future, generic, inner, operator, outer, rest, var, goto, const when naming tags yourself

2. Naming Specifications for Identifiers and Names

1) Identifier

The sequence of characters that Java uses to name elements such as variables, methods, and classes is called identifiers.
Every place you can name is called an identifier.

Define the legal identifier rule:

  • It consists of 26 upper and lower case letters, 0-9, or $.
  • Numbers cannot begin.
  • Keyword and reserved word can not be used, but can contain keyword and reserved word.
  • ava is case-sensitive and unlimited in length.
  • The identifier cannot contain spaces.

Note: When naming, in order to improve the reading line, we should try to be meaningful.

2) Naming Specification

Package name: All letters are lowercase when multiple words are formed: xxxyyzzz
Class name, interface name: When multiple words are formed, all words are capitalized: XxxYyZzz
Variable name, method name: When multiple words are formed, the first word is lowercase and the second word begins with capitalization of each word: xxxYyZzz

Constant name: All letters are capitalized. When multiple words are used, each word is underlined: XXX_YY_ZZZ

3. variable

1) The concept of variables

A storage area in memory
The region has its own name (variable name) and type (data type)
Every variable in Java must be declared before it is used
The data of this region can change continuously in the same type range.
Use variables to note:
Scope of variables: valid between pairs of {
initialize value
Define the format of variables: data type variable name = initialization value

Variables access this area by using variable names

2) Basic types of variables

  • Integer types: byte,short,int,long
  • Floating point type: float,double
  • Character type: char
  • boolean type: boolean
  • class: class
  • interface:
  • Array: []

Java integer constants default to int, declaring long constants followed by'l'and'L'

Java's floating-point constants are double by default, declaring float constants, followed by'f'or'F'

boolean type: boolean can only be true or false, not null. False and true cannot be replaced by 0 or non-0 integers.

3. Type Conversion

/*Operations between variables: (boolean is not considered. The rest: char byte short int long float double)
1,Automatic Type Conversion
2,Mandatory Type Conversion
*/
class test
{
    public static void main(String[] args)
    {
        //1. Automatic Type Conversion
        int i1 = 12;
        short s1 = 2;
        int i2 = i1+s1;//Automatic Type Conversion, Small Capacity Data Type Converted to Large Capacity Data Type
        float f1 = 12.3F;
        float f2 = f1 +i2;//Automatic Type Conversion
        double d1 = f2 + 12.3;//12.3 is a double type data, so it is converted to a double type.

        long l = 12;//Small data types can be assigned to large data types.
        float f3 = 1;
        System.out.println(i2);
        System.out.println(f2);
        System.out.println(d1);

        char c1 = 'a';
        int i3 = c1 +1;//Automatic conversion of char type to int type
        System.out.println(i3);

        //Note: When performing operations between char byte short, the default result is int type
        short ss1 = 12;
        byte bb1 = 1;
        char cc1 = 'a';
        //short ss2 = ss1 + bb1; wrong
        int ii1 = ss1 + bb1;
        //char cc2 = cc1 +bb1;
        int ii2 = cc1 + bb1;
        System.out.println(ii1);
        System.out.println(ii2);

        //2. Forced Type Conversion: A data type with large conversion bits and small capacity, using a Forced Type Converter: ()
        //Mandatory Type Conversion: Loss of Precision
        long l1 = 123456L;
        //int m1 = l1; wrong
        int m1 = (int)l1;
        System.out.println(m1);

        byte by1 = (byte)m1;
        System.out.println(by1);//Accuracy loss is produced.

        //The usual string is also a data type: String
        String nation = "I am XiJian";
        System.out.println(nation);
        //Operations between strings and basic data types: only join operations: +, and still get a string
        String str = "abc";
        String str1 = str + m1;
        System.out.println(str1);//abc12345 

    }
}

4. operator

1) Arithmetic operators

//Testing arithmetic operators: + - + - */%+ - ++ +
class TestAri 
{
    public static void main(String[] args) 
    {
        //Except: /
        int i = 12;
        int j = i/5;
        double d = i / 5;
        System.out.println(j);//The value of j is 2.
        System.out.println(d);//The value of d is 2.0
        double d1 = i/5.0;//The value of d1 is 2.4
        //Modeling:%. Symbols of the result depend on the number being touched.
        int i1 = i % 5;//i1 = 2
        int i2 = -12 % 5;//i2=-2
        int i3 = 12 %(-5);//i3=2
        int 14 = -12 % (-5);//i4=-2
        //Front ++: Add one first and then do the operation
        //Later ++: First do the operation and then add one

        int myInt1 = 10;
        int myInt2 = myInt1++;//Post + +
        System.out.println(myInt1);//11
        System.out.println(myInt2);//10

        int myInt3 = 10;
        int myInt4 = ++myInt3;//Post + +
        System.out.println(myInt3);//11
        System.out.println(myInt4);//11
        //Before - --- subtract one first and then do arithmetic
        //Later --: First do the operation, then subtract one.

    }
}

2) Assignment Operator

//Assignment operator: += - = *= /=%=, does not change the original data type
class GetValue
{
    public static void main(String[] args) 
    {
        int i1 = 10;
        i1 += 3;//i1 = i1 +3

        short s = 10;
        s = s+3;//Compiler fails, automatic type converts to int type, but s is short type
        s = (short)(s + 3);//This is the only way to achieve it.
        s+=3;//It can realize operation without changing the data type of s.

    }
}

3) comparison operator

4) Logic Operator

Logic and | Logic or! Logical non

& - Short Circuit and | - Short Circuit or ^ - Logical XOR

//Logical Operator: & & & & | |! ^
class TestLogic 
{
    public static void main(String[] args) 
    {
        boolean a = true;
        boolean b = false;
        //The difference between & and & is: &: Whether true or false on the left, the right side performs operations.
        //             &&: When the left end is false, the right end will not perform the operation, which is short-circuit operation.
        //When used, it is recommended to use &&
        int i1 = 10;
        if(b&(i1++)>0)
        {
            System.out.println("It's sunny today.");
        }
        else
        {
            System.out.println("Rarely without haze");
        }
        System.out.println(i1);//11
        int i2 = 10;
        if(b&&(i2++)>0)
        {
            System.out.println("It's sunny today.");
        }
        else
        {
            System.out.println("Rarely without haze");
        }
        System.out.println(i2);//10

        //The difference between | and |: |: When the left end is true, the right end performs normal operations.
        //                | |: When the left end is true, the right end does not perform operations.
        int i3 = 10;
        if(a | (i3++)>0)
        {
            System.out.println("It's sunny today.");
        }
        else
        {
            System.out.println("Rarely without haze");
        }
        System.out.println(i3);//11
        int i4 = 10;
        if(a || (i4++)>0)
        {
            System.out.println("It's sunny today.");
        }
        else
        {
            System.out.println("Rarely without haze");
        }
        System.out.println(i4);//10
    }
}

5) Ternary Operator

/*
Ternary Operator: (Conditional Expressions)? Expressions 1: Expressions 2;
Expressions 1 and 2 are required to be of the same data type.
        Since it is an operator, there must be the result of the operation. The data type of the result is the same as that of expressions 1 and 2.
*/
class TestSanYuan 
{
    public static void main(String[] args) 
    {
        int i = 10;
        int j = 20;
        //At this point max records the larger values of i and j
        int max = (i > j)? i : j;

        String str = (i > j)? "i large" : "j large";
        //String STR = I > j? I: "j big"; error
        String str1 = (i > j)?"i large" : (i == j)? "Equal": "j large" ;

        System.out.println(max);
        System.out.println(str);
        System.out.println(str1);
    }
}

Posted by Azeryk on Fri, 25 Jan 2019 19:57:14 -0800