1, Enumeration
Enumeration refers to a type consisting of a fixed set of constants
Features: safe type, easy to input, clear code
Common ways of enumerating
a) Define enumeration values directly
public enum Week{ //Enumeration values are all capitalized and separated by commas. enum keyword is added before class name to distinguish enumeration classes SUN,MON,TUE,WED }
b) Add method to enumeration
public enum Week{ SUN("Sunday"), MON("Monday"), TUE("Tuesday"), WED("Wednesday"); private String name; //Note that the get/set method only writes the get method. Enumeration is essentially a static constant. If there is a set method, //You can modify the value of static constants at will, and errors will occur in actual use public String getName() { return name; } //The constructor is private and cannot be instantiated private Week(String name){ this.name = name; } }
c) In the switch selection structure, the case value can be an enumeration value
2, Packaging
a) Each basic type has a corresponding wrapper class in the java.lang package
int-Integer
byte-Byte
short-Short
long-Long
float-Float
double-Double
char-Character
boolean-Boolean
b) Function:
Provides a series of methods to use,
The collection is not allowed to store data of basic data type. When storing numbers, the wrapper class shall be used
c) Note:
(1) When the Boolean class constructor parameter is of String type, if the String content is true (regardless of case), the Boolean object represents true, otherwise it represents false
(2) When the Number wrapper class constructor parameter is of String type, the String cannot be null, and the String must be parsed into the data of the corresponding basic data type, otherwise the compilation fails, and a NumberFormatException exception will be thrown at runtime
(3) The default value of the packaging class is null, and the default value of the basic type is a value. The packaging class can distinguish between cases where the value is not assigned and cases where the value is assigned as 0. The packaging class can be used to distinguish, for example, cases where the student is absent from the exam and the student's score is 0. For example, it has practical use in the form data application of jsp
(4) The data encapsulated by the Integer object is within the value range of 1 byte, i.e. - 128 ~ 127. Within this range, we compare whether the data values are equal. If it exceeds, we compare whether the addresses are the same
Integer tg1 = 500; Integer tg2 = 500; tg1 == tg2;//false Integer tg1 = 127; Integer tg2 = 127; tg1 == tg2;//true
//If non numeric string code is compiled in parentheses, an exception will be reported Integer i=new Integer(1); Integer i=new Integer("150"); Integer i=new Integer("hi");//There will be NumberFormatException exception during compilation
d) Common methods of wrapper classes (all static methods, called directly)
Convert packaging data to corresponding basic type data:
byteValue(),intValue(),longValue(),shortValue()
doubleValue(),floatValue(),charValue(),booleanValue()
toString(): returns the basic type data represented by the wrapper object in the form of string (basic type - > string)
valueOf: returns an Integer instance object of the specified type
parselnt(String s): convert numeric string to basic type data
Integer integerId=new Integer(25); int intId=integerId.intValue(); int byteId=integerId.byteValue(); int doubleId=integerId.doubleValue();
e) Characteristics of packaging:
After JDK1.5, mixed mathematical operations of basic data type and packaging type are allowed
Wrapper classes are not used to replace basic data types. They are used when basic data types need to be represented by objects
f) Concept of automatic unpacking and automatic packing
Automatic packing ----- value of basic type → instance of packing class, and the value is automatically converted to an object
Automatic unpacking ----- value of basic type ← instance of packing class, and the object is automatically converted to value
3, String object
a) Storing strings using String objects
String s = "Hello World"; String s = new String(); String s = new String("Hello World");
b) String comparison method
What is the difference between "= =" and equals():
equals compares whether the values of two strings are the same
==Whether the value and address of the compared string are the same at the same time
Other methods of string comparison
Use equalsIgnoreCase() method -- case insensitive
//For example, when logging in, the case of the user name is not considered to realize login
Use the toLowerCase() method to convert all strings to lowercase
Using the toUpperCase() method, all strings are capitalized
c) String connection
Method 1: use "+"
Method 2: use the concat() method of the String class
d) Common string extraction methods
(1) Search for the first character ch (or string value). If it is not found, return - 1
public int indexOf(int ch)
public int indexOf(String value)
(2) Search for the last character ch (or string value). If it is not found, return - 1
public int lastIndexOf(int ch)
public int lastIndexOf(String value)
(3) Extract the part of the string starting from the location index
public String substring(int index)
(4) Extract the string part between beginindex and endindex
public String substring(int beginindex, int endindex)
(6) Returns a copy of the call string without any spaces
public String trim()
(7) String splitting: String calls split(""); Method
4, StringBuffer class
a) StringBuffer declaration:
StringBuffer strb = new StringBuffer(); StringBuffer strb = new StringBuffer("aaa");
b) Use of StringBuffer:
sb.toString(); // Convert to String type
sb.append(“xx”); // Append string
sb.insert (1, “xx”); // Insert string
c) String class & StringBuffer class
Features: String is an immutable object. It is better not to use String for strings that often change content
StringBuffer is a variable string. If the string changes frequently, you can use StringBuffer for more efficiency
After JDK5.0, StringBuilder is provided, which is equivalent to StringBuffer. The difference is that StringBuffer is thread safe and inefficient; StringBuilder thread is unsafe, efficient, and locked internally
5, Random number class
next (int bits) returns the random value of the corresponding data type
Random random = new Random; int ranInt = random.nextInt(10);//Generate a random number of integer type between [0,10] //Note that boolean, long, double and float in the next method cannot pass parameters int ranLong = random.nextLong(); int ranDouble = random.nextDouble();
be careful:
The Random number generated by Random class is a pseudo-Random number. The Random number generated by Random class is determined according to the seed and order. You can understand the details by yourself
6, Math class
Math methods are defined as static forms and can be called directly in the main function through the math class
Common methods of Math class
static double abs(); / / returns the absolute value of the corresponding data type
static double ceil(double a); / / round up
static double floor(double a); / / round down
static double round(double a) / / round - floor (a+0.5);
Math.ceil(10.3);->11 Math.floor(10.9);->10 Math.round(10.4);->10 Math.round(-10.6);->-11
static long max(long a, long b); / / compare the maximum of the two numbers
static long min(long a, long b); / / compare the smallest of the two numbers
7, Date class
java.util.Date class: represents date and time
The java.text.SimpleDateFormat class specifies the date format
Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String form = format.format(date); System.out.println(form);
8, Calendar Class
Calendar calendar = Calendar.getInstance(); Date time = calendar.getTime(); System.out.println(time); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); int week = calendar.get(Calendar.DAY_OF_WEEK)-1;//Foreign weeks begin on Sunday System.out.println(year + "year" + month + "month" + day + "day" + "week" + week);
Calendar calendar = Calendar.getInstance(); //Date date = new Date(); //calendar.setTime(date); / / set the current time calendar.set(2015, 4, 6);//Set custom event time int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); int week = calendar.get(Calendar.WEEK_OF_YEAR); System.out.println(year + "year" + month + "month" + day + "The first day of this year" + week + "Week");