Neusoft training JavaSE day01

Posted by mbeals on Sun, 16 Jan 2022 12:41:27 +0100

Environment configuration

JAVA_HOME=C:\Program Files\Java\jdk1.8.0_73
PATH=....;%JAVA_HOME%/bin

JDK, JRE, JVM relationship

  • JDK(Java Development Kit)

    Java development and compilation tools include all tools for developing Java programs, such as javac and Java. JRE is included in JDK.

  • JRE

    Java running environment. If you want to run Java programs, you need the support of JRE, which contains JVM.

  • Java virtual machine

    Referred to as jvm, it is a virtual computer running all Java programs, just like a simulator. (the program is running in the jvm)

JVM is the running environment of Java language and one of the most attractive features of Java. The JVM is used to read and process compiled platform independent bytecode (class) files to achieve java portability (cross platform).

Note that the Java virtual machine (JVM) is not cross platform. In other words, you have to install the Win version of the JVM under Win and the Linux version of the JVM under Linux.

Execution process

Computer shortcuts

ctrl + c

ctrl + v

ctrl + s

ctrl + z

ctrl + x

ctrl + a

win + e

win + r

alt + tab

...

Eclipse common shortcuts

//main + alt + / + enter create main method quickly

//syso + alt + / + enter write printing method quickly

//shift + alt + a turns multiline editing on or off

//ctrl + alt + ↓ copy one line and insert the next

//ctrl + alt + ↑ copy a line and insert the previous line

//ctrl + d to delete the line

Identifier

Symbols used to compose code, such as class name, method name, variable name, etc. In order to enhance the readability of the program during programming, the defined names must be meaningful. These names are called identifiers.

Attention

  • Consists of letters, numbers, underscores (_)$ Composition, but cannot start with a number.

  • Case sensitive.

  • Keywords and reserved words cannot be used.

  • The class name in the Java API is not recommended as its own class name.

A meaningful English word that uses a non keyword and reserved word cannot afford a non mainstream name.

data type

classification

  • Basic data type

    • integer

      • byte
      • short
      • int
      • long
    • float

      • float
      • double
    • character

      • char
  • unicode mapping table used in the JVM

    • Essence or integer
  • It can mark up to 2 Symbol bytes [0 to 65535]

  • boolean

  • Reference data type (object type)

Type conversion

type

  • automatic
  • force

Overflow problem

TODO

array

meaning

Collection of the same data type

One dimensional array

grammar

  • Type [] name = {value1,..., valueN}
  • Type [] name = new type [] {value1,..., valueN}
  • Type [] name = new type [size];
  • Type [] name;

Two dimensional array

meaning

Each element of a one-dimensional array is a one-dimensional array

definition

  • Type [] [] [] [] name = {{v1,..., vn},{v1,..., vn},{v1,..., vn},...};
  • Type [] [] [] [] name = new type [] [] {v1,..., vn},{v1,..., vn},{v1,..., vn},{v1,..., vn},...};
  • Type [] [] [] [] name = new type [Size1] [size2]// Size1 is required, size2 is not required
  • Type [] [] [] name;

Variable parameters

grammar

Used in the parameters of a method, it is essentially an array

requirement

  • A variable parameter can only be the last parameter in a method parameter
  • There can be at most one variable parameter in a method

case

public class _02ArrayMethod {
	
	public static void main(String ... args) {
		//Essentially, an empty array is passed
		say();
		
		say("first");
		
		say("first","second");
	}
	
	public static void say(String ... content ){
		System.out.println(Arrays.toString(content));
	}
}

Arrays

meaning

JDK provides a tool class for manipulating data

common method

  • sort
  • toString
  • copyOf
  • binarySearch

case

public class _04Arrays {
	public static void main(String[] args) {
		int [] ages = {1,2,3,6,4};
		
		//Arrays.toString(ages);
		
		//Bubble sort select sort
		
		//Half search
		
		//sort
		Arrays.sort(ages);
		
		//[1, 2, 3, 4, 6]
		//System.out.println(Arrays.toString(ages));
		
		//Half search
		int index = Arrays.binarySearch(ages, 4);
		
		System.out.println(index);
		
	
		//If the length is not enough, the default value of element type is used for filling
		int temp [] = Arrays.copyOf(ages, 6);
		System.out.println(Arrays.toString(temp));
	}
}

String

meaning

An immutable string is stored as an array of essential characters.

common method

 public class _05String {

	public static void main(String[] args) throws InterruptedException {
		//String
        
        //meaning
        //Immutable string. Once an object is created, it cannot be modified
        //The essence is stored in character array
		   
		//common method
         
		String name = "  javaHello  ";
		 
		//Get length
		System.out.println(name.length());
		
        //Capitalize
		System.out.println(name.toUpperCase());
        //Turn lowercase
		System.out.println(name.toLowerCase());
		
		//Remove the left and right spaces
		System.out.println(name.trim());
		
		
		//97 at the first occurrence of the string
		System.out.println(name.indexOf(97));
		
        //97 at the last occurrence of the string
		System.out.println(name.lastIndexOf(97));
		
		//From what position to the back for interception
		String result = name.substring(2);
		System.out.println(result);
		
		//From what location to what location to intercept
		result = name.substring(2,6);
		System.out.println(result);
		
		
		//replace
		result = name.replace("java", "android");
		System.out.println(result);
		 
        //cutting
		String content = "a,b,c,d";
		System.out.println(Arrays.toString(content.split(",")));
		
		//Include substrings
		content.contains("a,b");
	}
}

Process oriented

meaning

Each function is completed by function. When a function is completed, it can be called next to the function

object-oriented

meaning

As the name suggests, this idea is to think from the perspective of objects. We reasonably put multiple functions into different objects, emphasizing objects with certain functions.

advantage

  • Object oriented is more in line with normal thinking
  • Object oriented is suitable for large-scale project development, which is convenient for maintenance and expansion

Attention

  • Never misunderstand that object-oriented design is better than process oriented design

  • Object oriented is based on process oriented, and can never replace process oriented

  • The object-oriented method is process oriented.

class

meaning

It is to create an object template, which is abstract

In object-oriented development, existing classes have objects.

form

0-N behaviors

0-N features

grammar

Modifier  class Class name{   
    0-N Behavior
        
    0-N Characteristics
}

Attention

  • The class name is a legal identifier. It is recommended to capitalize the first letter of the word

object

meaning

Concrete embodiment of class

Physical existence

basic operation

  • create object
  • Print object
  • Call the method of the object
  • Modify / get object variables
  • Object comparison
  • Recycle object
public class User {
	String name;
	int age;
	
	public void say(){
		System.out.println("test");
	}
	
	//Entrance method
	public static void main(String[] args) {
		
		//Create object new class name ([parameter 1,..., parameter N]); Immediate object [anonymous object]
		//Create object class name object name = new class name ([parameter 1,..., parameter N]);  
		
		User zhangsan = new User();
		
		//Call method object Method name ([parameter 1,..., parameter N]);
		zhangsan.say();
		
		//Modify the member variable object Member variable = new value;
		zhangsan.age = 10;
		
		//Gets the member variable object Member variables;
		System.out.println(zhangsan.age);
		
		//Printing objects is essentially printing objects toString() result
		System.out.println(zhangsan);
		
		 
		System.out.println(zhangsan.toString());
		
		//Each time an object is created, a new space is opened up in the heap, and the object name points to the space.
		//Object comparison
		User ls = new User();
		User ww = new User();
		
		//== 
		   //If it is used in the basic data type to compare whether the contents are the same
		   //If the reference data type is used to compare whether the memory addresses are the same
		
		System.out.println(ls==ww);
		
		//Object recycling
		   //There is a low priority thread [background task] in the JVM, which will recycle objects that are not referenced. GC
		   //When GC reclaims an object, it will call the finalized method of the object first
		
		ls = null;
		
		//Manually trigger gc operation
		System.gc();
	}
	
	@Override
	protected void finalize() throws Throwable {
		System.out.println("I was recycled...");
	}
}

constructor

meaning

A special method used to create and initialize objects and return objects

grammar

Modifier class([parameter list]){
    //Method body
}

Attention

  • Creating a class will automatically have a default constructor. Once it is displayed to the constructor, the default will be invalid
  • The constructor name is the same as the class name
  • No return type
  • You don't need to return any data
  • Constructors can be overloaded

case

public class Role {
	
	//effect
	   //Create the object and return the object, and initialize the information of the object when creating the object
	
	//grammar
	
	/*
	   
	 Modifier class ([parameter list]){
	    //Method body
	 }
	 
	 */
	
	
	String content;
	
	
	public Role(){
		
	}
	
    public Role(String name){
    	content = name;
	}
     
	//Attention
	/*
	  
	- Creating a class will automatically have a default constructor. Once it is displayed to the constructor, the default will be invalid
	- The name is the same as the constructor
	- No return type
	- You don't need to return any data
	- Constructors can be overloaded
	
	*/
	  
	public static void main(String[] args) {
		Role one = new Role();
		
		Role two = new Role("test");
		System.out.println(two.content);
	}
}

default constructor

  • After defining a class, it comes with a default constructor
  • Without parameters
  • The scope modifier is the same as that of the class
  • If the constructor is specified in the display, the default will be invalidated

method

meaning

Complete a collection of function codes

definition

Modifier return type method name([Parameter class table]){
    Method body
    [return Corresponding type of data;]
}

case

public class _08Method {
	
	public static void main(String[] args) {
		
	}
	
	public int cal(int a,int b){
		return a+b;
	}
	
	public void say(){
		System.out.println("test");
	}
}

Method overloading

meaning

There are multiple methods with the same name, but the parameters are different [type and number], which has nothing to do with the return type of the method.

Case list

public class _09MethodOverload {
	public static void main(String[] args) {
		
	}
	
	//Different parameters
	   //type
	   //quantity

	//There are multiple methods with the same name, but the parameters are different
	
	public int cal(int a,int b){
		return a+b;
	}
	
    /*	
    public long cal(int a,int b){
		return a+b;
	}*/
	
	public float cal(float a,float b){
		return a+b;
	}
	
	public int cal(int a,int b,int c){
		return a+b+c;
	}
}

task

  • Bubble sorting

  • Select sort

  • Binary search

  • Practice String and Arrays related methods

  • Think about the difference between process oriented and object-oriented

  • Thinking constructor

  • Think about the relationship between classes and objects

Topics: Java