Learning java on the third day

Summarize

delimiter

  • ;Semicolon to indicate the end of the statement
  • () Parentheses, used more, will be discussed later
  • {} curly braces, code blocks, represent a whole
  • [] Square brackets, used when defining an array
  • . dot, access symbol
  • space, delimited
  • "" string, representing a string

keywords

  • Words that give special meaning when designing the Java language

reserved word

  • Meaningless keywords reserved by Java (true, false, null)

identifier

  • concept:

    1. The names of classes, variables, and methods can all be called identifiers
  • Regulation:

    1. All identifiers should start with a letter, underscore, $, not a number
    2. Keywords cannot be used as identifiers
  • Writing Specifications:

    1. Identifiers are generally not recommended to start with $ and _ (Ali specification)
    2. General identifiers are known by name
    3. The first letter of the class name is usually capitalized, and then camel case (HelloWorld)
    4. The first letter of the method name is usually lowercase, and then camel case (niHao)

constant

  • a fixed value

    package day0930;
    
    public class General {
    
    	public static void main(String[] args) {
    		//Integer constant
    		System.out.println(23);//23
    		
    		//floating point constant
    		System.out.println(23.02);//23.02
    		
    		//boolean constant
    		System.out.println(false);//false
    		
    		//character constant
    		System.out.println('A');//A
    		
    		//string constant
    		System.out.println("nihao");//nihao
    	}
    
    }
    
    

type of data

  • basic data type

    1. Integer type: byte(8), short(16), int(32), long(64)

      example:

      package day0930;
      
      import java.math.BigInteger;
      
      public class Number {
      
      	public static void main(String[] args) {
      		//The integer type uses the int type by default
      		// byte type
      		// byte b = 128;//Error, more than [-128,127]
      		byte b = 65;
      		System.out.println(b);// 65
      
      		// short type
      		short s = 256;
      		System.out.println(s);// 256
      
      		// int type (maximum value: 2147483647)
      		int i = 2147483647;
      		System.out.println(i);// 2147483647
      
      		int i1 = i + 1;// Out of scope, but the compilation only looks at the type and does not perform the result operation
      		System.out.println(i1);// -2147483648, which is calculated by one's complement complement
      
      		// long type
      		long l = 1231231L;// Since each integer is of type int, to change to type long, add L;
      		System.out.println(l);// 1231231
      
      		// BigInteger type (used to represent numbers with a larger range than the long type)
      		BigInteger big = new BigInteger("213214214124");
      		System.out.println(big);
      	}
      
      }
      
      
      package day0930;
      
      public class Number1 {
      
      	public static void main(String[] args) {
      		//java default decimal
      		//binary representation decimal
      		int i = 0b1000;
      		System.out.println(i);//8
      		
      		//Octal means decimal
      		int i1 = 037;
      		System.out.println(i1);//31
      		
      		//Hexadecimal for decimal
      		int i2 = 0x1A;
      		System.out.println(i2);//26
      	}
      
      }
      
      
    2. Floating point: float (32), double (64)

      example:

      package day0930;
      
      import java.math.BigDecimal;
      
      public class Decimal {
      
      	public static void main(String[] args) {
      		//Floating point type is double type by default
      		//float type
      		float f = 11.02F;//Indicates the float type, with an F at the back
      		System.out.println(f);//11.02
      		
      		//The floating point type represents a decimal, which is infinitely close to this number (different precision)
      		float f1 = 1.0F/3.0F;
      		System.out.println(f1);//0.33333334
      		
      		double d = 1.0/3.0;
      		System.out.println(d);//0.3333333333333333
      		
      		double d1 = 1.0/0.0;
      		System.out.println(d1);//Infinity
      		
      		double d2 = 0.0/0.0;
      		System.out.println(d2);//NaN
      		
      		//more accurate method
      		BigDecimal b1 = new BigDecimal("1.3");
      		BigDecimal b2 = new BigDecimal("1.4");
      		BigDecimal b3 = b1.multiply(b2);
      		System.out.println(b3);//1.82
      		
      	}
      
      }
      
      
    3. Boolean: boolean(1) (false, true)

      example:

      package day0930;
      
      public class Boolean {
      
      	public static void main(String[] args) {
      		//boolean type
      		boolean b1 = true;
      		System.out.println(b1);//true
      		
      		boolean b2 = false;
      		System.out.println(b2);//false
      	}
      
      }
      
      
    4. Character type: char(16)

      example:

      package day0930;
      
      public class Char {
      
      	public static void main(String[] args) {
      		//character type
      		char c = 'you';//can be a Chinese character
      		System.out.println(c);//you
      		
      		//Chinese character representation
      		char c1 = 65;
      		System.out.println(c1);//A
      		
      		//Characters represent Chinese characters
      		int i = 'A';
      		System.out.println(i);//65
      		
      		//character in hexadecimal
      		char c2 = '\u0041';
      		System.out.println(c2);//A
      		
      		//Note that char cannot have empty characters, but can have space characters (String can have empty strings)
      		char c3 = ' ';
      		System.out.println(c3);// 
      	}
      
      }
      
      
  • reference data type

    1. kind

      String class example:

      package day0930;
      
      public class StringTest {
      
      	public static void main(String[] args) {
      		//About String
      		String str = "Hello!!";
      		System.out.println(str);
      		
      		String str1 = 65 + "haha" + 23;
      		System.out.println(str1);//65haha23
      		
      		String str2 = 65 + 23 + "haha";
      		System.out.println(str2);//88haha
      		
      		String str3 = "haha" + 65 + 23;
      		System.out.println(str3);//haha6523
      		
      		String str4 = 'A' + 5 + "haha";
      		System.out.println(str4);//70haha
      		
      		String str5 = 'A' + "haha" + 5;
      		System.out.println(str5);//Ahaha5
      		
      		String str6 = "";
      		System.out.println(str6);//This means the empty string
      	}
      
      }
      
      
    2. interface

    3. enumerate

    4. array

    5. annotation

variable declaration method

package day0930;

public class Variable {

	public static void main(String[] args) {
		//Assignment while declaring a variable
		int i = 12;
		System.out.println(i);
		
		//Declare variables first, then assign
		int j;
		j = 20;
		System.out.println(j);
		
		//Declare multiple variables at the same time
		int n, m;
		n = 10;
		m = 20;
		System.out.println(n + "," + m);
		
		int x=30,y=60;
		System.out.println(x + "," + y);
	}

}

Difference between member variable and local variable

  • Same point

    1. are used to store data

  • difference

    1. declared location

      Member variables: declared in the class structure

      Local variables: declared in the method body

    2. Scope

      Member variables: apply to the entire class

      Local variables: used in the method body

    3. About default values

      member variable (with default value when not assigned)

      ​ Integer type 0

      ​ Floating point type 0.0

      ​ character type empty

      ​ String type null

      ​ boolean type false

      Local variables (no default value, must be assigned before being used)

  • static

    1. When static modification, jvm will give priority to processing
    2. The main method needs to call the member variable, and the member variable should be modified with static

Tags: Java

Posted by kday on Fri, 13 May 2022 16:52:44 +0300