Common knowledge induction

Posted by stretchy on Sun, 19 Dec 2021 00:50:55 +0100

Object class

1. equals method

Function of equals method It is used for comparison between objects and returns true and false results Example: S1 equals(s2); S1 and S2 are two objects Scenario of overriding the equals method When you don't want to compare the address value of the object and want to compare it with the object attribute. How to override the equals method 1. alt + insert in IntelliJ idea, select equals() and hashCode(), then next and finish all the way 2. Right click in the blank space in eclipse and select source - > generate hashcode () and equals () -- > generate
		@Override
	    public boolean equals(Object o) {
	        if (this == o) return true;
	        if (o == null || getClass() != o.getClass()) return false;
	        Student student = (Student) o;
	        return Objects.equals(name, student.name);
	    }
	
	    @Override
	    public int hashCode() {
	        return Objects.hash(name);
	    }

2. toString method

How to override the toString method 1. Alt + Insert in IntelliJ idea to select toString 2. Right click the blank space in eclipse and select Source - > generate to string o - > generate toString method: It is more convenient to display the attribute values in the object in a good format
  • Code demonstration:
public class File {
    double size;
    String name;

    public File(double size, String name) {
        this.size = size;
        this.name = name;
    }

    @Override
    public String toString() {//Overridden toString method
        return "File{" +
                "size=" + size +
                ", name='" + name + '\'' +
                '}';
    }
}
public class FileTest {
    public static void main(String[] args) {
        File f1 = new File(9.16, "Learning materials");
        File f2 = new File(8.16, "The move ");
        System.out.println(f1.toString());
        System.out.println(f2.toString());
    }
}
//Operation results
File{size=9.16, name='Learning materials'}
File{size=8.16, name='The move '}

Math class

1. Math class overview Math contains methods for performing basic numeric operations 2. Method calling method in Math There is no constructor in Math class, and the internal methods are static. You can use class names Method name
  • common method
Method nameexplain
public static int abs(int a)Returns the absolute value of the parameter
public static double ceil(double a)Returns the minimum double value greater than or equal to the parameter, which is equal to an integer
public static double floor(double a)Returns the maximum double value less than or equal to the parameter, which is equal to an integer
public static int round(float a)Returns the int closest to the parameter by rounding
public static int max(int a,int b)Returns the larger of the two int values
public static int min(int a,int b)Returns the smaller of the two int values
public static double pow (double a,double b)Returns the value of a to the power of b
public static double random()The return value is a random number in the [0.0, 1.0) interval

Date processing class

1. Calendar Class

  • Calendar Class overview
    • Calendar provides some methods for the conversion between a specific moment and a set of calendar fields, and some methods for manipulating calendar fields
    • Calendar provides a class method, getInstance, to get commonly useful objects of this type.
    • This method returns a Calendar object.
    • Its calendar field has been initialized with the current date and time: calendar rightnow = calendar getInstance();
      Common methods of Calendar Class

Common methods of Calendar Class:

Method nameexplain
public int get(int field)Returns the value of the given calendar field
public final void set(int year,int month,intdate)Set the month, year and day of the current calendar
  • Code demonstration:
public class CalendarTest {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();

        //Returns the value of the given calendar field
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;//Start from 0
        int date = calendar.get(Calendar.DATE);
        int week_day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int year_day = calendar.get(Calendar.DAY_OF_YEAR);
        System.out.println("Today is" + year + "year" + month + "month" + date + "day");
        System.out.println("Today is the third day of the week" + week_day + "day");
        System.out.println("Today is the third day of this year" + year_day + "day");

        System.out.println();

        //Set the month, year and day of the current calendar
        calendar.set(2077, 11, 10);
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        date = calendar.get(Calendar.DATE);
        System.out.println(year + "year" + month + "month" + date + "day");
    }
}
//Operation results
 Today is August 24, 2021
 Today is the second day of the week
 Today is the 236th day of this year

2077 December 10

2. SimpleDateFormat class

  • SimpleDateFormat class overview
    • SimpleDateFormat is a concrete class for formatting and parsing dates in a locale sensitive manner.

SimpleDateFormat class constructor:

Method nameexplain
public SimpleDateFormat()Construct a SimpleDateFormat, using the default mode and date format
public SimpleDateFormat(String pattern)Construct a SimpleDateFormat, using the given pattern and the default date format

Common methods of SimpleDateFormat class:

Method nameexplain
public final String format(Date date)Format the date as a date / time string
public Date parse(String source)Parses text from the beginning of a given string to generate a date
  • Code demonstration:
public class SimpleDateFormatTest {
    public static void main(String[] args) throws ParseException {
        //Convert Date object to string
        Date date = new Date();//current date
        System.out.println(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd day HH:mm:ss");//format
        System.out.println(sdf.format(date));

        System.out.println();

        //Convert string to Date object
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy year MM month dd day HH:mm:ss");
        System.out.println(sdf2.parse("2077 December 10, 2008:17:21"));
    }
}