7/7 of Java Foundation in 7 days

The toString method of Object class

Class Object is the root class of class hierarchy

Each uses Object as a superclass

All objects implement methods of this class

//This is a subclass of the Object class, which implements all its methods
public class Person{
    private String name;
    private int age;
    
    public Person(){}
    public Person(String name,int age){
    this.name=name;
    this.age=age;
    }
    
    public void setName(String name){
    this.name = name;
    }
    public String getName(){
    return name;
    }
    
    public void setAge(int age){
    this.age = age;
    }
    public int getAge(){
    return age;
    }
    //Rewriting toString method
    //Rewrite the parameter list to be the same as the parent parameter list
    public String toString(){
        
        String string = "{name="+name+"   age="+age+"}";
        return string;
        
    }
    
}
public class PersonDemo{
    public static void main(String[] args){
    Person person = new Person("Li Bai",22);
    //Print the object directly. Print out the address value.
    System.out.println(person);//Person@3b192d32
    //The toString method that does not override the parent class prints the address value as well.
    String s = person.toString();
    System.out.println(s);//Person@3b192d32
    //Now rewrite the toString method,
    person.toString();//{name = Li Baiage = 22}
    
    }
}
//This is a subclass of the Object class, which implements all its methods
public class Person{
    private String name;
    private int age;
    
    public Person(){}
    public Person(String name,int age){
    this.name=name;
    this.age=age;
    }
    
    public void setName(String name){
    this.name = name;
    }
    public String getName(){
    return name;
    }
    
    public void setAge(int age){
    this.age = age;
    }
    public int getAge(){
    return age;
    }
    
}

equals Method of Object Class

public class PersonDemo{
    public static void main(String[] args){
    Person p1 = new Person("Li Bai",22);
    Person p2 = new Person("Su Shi",23);
    //Print the object directly. Print the address value.
    System.out.println(p1);
    System.out.println(p2);
    //Method equals of Object class
    //equals source code
    //boolean   equals​(Object obj)   Indicates whether some other object is "equal to" this one.
    
    /*public boolean equals(Object obj) {
        return (this == obj);
    }*/
    
    boolean b = p1.equals(p2);
    System.out.println(b);
    }
}public class PersonDemo{
    public static void main(String[] args){
    Person p1 = new Person("Li Bai",22);
    Person p2 = new Person("Su Shi",23);
    //Print the object directly. Print the address value.
    System.out.println(p1);
    System.out.println(p2);
    //Method equals of Object class
    //equals source code
    //boolean   equals​(Object obj)   Indicates whether some other object is "equal to" this one.
    
    /*public boolean equals(Object obj) {
        return (this == obj);
    }*/
    
    boolean b = p1.equals(p2);
    System.out.println(b);
    }
}

Date class

public class DemoDate{
    public static void main(String[] args){
        long l =System.currentTimeMillis();//How many milliseconds did it take from January 1, 1970 to today?
        System.out.println(l);
        //Turn milliseconds into days
        long  day = 24*60*60*1000;
        long aday = l/day;
        System.out.println(aday);//18036
        //Converting Days into Adulthood
        System.out.println(aday/365);//1970 is 49 years from now
    }
}

Construction Method and Membership Method of Date Class

import java.util.Date;
public class DateClass{
    public static void main(String[] args){
    Date date = new Date();
    //Date's empty parameter constructor returns the current date and time of the system
    System.out.println(date);
    Date date1 = new Date(100000000L);
    //The date parametric construction method converts the passed values into dates
    System.out.println(date1);
    
    System.out.println(method());
    }
    //Get the value of milliseconds
    public static long method(){
        
        Date date = new Date();
        return date.getTime();
    }
}

format Method and parse Method of DateFormat Class

import java.text.*;
import java.util.*; 
public class DemoDateFormat{
    public static void main(String[] args) throws ParseException{
        method2();
    }
    
    
    public static void method(){    //First create the DateFromat subclass SimpleDateFormat object
    //And the object is created using the constructor to specify the format
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd day HH time mm 55 seconds");
    //Call the method format, which needs to pass the Date class
    
    Date date = new Date();
    System.out.println(date);
    String str = sdf.format(date);
    System.out.println(str);
    }
    public static void method2() throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd day HH time mm 55 seconds");
        Date date = sdf.parse("2019 May 20, 2000 15:21:55 seconds");
        System.out.println(date);
    }
}
import java.util.*;
import java.text.*;
//Calculate how long a person lives
public class Survival{
    public static void main(String[] args) throws ParseException{
    System.out.println("Let's help you figure out how long you've lived.");
    System.out.println("Please follow"+" yyyy-MM-dd"+" Type in your date of birth");
    Scanner sc = new Scanner(System.in);
    String birth = sc.next();
    //Now I'm going to parse the date of birth string into Date format.
    //It needs to be in the same format as the date of birth, otherwise the exception will be resolved
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    //A string format with a specified format is required
    Date date = sdf.parse(birth);
    //Converting Dates to Millisecond Values
    long ms = date.getTime();
    //Get the current date and convert it to a millisecond value
    long todayTime = new Date().getTime();
    long newMs = todayTime-ms;
    long newDay = newMs/1000/60/60/24;
    System.out.println(newDay);
    }
}

Common member methods of Calendar classes

  1. Public int get (int field): Returns the value of a given calendar field
  2. public void set (int field, int value): Set a given calendar field to a given value
  3. Public Abstract void add (int field, int amount): According to calendar rules, add or subtract the specified amount of time for a given calendar field
  4. public Date getTime(); returns a Date object representing this Calendar time value
import  java.util.*;
public class CalendarDemo{
    public static void main(String[] args){
    //public int get(int field): Returns the value of a given calendar field
    //Parameter: Pass the specified calendar field (YEAR,MONTH....)
    //Return value: The specific value represented by the calendar field
    //getInstance is a special method
    //He will return a subclass of Calendar
    Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);//Get the year of the current system
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    System.out.println("year:"+year+" month:"+month+" day:"+day);
    System.out.println("====================================");
    
    c.set(Calendar.YEAR,2520);
    c.set(Calendar.MONTH,5);
    c.set(Calendar.DATE,20);
    
    
    int year1 = c.get(Calendar.YEAR);//Get the year of the current system
    int month1 = c.get(Calendar.MONTH);
    int day1 = c.get(Calendar.DAY_OF_MONTH);
    System.out.println("year:"+year1+" month:"+month1+" day:"+day1);
     //The same is true for increasing years.
    //c.add(Calendar.YEAR,2);
    //c.add(Calendar.MONTH,3);
     //Method of converting calendar objects into date objects
     //c.getTime(); this method returns the date
    }
}

Class System

System class provides a large number of static methods to obtain system-related information or system-level operations. In the API documents of System class, common methods are:

Public static long current Time Millis (): Returns the current time in milliseconds

public static void arraycopy(Object src, int srcPos,Object Dest, int destPos, int length: Copy the data specified in the array to another array;

import  java.util.*;
public class ArrayDemo{
    public static void main(String[] args){
    int[] src = {1,2,3,4,5};
    int[] dest = {6,7,8,9,10};
    System.out.println("Before replication:" +Arrays.toString(dest));
    System.arraycopy(src,0,dest,0,3);
    System.out.println("After replication:"+ Arrays.toString(dest));
    
    }
}

StringBuilder class

String buffer, which can improve the efficiency of string operation (can be seen as a variable length string)

//Constructing Method of StringBuilder
public class StringBuilderDemo{
    public static void main(String[] args){
    //A null parameter construction method with no characters and an initial character capacity of 16
    StringBuilder sb = new StringBuilder();
    System.out.println(sb+"Why should the Qiang flute complain about willows and spring breeze?");
    //A parameterized constructor with character content specified
    StringBuilder sb1 = new StringBuilder("Clouds think of clothes and flowers, and spring breeze breezes the sill and luxuriant");
    System.out.println(sb1);
    }
}
//Common methods of StringBuilder
public class StringBuilderDemo2{
    public static void main(String[] args){
    StringBuilder sb = new StringBuilder();
    //Here, the address value of sb is assigned to sb1
    StringBuilder sb1 = sb.append("Clouds want clothes and flowers, and spring breeze breezes the sill and dews.");
    System.out.println(sb);
    System.out.println(sb1);
    System.out.println(sb==sb1);
    
    //Chain programming
    StringBuilder libai =sb.append("If it's not for a group of Yushantou").append(",I will meet Yaotai next month.").append("----Plain Tune").append(".Li Bai");
    System.out.println(libai);
//Clouds want clothes and flowers, and spring breeze breezes the sill and dews. If you don't meet at the top of the mountain, you'll meet Yaotai next month. - Qingping Diao. Li Bai
    }
}
//Conversion between String and StringBuilder
public class StringBuilderDemo3{
    public static void main(String[] args){
    //String--->StringBuilder
    String str = "Clouds think of clothes and flowers";
    System.out.println(str);
    StringBuilder sb = new StringBuilder(str);
    sb.append("  Spring breeze blows the sill and luxuriant");
    System.out.println(sb);
    
    //StringBuilder-->Strintg
    String sb2 = sb.toString();
    System.out.println(sb2+"  If you don't meet at the top of the mountain, you'll meet Yaotai next month.");

    }
}
/* Packaging category:
    Basic data types are very convenient to use, but there is no corresponding method to manipulate these basic types of data. We can use a class to load the basic types of data and define some methods in the class. This class is called wrapping class. We can use the class method to manipulate these basic types of data.
*/
/*Packing: Packing basic types of data into packaging classes (int - > Integer)
    Integer Class Construction Method
    Integer​(int value) 
    Construct a newly allocated Integer object that represents the specified int value
    Integer​(String s)  
    Construct a newly allocated Integer object that represents the value indicated by the Stirng parameter
    The passed string must be a basic type of string
    
    Static method:
        Integer valueOf(int i)Returns an Integer representing the specified int value
        Integer valueOf(String s)
        
        
    Unpacking: Remove the basic type int int Value () from the packaging class.
    */
public class IntegerDemo{
    public static void main(String[] args){
    //To pack a case, first use the construction method to pack the case.
    Integer integer = new Integer(5);//Transfer int value to complete packing
    //Packing, second way
    Integer integer2 = new Integer("10");//Pass the basic type of string
    System.out.println(integer);//Rewrite toString
    System.out.println(integer2);//Rewrite toString
    //Third way of packing
    Integer integer3 = Integer.valueOf(1);
    Integer integer4 = Integer.valueOf("2");
    System.out.println(integer3);
    System.out.println(integer4);
    
    //Unpacking
    int i = integer3.intValue();
    System.out.println(i);
    
    
    }

}




//Auto-unloading and auto-loading
Integer in = 1; //Automatic packing Integer in = new Integer(1);

//Int in = in + 2; // Automatic unpacking occurred

Integer classes can convert basic types of strings into int types, or int types into string types.

Keywords: Java Spring Programming

Added by Jbert2 on Mon, 20 May 2019 22:20:59 +0300