regular classes and common APIs

5 minute read

Published:

Java basics – regular classes and common APIs

Regular classes and common APIs

Object Class

The java.lang.Object class is the root class of the Java language and the parent class of all classes.

  • toString() method

    public String toString()

    To print the name of the object directly is to call the toString() method of the object.

    The toString method returns the address value of the object by default.

    note:

    1. It doesn’t make sense to print the address value of the object directly, you need to rewrite the toString() method in the Object class.
    2. To see if a class has overridden the toString method, just print the object directly (address value -> not rewritten; non-address value -> rewritten).
  • equals() method

    public boolean equals(Object obj)

    The default comparison is the address value of the two objects.

note:

  1. We need to rewrite the equals method to compare the properties of two objects
  2. You need to use downcast to convert the Object type to a specific object type. Pay attention to the ClassCastException problem
  • Objects class

    A tool class added after JDK1.7+, java.util.Objects

    It consists of some static methods, these methods are null pointer safe

    Such as:

      public static boolean equlas(Object a,Object b){
          return (a==b)||(a!=null&&a.equals(b));
      }
    

Date and time class

  • Date class

    The java.util.Date class represents a specific moment, accurate to the millisecond.

    Construction method:

    1. Date(): Get the date and time of the current system
    2. Date(long date): Convert millisecond value to Date date Member method: long getTime(): Convert date to millisecond value
  • DateFormat class

    The java.text.DateFormat class can complete the conversion between date and text.

    Member method:

    1. String format(Date date): Format the Date date into a string according to the specified pattern
    2. Date parse(String source): Parse the string matching the pattern into Date date
yyear
MMonth
dday
HHour
mminutes
sseconds

Note: The DateFormat class is an abstract class, you cannot create objects directly, you can use SimpleDateFormat

Construction method:

SimpleDateFormat(String pattern): Create SimpleDateFormat with the given pattern and the date format symbols of the default locale, Such as: yyyy-MM-dd HH:mm:ss

  • Calendar class

    The java.util.Calendar class is an abstract class that provides many methods for calendar fields

    Member method:

    1. static Calendar getInstance(): Obtain a calendar using the default time zone and locale, and return a Calendar subclass object
    2. public int get(int field): returns the value of a given calendar field
    3. public void set(int field,int value): Set the given calendar field value to the specified value
    4. public abstract void add(int field, int amount): Add or subtract a specified amount of time for a given calendar field according to the rules of the calendar
    5. public Date getTime(): Returns a Date object representing the time value of this Calendar

System Class

A large number of static methods are provided in the java.lang.System class. The commonly used methods are:

  1. public static long currentTimeMillis(): returns the current time in milliseconds
  2. public static void arrayCopy(Object src, int srcPos, Object dest, int destPos, int length): Copy the specified data in the array to another array

StringBuilder class

  • String class

    The string is a constant, the bottom layer is a final array, the content cannot be changed after creation

    private final byte[] value;

  • StringBuilder class

    String buffer can improve the efficiency of string operation, the bottom layer is an ordinary array, which can be changed

    private byte[] value=new byte[16];

    If it exceeds the range, it will automatically expand

  • Construction method
    1. public StringBuilder(): Construct an empty StringBuilder container
    2. public StringBuilder(String str): Construct a StringBuilder container and add the string to it
  • Common methods
    1. public StringBuilder append(......): add any type of data in the form of a string, and return the current object itself
    2. public String toString(): Convert the current StringBuilder object to a String object

Packaging

  • Basic data types

It is very convenient to use, but there is no corresponding method to operate

You can use a class to wrap the basic data types and define some methods in the class. These classes are called packaging classes

  • Boxing and unboxing
  1. Packing: Pack basic types of data into packaging

    (1) Construction method

    Integer(int value), Integer(String str)

    Note: The string must be a basic type of string, otherwise a NumberForamtException will be thrown

    (2) Static method

    static Integer valueOf(int i), static Integer valueOf(String s)

  2. Unpacking: Take out the basic types of data in the packaging category

    Member method: int intVaule()

Auto-boxing and auto-unboxing: After JDK 1.5+, basic types of data and packaging classes can be automatically converted to each other

  • Conversion between basic types and strings

    1. Basic type conversion to String

      (1) The value of the basic type + “”

      (2) The static method toString (parameter) of the wrapper class

      (3) The static method valueOf of the String class (parameter)

    2. String conversion to basic data types

      Use the static method parseXxx(string) of the wrapper class

      Note: The string must be a basic type of string to prevent NumberFormatException

Integer in = 1 ;//Auto box
int i=in+ 2 ;//Automatic unboxing