JavaSE learning notes 01

1. HelloWorld

Write code

public class Hello{
	public static void main(String[] args){
		System.out.println("Hello,World!");
	}
}

Code explanation

The difference between Public class and class:

  1. Multiple class es can be defined in a java file

  2. Public classes can be empty

  3. If there is any Public class, there can only be one, and the class name and file name should be consistent

System.out.println("Hello,World!");For the output statement, the content with double quotation marks in parentheses is the output content

2. Notes

Comments are not executed. They are for those of us who write code

  1. Single line note://

    //This is a single line comment
    
  2. Multiline comment: / * comment content*/

    /*    This is a multiline comment      */
    
  3. Document comments: / * * comment content*/

    /**
     * @Description HellWorld
     * @Author Most Aegean girl E
     */
    
Interesting code comments
/***
 *                    .::::.
 *                  .::::::::.
 *                 :::::::::::  FUCK YOU
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 */

3. Identifier

  1. //All identifiers should start with a letter, a dollar sign ($), or an underscore
public class Demo01 {
    public static void main(String[] args) {
        String Hero = "Sona ";
        String hero = "janna ";
        String $Hero3 = "Morgana ";
        String _Hero4 = "Leona ";
    }
}
  1. //You cannot start with numbers and other special symbols
// String 1Hero = "jinx";
// String #Hero = "lac silk";
// String *Hero = "jiela";
//All the above are wrong
  1. //The first character can be followed by any combination of letters, dollar characters, underscores, or numbers
  2. //Keywords cannot be used as variable or method names
  3. //Identifiers are case sensitive (different case of the same word can be regarded as two, for example, hero and hero are two different)
  4. You can use Chinese names and Pinyin, but it is generally not recommended. It is very Low

4. Data type

Basic data type

type byte Value range
byte 1 -128~127
short 2 -32768~32767
int 4 -2147483648~2147483647
long 8 ...
float 4 ...
double 8 ...
boolean 1 ...
char 2 0~65535
byte

Bit: it is the smallest unit of data storage in the computer. 11001100 is an eight bit binary number

byte: it is the basic unit of data processing in the computer. It is customarily expressed in capital B

1B (byte) = 8bit (bit)

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

1 bit means 1 bit 1Byte indicates a byte 1B = 8b

1024B = 1KB 1024KB = 1M

1024M = 1G 1024G = 1TB

public class Demo02 {
    public static void main(String[] args) {
        //Eight basic data types
        //integer
        int num1 = 10;//Most commonly used
        byte num2 = 20;
        short num3 = 30;
        long num4 = 30L;//Long type should be followed by an L

        //Decimals: floating point numbers
        float num5 = 50.1F;//For Float type, add an F after the number
        double num6 = 3.1415926;

        //character
        char name = 'A';

        //String, string is not a keyword, class
        String name2 = "Sona ";

        //Boolean: Yes No
        boolean flag = true;//1
        boolean flag2 = false;//0
    }
}
expand
public class Demo03 {
    public static void main(String[] args) {
        //Integer extension: hexadecimal
        //Binary 0b, octal 0, decimal, hexadecimal 0x
        int i = 10;
        int i2 = 010;//Octal 0
        int i3 = 0x10;//Hex 0x 0 ~ 9 A ~ F 16
        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println("=======================================");

        //==================================
        //Floating point expansion, how to express banking business (money)?
        //BigDecimal math tool class
        //==================================
        //float
        //double
        float f = 0.1f;//0.1
        double d = 1.0/10;//0.1
        System.out.println(f == d);//false

        float d1 = 123456789f;
        float d2 = d1 + 1;
        System.out.println(d1 == d2);//true
        //It is best to use floating point numbers for comparison
        System.out.println("=======================================");

        //==================================
        //Character expansion
        //==================================
        char c1 = 'a';
        char c2 = 'in';
        System.out.println(c1);
        System.out.println((int)c1);//Cast type
        System.out.println(c2);
        System.out.println((int)c2);//Cast type
        System.out.println("=======================================");
        //All characters are still numbers in nature

        //Common escape characters
        // \t: Represents a tab
        // \n: Indicates a line break
        // \: represents a character
        // \': represents character'
        // \: represents a character\
        // \r: Indicates enter
        System.out.println("Hello\tWorld!");
        System.out.println("Hello\nWorld!");
        System.out.println("=======================================");
        
        //Boolean extension
        boolean b1 = true;
        if (b1 == true){}//Novice
        if (b1){}//an old hand
        //Less is More!  The code should be concise and easy to read
    }
}
Type conversion
  1. Cast type

    //Cast: (type) variable name high -- low
    int i = 128;
    byte b = (byte)i;//out of memory
    System.out.println(i);//128
    System.out.println(b);//-128
    
  2. Automatic type conversion

    //Automatic conversion: low high
    int i2 = 128;
    double d = (double)i2;
    System.out.println(d);//128.0
    

6 rules for basic type conversion:

​ Article 1: only boolean cannot be converted

​ Article 2: automatic type conversion (small to large)

​ Article 3: mandatory type conversion (from large to small) needs to add a mandatory type converter, which may lose accuracy

​ Article 4: when an integer does not exceed the value range of byte short char, it can be assigned directly

​ Article 5: in the mixed operation of byte short char, they shall be converted into int before operation

​ Article 6: when multiple data types are mixed, first convert to the one with the largest capacity, and then do the operation

Floating point numbers can be forcibly converted to integers. During the transformation, the decimal part of the floating point number will be lost. If the transformation exceeds the maximum range that an integer can represent, the maximum value of the integer will be returned.

 System.out.println((int)23.7);//23
 System.out.println((int)-45.89f);//-45

If you want to round, you can add 0.5 to the floating-point number and force the transformation

double d = 2.6;
int n = (int) (d + 0.5);
System.out.println(n);//3

When operating large numbers, pay attention to the overflow problem

int money = 10_0000_0000;
int years = 20;
int total = money * years;//-1474836480, overflow during calculation
System.out.println(total);

long total2 = money * ((long)years);//First convert a number to long
System.out.println(total2);//20000000000

Reference data type

In addition to the above basic types of variables, the rest are reference types. For example, the most common reference type is String. Different from char type, String type is a reference type. A String can store 0 to any character.

String s = "";//Empty string containing 0 characters
String s1 = "A";//Contains one character
String s2 = "chinese ABC";//Contains 6 characters, including one space

5. Variables

What is a variable: it is a variable

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

Variable precautions:
  1. Each variable has a type, which can be either a basic type or a reference type
  2. Variable name must be a legal identifier
type varName [=value] [{,varName[=value]}];
//Data type variable name = value; 	 You can declare multiple variables of the same type separated by commas
//int a,b,c;
int a = 1,b = 2, c = 3;
String name = "what the fuck";
char x = 'X';
double y = 3.14;
Scope of variable
public class Demo06 {
    //Attributes, variables

    //Instance variable: subordinate to the object. If it is not initialized by itself, this type will be the default value
    //Integer default 0
    //Floating point number defaults to 0.0
    //Boolean: false by default
    //The default value is null except for the basic type
    String name;
    int age;

    //Class variable static
    static double salary = 5000;

    //main method 
    public static void main(String[] args) {
        //Local variables: values must be declared and initialized
        int i = 10;
        System.out.println(i);//10

        //Variable type variable name = new Demo06();
        Demo06 demo06 = new Demo06();
        System.out.println(demo06.name);//null
        System.out.println(demo06.age);//0

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

    }
}

6. Constant

Constant: the value cannot be changed after initialization! Value that will not change. It can be understood as a special variable. After its value is set, it is not allowed to be changed during program operation.

final Constant name = value;

final double PI = 3.14;
//Constant names generally use uppercase characters.
public class Demo07 {
    //Modifier, there is no order
    static final double PI = 3.14;
    final static double PI2 = 3.1415926;

    public static void main(String[] args) {
        System.out.println(PI);//3.14
        System.out.println(PI2);//3.1415926
    }
}
Variable name naming convention
  1. All variables, methods and class names: see the meaning of the name
  2. Class member variables: lowercase initial and hump principle: monthSalary except the first word, the following words are capitalized
  3. Local variables: initial lowercase and hump principle
  4. Constants: uppercase letters and underscores: MAX_VALUE
  5. Class name: initial capitalization and hump principle: Man, GoodMan
  6. Method name: initial lowercase and hump principle: run(), runRun()

Posted by jcran on Wed, 11 May 2022 17:29:44 +0300