java learning notes -- 1 (dark horse programmer at station b)

Posted by kaser on Wed, 05 Jan 2022 10:41:51 +0100

2021/12/28

1. Common DOS commands

The command of DOS operating system is a disk oriented operation command, mainly including directory operation command, disk operation command, file operation command and other commands. Used to operate files in the operation window

  • Open the command prompt window (1. Shortcut: win+R; 2. Enter cmd; 3. Enter)
  • Common operations
operationexplain
Drive letter +:Enter other disks, such as e:
dirView the directory under the current path
cd + directoryEnter unipolar directory
cd+...Exit unipolar directory
cd \ directory 1 \ directory 2Enter multi-level directory
cd +\Exit all directories
clsClear screen
exitExit window

2. Configure the path environment variable

-Q:why do you want to configure it? What is path??

  • Ans: because if we use the dos command to enter a folder, we have to keep cd too much trouble. With path, we can easily use javac and Java commands.

-How to match?
1. Enter configuration settings

2. Configure JAVA_HOME variable

3. Configure the path variable

4. Check whether the configuration is successful (directly enter javac in the dos window to see if the message appears)

3.Hello World case (Notepad)

  • Three steps of developing Java program: writing program, compiling program and running program;
  • step
    1. Create a new text document with notepad and change the name to Hello world java
    2. Write the program inside
    3. Run titi in dos window


I don't know why I can't run it or use idea

2021/12/29

4. Notes

  • Single line comment format: / / comment information
  • Multiline comment format: / comment information/
  • Document comment format/****/

5. Keywords

What is it? A word given special meaning by the java language
characteristic? All letters of keywords are lowercase; General keywords have color in the interpreter, which is very special;

6. Constant

What is it? The amount whose value cannot be changed during program operation;

constant type explain
character stringeg enclosed in double quotation marks: "nihao"
integerint
decimalfloat
characterchar enclosed in single quotation marks
Booleanbool true false
emptynull

7. Data type

What is computer storage? The smallest information unit of computer equipment is called "bit"; The smallest storage unit of computer equipment is "Byte";
Data types in Java? Java is a strong data type. It gives a clear data type for each kind of data. Different data types also allocate different storage space, so the data size is also different;

8. Variables

What is it? In essence, a variable is a small piece of the internal order.
component? Data type + variable name + variable value; eg: char a =10;
Use of variables? Value and modified value;
**Precautions** When using the long type, the number beyond the int range should be followed by an "L". In order to prevent the decimal type from being incompatible, float should be followed by an "F";

9. Identifier

What is it? Symbols that give names to classes, methods, variables, etc;
matters needing attention?

  • Components: numbers, letters, underscores and dollar symbols;
  • Cannot start with a number;
  • Cannot be keyword;
  • Case sensitive;

appointment?

  1. When the identifier is a word, the first letter is lowercase; For example: name; (small hump)
  2. Identifiers are multiple letters. The first letter is lowercase and the first letter of other words is uppercase; For example: firstName; (small hump)
  3. When the identifier is a word, the first letter is capitalized; For example: Name; (large hump)
  4. The identifier is a plurality of letters, and the first letter of each word is capitalized; For example: FirstName; (large hump)

//The small hump naming method is for methods and variables; The big hump is named for the class;

10. Type conversion

Comparison of various data types: byte < short = char < int < long < float < double
Automatic type conversion: assign a value or variable representing a small range to another variable representing a large data range; For example: double d = 10;
Forced data type conversion: assign a value or variable representing a large range to another variable representing a small data range; Format: target data type variable name = (target data type) value or variable; For example: int k =(int)88.88;

11. Operator

What is it? Operator: a symbol that operates on constants or variables; Expression: an expression that links constants and variables with operators. Different operators reflect different expressions;
Class? Arithmetic operator, assignment operator, self increasing and self decreasing operator, relational operator (due to conditional judgment), logical type (connection relational operator), ternary operator;
special
1. The addition and subtraction of characters is the numerical addition and subtraction of ascll code of characters, and can perform arithmetic operation with integer floating-point numbers;

2. String addition is a splicing operation, and the operation is sequential;
3. Ternary operator: Format: relational expression? Expression 1: expression 2; Example: a > b? a:b;

2021/12/31

12. Data input

Data input class: Scanner;
.
Steps:
1. import java.util.Scanner
2. Create object * "Scanner sc = new Scanner(System.in);"* 3 . Receive data "int i = sc.nextInt();"

Case: three monks are taller than each other

13. Branch statement

1.The process statement: in the program, the execution order of each statement has a direct impact on the result of program execution. We realize the function of the program by controlling the execution of statements. The most common process: top-down sequential execution;
2. Classification: sequential structure (from top to bottom); Branch structure (if,switch); Loop structure (for, while, do... while);
3.switch format and Description:

switch(expression){   ///Expressions can be byte, short, int, char, enumeration;
	case Value 1:  //case is followed by the value compared with the expression.
		Statement body 1;  
		break;  //Indicates an interrupt and ends the switch statement;
	case Value 2:
		Statement body 2;
		break;
	...
	default:  //Execute default when all case s do not match; (default)
		Statement body n+1;
		[break;]
		
}

4. Statement format of for loop

for(Initialization statement;Conditional judgment statement;Conditional control statement){
	Loop statement body;
}

5.while loop statement format

while(Conditional judgment statement){
	Loop body statement;
	Conditional control statement;
}

6.do... while statement format

do{  //At least once 
	Loop body statement;
	Conditional control statement;
}while(Conditional judgment statement);

3. Case:
(if) Xiao Ming's reward:

(switch) spring, summer, autumn and winter

import java.util.Scanner;
public class ChunXiaQiuDong {
    //Input the month from the keyboard and output the season of this month;
    //Spring: 345 summer: 678 autumn: 9 10 11 winter: 1, 2, 12;
    public static void main(String[] args) {
        System.out.println("Please enter a month");
        Scanner sc = new Scanner(System.in);
        int month = sc.nextInt();
        switch (month){
            case 3:
            case 4:
            case 5:
                System.out.println("summer");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println("autumn");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println("winter");
                break;
            case 12:
            case 1:
            case 2:
                System.out.println("spring");
                break;
            default:
                System.out.println("Wrong");

        }

    }
}

(for) output data

(for) number of daffodils:

(while) Mount Everest

2022/1/1

14. Jump control statement

  • Continue: skip a loop in the loop and continue to execute the next loop
  • break: directly terminate the entire loop
    difference:

15.Random

The - used to generate a random number;
Use steps:
1. Guide Package: "import java.util.Random;";
2. Create an object: "Random r = new Random();"**
3. Obtain the random number * *: "int x = r.nextInt(10)";


Example: number guessing program

import java.util.Random;
import java.util.Scanner;
public class GassNum {
    public static void main(String[] args) {
        //The program automatically generates a number from 1 to 100. Use the program to guess what the number is;
        Random r = new Random();
        int n = r.nextInt(101)+1;
        System.out.println("Please enter the number you guessed:");
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        while (x!=n){
            if (x<n){
                System.out.println("Guess small, please re-enter:" );
                x = sc.nextInt();
            }
            else {
                System.out.println("If you guessed big, please re-enter:");
                x = sc.nextInt();
            }
        }
        System.out.println("You guessed right, ha ha, this number is:"+x);

    }
}

16. Shortcut keys in idea

  • Quick generate statement:
    1. Quickly generate the main method: "psvm" + enter;
    2. Quickly generate output statement: "sout h" + enter;
  • Content assist key:
    1. Ctrl + Alt + space (content prompt, code completion);
  • Shortcut keys:
    1. Single line comment: select the code and press Ctrl+/
    2 multi line comment: select the code, Ctrl+Shift+/
    3 format: Ctrl+Alt+L

Operation related to Idea: dark horse 57

17. Array definition format

1. Requirements: declare a large number of stored data variables at one time; And the stored data are the same type of data;
2. Definition: used to store multiple data storage models of the same type;
3. Definition format: format 1: data type [] variable name; Format 2: data type variable name [];

public class ShuZu {
    public static void main(String[] args) {
        //Two definitions of arrays
        
        int[] arr; //Format 1 recommendation
        //An array of int type is defined, and the array name is arr;
        
        int lrr[];
        //Defines a variable of type int, whose name is lrr array;
    }
}

4. Array initialization method

  1. Dynamic initialization: only the array length is specified during initialization, and the initial value is allocated by the system;
    Format: data type [] variable name = new data type [array length];
//dynamic initialization
        int[] arr = new int[10];
        /*Left:
                int:Indicates that the elements in the array are of type int;
                [ ]:This is an array;
                arr:This is the name of the array;
          right:
                new:Apply for memory space for the array;
                int:Note that the element type in the array is int type;
                [ ]:This is an array;
                10:Note that the length of the array is actually the number of elements in the array; 
                */
  1. Static initialization: specify the initial value of each element during initialization, and the array length is determined by the system;
    Format: data type [] variable name = new data type [] {data 1, data 2,...};
//initiate static
        int[] arr = new int[]{1,2,3,6};
        int[] lrr = {1,2,3,6};  //Abbreviation format

Get the number of elements in the array: arr.length;

18. Methodology

1. What is it?
Type in function is a code set that organizes code blocks with independent functions into a whole and has unique functions;
2. Precautions?
Methods must be created before they can be used. This process becomes the definition of methods;
The creation of a method does not run directly. It needs to be executed manually and becomes a method call;
Method is under class;
3. Definition of method:
Format:

public static Return value type method name(parameter){ 
       Method body;
       return data;
    }
/*
	public static   Modifier 
	Return value type the data type of the data returned after the operation of the method;
	Method name 		 Identification of calling method;
	parameter 			 It is composed of data type and variable name, and multiple parameters are separated by commas;
	Method body 			 Code block when the function is completed;
	return			For return;
*/

4. The simplest method call of the example (judge whether it is even)

public class FangFa {
    public static void main(String[] args) {
        isEventNumber();//Invoke the method in the main function;
    }

    public static void isEventNumber(){
        //Define a method to judge whether a number is even or not
        int number = 10;
        if(number%2==0){
            System.out.println(true);
        }
        else {
            System.out.println(false);
        }
    }
}

Example 2: judge the size of two numbers

public class MaxNum {
    public static void main(String[] args) {
        getMax();
    }
    public static void getMax() {
        int a = 100;
        int b = 200;
        System.out.println(a>b?a:b);
    }
}

5. Method definition with parameters

public static void Method name(parameter) {  //Format of method with parameters
        Method body
    }
    public static void Method name(Data type variable name) {  //Single parameter
        Method body
    }
    public static void Method name(Data type variable name 1 ,Data type variable name 2) { //Multiple parameters
        Method body
    }

Example (single parameter) judge even number

public class FangFa {
    public static void main(String[] args) {
        int x = 10;
        isEventNumber(x);
    }
    public static void isEventNumber(int number){
        //Define a method to judge whether a number is even or not
        if(number%2==0){
            System.out.println(true);
        }
        else {
            System.out.println(false);
        }
    }
}

Example (multiple parameters) judge the size of two numbers

public class MaxNum {
    public static void main(String[] args) {
        getMax(158,692); //Argument
    }
    public static void getMax(int a,int b) {  //Formal parameter
        System.out.println(a>b?a:b);
    }
}

6. Call of method with return value

public static Data type method name(parameter) {
        return data;  //The return value after return must be consistent with the data type on the method
    }
//The return value of a method is usually a variable defined in the main function

be careful:
The method is that the horizontal relationship cannot be nested, that is, the method cannot be nested inside the method;
void indicates that there is no return value. Return can be omitted or written separately without data;
The modification of the formal parameter will not affect the value of the argument, but for parameters of reference type (such as array type), the formal parameter will affect the value of the argument;

19. Overloading of methods

What is it?
Multiple methods are defined in the same class. The relationship between these methods meets the following requirements:
1. Multiple methods in the same class
2. Multiple methods have the same method name;
3. Multiple methods have different parameters, types and quantities;

characteristic?
1. Overloading only corresponds to the definition of the method and has nothing to do with the call of the method;
2. Only the method name and parameters in the same class are identified, which has nothing to do with the return value. It is not possible to judge whether the method is overloaded by the return value;
3. When calling, the java virtual opportunity distinguishes the methods with the same name through different parameters;

Why use method overloading?
This is one of the three characteristics of java, which is a form of polymorphism; Compatibility;

2022/1/2

20. Traversal of array

Example 1: traversing an array requires output on one line

New output statement: "System.out.print(" content ");"
//No line breaks

public class BianLi {
    public static void main(String[] args) {
        int[] arr = {11,22,33,44,55};
        printArr(arr);
    }
    public static void printArr(int[] arr) { //Call method to implement
        for(int i = 0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
    }
}

Example 2: use the method to realize the maximum value in the array

public class MaxNum {
    public static void main(String[] args) {
        int[] arr = {45,89,6,3,475,999,1023,874,36};
        int max = getMax(arr);
        System.out.println(max);
    }
    public static int getMax(int[] arr) {
        int flag = 0;
        for (int i = 0;i<arr.length;i++){
            flag = arr[flag]>arr[i]?flag:i;
        }
        return arr[flag];
    }
}

21.Debug

1. What is it?
It provides a tool for program debugging, which is used to view the execution process of the program, and can also be used to track the execution process of the program to debug the program;
2. Process?
Add breakpoint; Run the program with breakpoints; Observation; Delete breakpoints;

22. Comprehensive examples

Example 1: every seven days

public class Jump7 {
    public static void main(String[] args) {
        //Every seven, the output is 1 ~ 100, but if it contains 7 or a multiple of 7, it should be said!

        for (int i = 1;i<=100;i++){
            if(i%10==7||i/10==7|| i%7==0){
                System.out.print("Yes!"+" ");
            }
            else{
                System.out.print(i+" ");
            }
        }
    }
}
/*1 2 3 4 5 6 Yes! 8 9 10 11 12 13 too! 15, 16!
 18 19 20 Yes! 22 23 24 25 26 too! Yes! 29 30 31 
 32 33 34 Yes! 36 times! 38 39 40 41 too! 43 44 45 
 46 Yes! 48! 50 51 52 53 54 55 too! Yes! fifty-eight 
 59 60 61 62 Yes! 64 65 66 too! 68 69! Yes! 
 Yes! Yes! Yes! Yes! Yes! Yes! Yes! Yes! 80 81 82 
 83 Yes! 85 86! 88 89 90! 92 93 94 95 96 
 Yes! Yes! 99 100 */

Example 2: Immortal rabbit

public class NoDieRabbit {
    public static void main(String[] args) {
        //There was a pair of rabbits. They gave birth to a pair of rabbits every month from the third month of birth. Little rabbit Zhang gave birth to another pair every month from the third month
        //If rabbits don't die, what are the logarithm of rabbits in these 20 months?
        int[] arr = new int[20];
        arr[0] = 1;
        arr[1] = 2;
        System.out.println("The first"+1+"The number of rabbits in months is"+arr[0]);
        System.out.println("The first"+2+"The number of rabbits in months is"+arr[1]);
        for (int i = 2; i < arr.length; i++) {
            arr[i] = arr[i - 2] + arr[i - 1];
            System.out.println("The first"+(i+1)+"The number of rabbits in months is"+arr[i]);
        }

    }
}
/*law:
*       First month: 1
*       Second month: 1
*       The third month: 2 = = = = > Fibonacci series
*       The fourth month: 3
*       Fifth month: 5
* */


The number of rabbits in the 20th month was 10946

Example 3: a hundred dollars and a hundred chickens

public class MoneyChicken {
    public static void main(String[] args) {
        //A rooster costs 5 yuan, a hen 3 yuan and a chick 1 yuan. How many cocks, hens and chicks can you buy for 100 yuan?
        int g, m, x;
        for (g = 0; g <= 20; g++) {
            for (m = 0; m <= 33; m++) {
                x = 100 - g - m;
                if (x % 3 == 0 && g * 5 + m * 3 + x / 3 == 100) {
                    System.out.println("The number of cocks is:" + g + "The number of hens is:" + m + "The number of chicks is:" + x * 3);
                }
            }
        }
    }
}

Example 4: summation of array elements

public class ShuZuYuanSuQiuHe {
    public static void main(String[] args) {
        //Find the sum of array elements: each bit and ten bits cannot be 7 and can only be an even number
        int[] arr = {68,27,95,88,171,996,51,210};
        int sum = 0;
        for(int i = 0;i<arr.length;i++){
            if(arr[i]%2==0&&arr[i]%10!=7&&(arr[i]/10)%10!=7){
                sum+=arr[i];
            }
        }
        System.out.println(sum);  //1362
    }
}

Example 5: the contents of the array are the same

public class SameShuZu {
    public static void main(String[] args) {
        //Design a method to judge whether the contents of two arrays are the same
        int[] arr = {1,2,3,5};
        int[] brr = {1,2,3,5};
        System.out.println(biJiao(arr,brr));
     }

    public static boolean biJiao(int[] a,int[] b) {
        if(a.length!=b.length){
            return false;
        }
        for (int i = 0;i<a.length;i++){
            if(a[i]!=b[i]){
                return false;
            }
        }
        return true;
    }
}

Example 6: find

import java.util.Scanner;

public class ChaZhao {
    public static void main(String[] args) {
        //Given a static array, enter a number to find its index in the array
        int[] arr = {19, 28, 37, 46, 50};
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a number:");
        int x = sc.nextInt();
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == x) {
                System.out.println("The index of this number in the array is:" + i);
                break;
            }
            count = count + 1;
            if (count == arr.length) {
                System.out.println("There is no number you want in the array!!");
            }

        }
    }
}

2022/1/3
Example 7: reverse

public class FanZhuan {
    public static void main(String[] args) {   //Single pointer
        int[] arr = {19,28,37,46,50,60};
        int flag;
        if (arr.length%2==0){  //Even length
            flag = arr.length/2-1;
        }
        else {                  //Odd length
            flag = arr.length/2;
        }
        for (int i = 0;i<=flag;i++){
            swap(arr,i,arr.length-1-i);
        }
        for (int j = 0;j<arr.length;j++){
            System.out.print(arr[j]+" ");
        }
    }
    public static void swap(int[] arr,int a,int b) {   //Exchange method
        int t = 0;
        t = arr[a];
        arr[a] = arr[b];
        arr[b] = t;
    }
}

public class FanZhuan {
    public static void main(String[] args) {   //Double pointer
        int[] arr = {19,28,37,46,50};
        int start,end;
        for (start = 0,end = arr.length-1;start<=end;start++,end--){
            int temp = 0;
            temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
        }
        for (int j = 0;j<arr.length;j++){
            System.out.print(arr[j]+" ");
        }
    }
}

Example 8: judges scoring

import java.util.Scanner;

public class DaFen {
    public static void main(String[] args) {
        //Six judges will score, the highest score and the lowest score will be removed, and the average value of the other four judges will be taken
        //Note: the score is an integer from 0 to 100
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[6];
        System.out.println("Please enter the scores of six judges:");
        for (int i=0;i<6;i++){
            arr[i] = sc.nextInt();
        }
        int jg = (summ(arr)-minn(arr)-maxx(arr))/4;
        System.out.println("The scoring results are:"+jg);
    }

    public static int minn(int[] arr) {
        int min = 0;
        for (int i=1;i<6;i++){
            if (arr[i]<arr[min]){
                min = i;
            }
        }
        return arr[min];
    }
    public static int maxx(int[] arr) {
        int max = 0;
        for (int i=1;i<6;i++){
            if (arr[i]>arr[max]){
                max = i;
            }
        }
        return arr[max];
    }
    public static int summ(int[] arr) {
        int sum = 0;
        for (int i=0;i<6;i++){
            sum += arr[i];
        }
        return sum;
    }
}

23. Classes and objects

1. What is object-oriented?
"The object-oriented method is mainly to objectify things, including their attributes and behaviors.
Object oriented programming is closer to the idea of real life.
Generally speaking, the bottom layer of object-oriented is still process-oriented. Process oriented is abstracted into classes, encapsulated and easy to use. It is object-oriented (everything is object). "
Namely: specific data information oriented;
2. What is class?
It is the abstraction of a kind of things with common attributes and behaviors in real life;
That is, the type of object-oriented data information;
3. What are the attributes of an object?
Attributes: objects have various characteristics, and each attribute of each object has a specific value;
4. What is the behavior of the object?
Behavior: operations that an object can perform;
5. Relationship between class and object?
Class is abstract and object is concrete;
Class is the abstraction of object, and object is the entity of class;
6. Definition of class?
Importance of classes: classes are the basic components of Java programs;
Class composition: attribute and behavior;

  • Attribute: reflected in the class through member variables (variables outside the methods in the class)
  • Behavior: reflected by member methods in the class (compared with the previous method, remove the static variable)

    2022/1/4
/*
*  Define a mobile phone class:
* */

public class Phone {   //Class name

    //Member variable
    String brand; //brand
    int price;      //Price

    //Member method
    public void call(){ //phone
        System.out.println("feed~");
    }
    public  void sendMessage(){  //send message
        System.out.println("Hello!");
    }
}

7. The number of objects is used

To be in main Method	

//Create object:

  • Format: class name object name = new class name ();
  • Example: Phone p = new Phone();

//Use object

  • Format: object name Variable name// Using member variables
  • Example: p.brand;
  • Format: object name Method name ()// Using member methods
  • Example: p.call();
public class PhoneDemo {
    public static void main(String[] args) {

        //create object
        Phone p = new Phone();

        //Using member variables
        System.out.println(p.brand);   //Direct output is the default
        System.out.println(p.price);

        //Assign member variables
        p.brand = "Apple";
        p.price = 15000;

        System.out.println(p.brand);
        System.out.println(p.price);

        //Using member methods
        p.call();
        p.sendMessage();
    }
}


8. Case: Students
Requirements: define a student class, then define a student test class, and complete the use of member variables and member methods through objects in the student test class;

public class Student {  //Student class

    String name;  //Member variable
    int age;

    public void study(){  //Member method
        System.out.println("I am a student,I love learning.!");
    }
    public void doHomeWork(){
        System.out.println("do homework,555 is due tomorrow");
    }
}

public class StudentDemo {
    public static void main(String[] args) {
        //Test student class

        Student mn = new Student();  //create object

        mn.name = "Beautiful nini";   //Member variable assignment
        mn.age = 22;
        System.out.println(mn.name+","+mn.age); //Output member variable

        mn.study();    //Output member method
        mn.doHomeWork();
    }
}

9. Member variable vs local variable

  1. Member variables: variables outside the methods in the class;
  2. Method variables: variables in the method;

    3. Differences between member variables and local variables
differenceMember variablelocal variable
Different positions in classOutside method in classMethod in class
Different memory locationsHeap memoryStack memory
Different life cyclesAs objects exist or disappearAs method calls exist or disappear
Different initial valuesThere is a default initial valueThere is no default value, which must be defined before assignment

24. Packaging

1.why? - There is data security in accessing member variables directly through object names;
2. Encapsulation: hide the attributes and implementation details of the object, only disclose the interface, and control the access level of attribute reading and modification in the program. Encapsulation is one of the three characteristics of object-oriented (polymorphism / encapsulation / inheritance); Specifically, some information is hidden inside the class, which is not allowed to be directly accessed by external programs. Instead, the operation of hidden information and access to member variable private are realized through the methods provided by the class, and the corresponding methods are provided;
**Advantages of encapsulation: * * control member variables through methods, which improves the security of code; Encapsulate the code to improve the reusability of the code;
3. private keyword: a permission modifier; Member variables can be modified; The function is to protect members from being used by other classes. Members modified by private can only be accessed in this class;
4. Operations on private modified variables:
//The "get variable name ()" method is provided to obtain the value of the member variable. The method is decorated with public;
//The "set variable name (parameter)" method is provided to set the value of member variables. The method is decorated with public;


resolvent


5.this keyword
When using the set method, local variables and member variables have the same name, so as to solve the problem that local variables hide member variables; this is used to refer to member variables. this represents the object to which the method is called;
For example:

Improvement method:

Here: use this modifier to refer to member variables, and those without this are local variables;

6. Construction method
*Function: * create object; Function: complete the initialization of object data;

format:
        Modifier class name(parameter){   //Modifiers are generally public
        
        }



matters needing attention:
//When no constructor is given in a class, the system will give a default parameterless constructor by default;
//If a construction method is defined, the system will no longer pass the default construction method;

    public Student(){}  //Nonparametric construction method

Characteristics of construction method:
1. The same method name and different parameters; (overload of construction method)

public class Student {  //Student class

    String name;  //Member variable
    private int age;

    public void show() { //Member method
        System.out.println(name + "," + age);
    }

    public Student() {   //Parameterless construction method (default)
    }

    public Student(String name) {   //  Construction method
        this.name = name;
    }

    public Student(int age) {      //Construction method
        this.age = age;
    }

    public Student(String name, int age) {   //Construction method
        this.age = age;
        this.name = name;
    }
   }
public class StudentDemo {
    public static void main(String[] args) {
        //Test student class

        Student s1 = new Student();  //create object
        s1.show();
        Student s2 = new Student("meini");
        s2.show();
        Student s3 = new Student(18);
        s3.show();
        Student s4 = new Student("zhaolongjie",24);
        s4.show();
    }
}

Topics: Java Back-end