regular classes and common APIs
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:
- 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.
- 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:
- We need to rewrite the equals method to compare the properties of two objects
- 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:
Date()
: Get the date and time of the current systemDate(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:
String format(Date date)
: Format the Date date into a string according to the specified patternDate parse(String source)
: Parse the string matching the pattern into Date date
y | year |
---|---|
M | Month |
d | day |
H | Hour |
m | minutes |
s | seconds |
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:
static Calendar getInstance()
: Obtain a calendar using the default time zone and locale, and return a Calendar subclass objectpublic int get(int field)
: returns the value of a given calendar fieldpublic void set(int field,int value)
: Set the given calendar field value to the specified valuepublic 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 calendarpublic 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:
public static long currentTimeMillis()
: returns the current time in millisecondspublic 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
public StringBuilder()
: Construct an empty StringBuilder containerpublic StringBuilder(String str)
: Construct a StringBuilder container and add the string to it
- Common methods
public StringBuilder append(......)
: add any type of data in the form of a string, and return the current object itselfpublic 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
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)
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
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)
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