Java-3. Basics

Java-3. Basics

1. Notes, identifiers and keywords

notes:

Single line "/ /", multi line "/ * * /", document JavaDoc

Identifier:

  1. Initials: "letters, $,"
  2. Keywords cannot be used as variable or method names
  3. Identifiers are case sensitive
    1. Case sensitive

2 data type

Strongly typed language

It is required that the use of variables shall strictly comply with the regulations and shall be defined before use

Weakly typed language

It is required that the use of variables shall comply with the regulations and shall be defined before use

Java classification

Basic type

\

byte
// Java data types - basic types
public class Demo2 {
    public static void main(String[] args) {
        String a="10";
        System.out.println(a);
        // Integer (note range)
        int num=10;
        System.out.println(num);
        byte num1=10;
        System.out.println(num1);
        short num2=10;
        System.out.println(num2);
        long num3=10L;
        System.out.println(num3);
        // Decimals: floating point numbers
        float num4=40.222F;
        System.out.println(num4);
        double num5=3.1314;
        System.out.println(num5);
        // character
        char name='A';
        System.out.println(name);
        // String, string is not a keyword \ class
        String name1="AAA";
        System.out.println(name1);
        // Boolean value
        boolean flag=true;
        System.out.println(flag);
    }
}
expand
public class Demo03 {
    public static void main(String[] args) {
        // Integer extension: binary [two 0b / eight 0 / ten / sixteen 0x]
        System.out.println(0b10);
        System.out.println(010);
        System.out.println(11);
        System.out.println(0x11);
        System.out.println("------------------------");

        // Floating point extension
        // float: finite, discrete, rounding error, close to but not equal to
        // It is best to use floating-point numbers completely for comparison - > BigDecimal
        float f1=0.1f;
        double d1=1.0/10;
        System.out.println(f1==d1);
        float f2=11111f;
        float f3=f2+1;
        System.out.println(f2==f3);
        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);
        // Encoding Unicode:2 bytes, range: 0~65536[U0000-UFFFF]
        System.out.println('\u0061');
        System.out.println("------------------------");

        // Transfer character
        System.out.println("Hello World");
        System.out.println("Hello\tWorld");
        System.out.println("Hello\nWorld");

        // Boolean extension
        boolean flag = true;
        if(flag==true){
            System.out.println(flag);
        }
        // Same form - > √
        if(flag){
            System.out.println(flag);
        }
    }
}
reference type


3 type conversion

public class Demo04 {
    public static void main(String[] args) {
        // Cast: (type) variable name
        // Requirements: high - > low
        // Automatic conversion:
        // Requirement: low - > High
        // byte,short,char-int-long-float-double
        int i =127;
        // The maximum byte is 127 - > more than: memory overflow
        byte b = (byte)i;
        System.out.println(i);
        System.out.println(b);
        System.out.println((double)i);
        /*
        Note:
        1. Boolean values cannot be converted
        2.Cannot convert an object type to an unrelated type
        3.When converting high capacity to low capacity, force conversion
        4.There may be memory overflow or accuracy problems during conversion
        */
        System.out.println((int)2.34);
        System.out.println((int)-34.2f);
        System.out.println("------------------------");

        char c='a';
        int d=c+1;
        System.out.println(d);
        System.out.println((char)d);
        System.out.println("------------------------");

        // Overflow problem
        // Numbers can be separated by underscores
        int money=10_0000_0000;
        int years=20;
        int total=money*years;
        System.out.println(total);
        long total1=money*years; // The default is int, which is converted to long after conversion, and the type already exists before conversion
        System.out.println(total1);
        long total2=money*((long)years); // First convert a number to long
        System.out.println(total2);
    }
}

4. Variables and constants

variable

The most basic storage unit in the program, including variable name, variable type and scope

Variable scope
public class Demo05 {
    // Class variable static
    static double salary=2300;
    // Attributes: Variables
    // Instance variable: subordinate to the object. The default value is 0/0.1/u0000/ Boolean value false / / other null
    String name;
    int age;

    // main method 
    public static void main(String[] args) {
        // Local variables: values must be declared and initialized
        int a=1,b=2,c=3;
        String name="nihao";
        char x='x';
        double pi=3.14;

        // Variable type variable name = new Demo08();
        Demo05 demo05 = new Demo05();
        System.out.println(demo05.age);
        System.out.println(demo05.name);

        // Class variable static
        System.out.println(salary);
    }
    // Other methods
    public void add(){}
}

constant

Its value cannot be changed after initialization

Format: final constant name (usually in uppercase characters) = value;

Naming conventions for variables

  1. All variables, methods and class names: high readability
  2. Class member variable: lowercase first letter & hump principle (the first word is lowercase, and the following word is uppercase)
  3. Local variable: initial lowercase & hump principle
  4. Constants: uppercase letters & underscores
  5. Class name: initial capitalization & hump principle
  6. Method name: initial lowercase & hump principle

5 operator

Algorithmic operator

​ +,-,*,/,%,++,–

package operator;

public class Demo01 {
    public static void main(String[] args) {
        // Binary operator
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        // Note the range of decimal division
        System.out.println(a/(double)b);
    }
}
package operator;

public class Demo04 {
    public static void main(String[] args) {
        // Unary operators: self increasing + +, self decreasing--
        int a = 3;
        int b = a++; // First b=a, then a=a+1
        System.out.println(b);
        System.out.println(a);
        int c = ++a; // First a=a+1, then c=a
        System.out.println(a);
        System.out.println(c);

        // exponentiation 
        double pow = Math.pow(2,3);
        System.out.println(pow);
    }
}
Assignment Operators

​ =

package operator;

public class Demo02 {
    public static void main(String[] args) {
        long a = 111123123L;
        int b = 123;
        short c = 10;
        byte d = 4;

        // Type conversion: byte\short\char is automatically converted to int after using the operator
        System.out.println(a+b+c+d); // long
        System.out.println(b+c+d); // int
        System.out.println(c+d); // int
    }
}
Relational operator

​ >,<,>=,<=,==,!=instanceof

package operator;

public class Demo03 {
    public static void main(String[] args) {
        // Return result of relational operator: true/false
        int a = 10;
        int b = 20;
        int c = 11;
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
        // Modulo operation
        System.out.println(c%a);
    }
}
Logical operator

​ &&,||,!

package operator;

public class Demo05 {
    public static void main(String[] args) {
        // And & &, or 𞓜, not!
        boolean a = true;
        boolean b = false;
        // The same truth is true and the rest is false
        System.out.println("a&&b="+ (a&&b));
        // The same false is false and the rest is true
        System.out.println("a||b="+ (a||b));
        System.out.println("!(a&&b)="+ !(a&&b));

        // Short circuit operation
        int c = 5;
        boolean d = (c<4)&&(c++<4);
        System.out.println(d);
        System.out.println(c);
    }
}
Bitwise Operators

&, |, ^, ~, > >, < <, > > (understand)

package operator;

public class Demo06 {
    public static void main(String[] args) {
        // Bit operation
        /*
        A = 0011 1100
        B = 0000 1101
        A&&B = 0000 1100
        A|B = 0011 1101
        XOR A^B = 0011 0001
        ~B = 1111 0010
        -----------------------------
        Bit operation: shift left > > shift right<<
        0000 0000   0
        0000 0001   1
        0000 0010   2
        0000 0011   3
        0000 0100   4
        0000 1000   8
        0001 0000   16
        2*8 Fastest algorithm - > 2 * 2 * 2
        */
        System.out.println(2<<3);
        System.out.println(2>>3);
    }
}
Conditional operator

​ ?, :

package operator;

public class Demo08 {
    public static void main(String[] args) {
        // Ternary operator x? y : z
        // If x = = true - > the result is y, otherwise z
        int score = 80;
        String type = score < 60 ? "false" : "true";
        System.out.println(type);
    }
}
Extended assignment operator

​ +=,-=,*=,/=

package operator;

public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        a+=b;
        System.out.println(a);
        a-=b;
        System.out.println(a);
        a*=b;
        System.out.println(a);
        a/=b;
        System.out.println(a);

        // String connector +, string - > converts the other party into a string for connection
        System.out.println(a+b);
        // Note the order of precedence: calculate or connect
        System.out.println(""+a+b);
        System.out.println(a+b+"");
    }
}

6. Package mechanism, JavaDoc

Package mechanism

Namespace used to distinguish class names

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

Format: (must be placed at the top)

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

Use package:

import package1[.package2..].(calssname|*)

Wildcard: * import all classes under the package

import com.kuang.base.*

Alibaba specification manual

JavaDoc

Used to generate API documents

[API help documentation]( Overview (Java Platform SE 8 ) (oracle.com))

parameter
  • @Author author name
  • @Version version number
  • @since indicates that the earliest jdk version is required
  • @param parameter name
  • @Return return value
  • @throws exception thrown
package com.kuang.base;
/**
* @author Kuang
* @version 1.0
* @since 1.8
* */
public class Doc {
    // Instance variable
    String name;
    // method

    /**
     * 
     * @param name
     * @return
     * @throws Exception
     */
    public String test(String name) throws Exception{
         return name;
    }
}
Generate JavaDoc from the command line
javadoc -encoding UTF-8 -charset UTF-8 Doc.java
Find JavaDoc generated using IDEA

Generate JavaDoc documents using IDEA (IDEA2020.3.2)_ Miyonesqn blog - CSDN blog)

Tags: Java

Posted on Sun, 05 Sep 2021 14:21:27 -0400 by beefy23