Packaging
6. The hash code value of the wrapper object is fixed
7. The parent class of value type wrapper class is Number
package cn.tedu.baozhuang; public class BZDemo1 { public static void main(String[] args) { //The hash code value of the object of the wrapper class is a fixed value System.out.println(new Integer(1).hashCode()); System.out.println(new Byte((byte) 1).hashCode()); System.out.println(new Short((short) 1).hashCode()); System.out.println(new Long(1).hashCode()); System.out.println(new Character('a').hashCode()); System.out.println(new Float(1.1f).hashCode()); System.out.println(new Double(1.1).hashCode()); System.out.println(new Boolean(true).hashCode()); // Integer in=1; m(1); } public static void m(Object obj){} }
Mathematics
Math math class, which provides simple mathematical operations (the properties and methods provided are static)
package cn.tedu.math; public class MathDemo { public static void main(String[] args) { //absolute value //System.out.println(Math.abs(-2.134)); //Round up /*System.out.println(Math.ceil(1.0000001)); //Round down System.out.println(Math.floor(1.999999)); //Round off (the result is an integer) System.out.println(Math.round(3.45));*/ //The first parameter is the base and the second parameter is the power //System.out.println(Math.pow(2.0,3.0)); //Random number (from 0.0 to 1.0 (excluding 1.0)) //The bottom layer is based on random algorithm //System.out.println(Math.random()); //Gets a random number from 20 to 40 inclusive //System.out.println(20+(int)(Math.random()*21)); //Simple verification code //Content of verification code char[] cs={'4','A','8','G','9','B','2','Scatter'}; //String to store the content of the verification code String str=""; //Loop to randomly select the content of verification code for (int i = 0; i < 4; i++) {//The verification code has only four digits str+=cs[(int)(Math.random()*9)];//Subscript range (0 ~ 7) } // System.out.println(str); } }
BigDecimal class provides precise decimal operation (ensure that the parameter is in string form)
package cn.tedu.math; import java.math.BigDecimal; public class BigDecemalDemo { //In the process of operation, it can be increased to 80 bits, but it is finally stored in 64 bits public strictfp static void main(String[] args) { //The binary complement form of most decimals is infinite digits, which can not be calculated accurately /*double d=2.1-1.9; System.out.println(d);*/ //The exact operation can only be performed if the parameter is in the form of string BigDecimal bd1=new BigDecimal("2.1"); BigDecimal bd2=new BigDecimal("1.9"); //The value of the calling method operation object System.out.println(bd1.subtract(bd2)); } }
The BigInteger class provides operations between very large numbers
package cn.tedu.math; import java.math.BigInteger; public class BigIntegerDemo { public static void main(String[] args) { //Operation between super large numbers // BigInteger bi1=new BigInteger("5343212313245342135421315646877983"); BigInteger bi2=new BigInteger("5134532312135431231546431321231585"); //Operation object value by calling System.out.println(bi1.multiply(bi2)); } }
time
The Date class represents time and Date
The SimpleDateFormat class provides the conversion between date and string
parse(): convert string to date
foamat(): convert date to string
package cn.tedu.time; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[] args) throws ParseException { //Create a class that represents time and date //Date date=new Date(); //CST (China standard time zone) -- current time and date //Parametric construction - add January 1900 to the specified basis //Black line - represents obsolete, which means that it will be eliminated in a new version /*Date date=new Date(2008-1900,8-1,8); System.out.println(date);*/ //Conversion between string and date //String to date String str="2012-12-12 12:12:12"; //create object //ParseException --- parse exception //Specify parsing format SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //Call the method to convert the string to a date Date date=sdf1.parse(str); //Convert date to string //create object SimpleDateFormat sdf2=new SimpleDateFormat("yyyy year MM month dd day HH Time mm branch ss second"); //Call the method to convert the date into a string String s=sdf2.format(date); System.out.println(s); } }
The Calendar class represents the Calendar class
package cn.tedu.time; import java.util.Calendar; import java.util.Date; public class CalendarDemo { public static void main(String[] args) { //Gets the subclass object of the Calendar class Calendar c=Calendar.getInstance(); //Set time c.setTime(new Date(2012-1900,12-1,12)); //Get calendar information System.out.println(c.get(Calendar.DAY_OF_WEEK)); System.out.println(c.get(Calendar.DAY_OF_MONTH)); } }
abnormal
A set of mechanisms used to identify problems, feed back problems, and solve problems
Throwable class - the top-level parent of the exception
Subclass--------
Error: it is a serious problem that a reasonable application should not try to grasp (changing external requirements, environment, resources, etc.)
Exception: it is a reasonable application, which can be blown or not handled
Processing method (1. Throw, 2. Capture)
package cn.tedu.exception; import java.text.SimpleDateFormat; public class ExceptionDemo1 { public static void main(String[] args) { //Arithmetic exception -- compile correctly and run incorrectly /*try { System.out.println(1/0); }catch (Exception e){ }*/ //System.out.println(1/0); //The array subscript is out of bounds --- the compilation is correct and the operation is wrong /*int[] arr={1}; arr[1]=1;*/ //Cloning does not support exceptions ----- compilation error /*try { new ExceptionDemo1().clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); }*/ //Parsing exception - compilation error //new SimpleDateFormat().parse(); } }
classification
Compile time exception - errors during compile time must be handled
Exception classes other than RuntimeException class and subclasses
CloneNotSupportedException - cloning does not support exceptions
ParseException -- parse exception
Runtime exception - error at runtime
RuntimeException classes and subclasses can be processed or not
ArithmeticException -- arithmetic exception
NullPointerException - null pointer exception
ArrayIndexOutofBoundsException - array index out of bounds exception
ClassCastException - type conversion exception
NumberFomatException - number format exception
package cn.tedu.exception; public class ExceptionDemo2 { public static void main(String[] args) { //Call method to read file //Several compile time exceptions are caught when several compile time exceptions are thrown on the method try {//The try block contains code that may cause problems String s=readFiles(null);//"W:\\a.txt" } catch (FileNotExitException e) {//The catch block will only detect whether there are exceptions in the try block //If an exception occurs, the corresponding thrown exception is captured according to the type of the corresponding exception class //e.printStackTrace(); System.out.println("The problem has been dealt with..."); }catch (FilesNotFoundException e){//=new FilesNotFoundException("pro, your file type is wrong!!!"); //Get description information //Call the method of the parent class to indirectly obtain the privatization attribute value of the parent class System.out.println(e.getMessage()); }catch (NullPointerException e){ //Print stack track e.printStackTrace(); } //After the exception is handled, the subsequent code is executed normally System.out.println("Read complete"); } //Define how to read files //Multiple exceptions can be thrown on the method (separated by commas) //If an exception must be thrown on a method, it is a compile time exception public static String readFiles(String path) throws FileNotExitException, FilesNotFoundException,NullPointerException { //Determine whether the path is null if (path == null) { //The judgment indicates that the path is null //Find problems and feed back problems throw new NullPointerException(); } //Determine whether the file type is a txt file if (!path.endsWith("txt")) { //The judgment indicates that it is not a txt file //discover problems //Feed back the problem (description information) and pass the exception class object upward throw new FilesNotFoundException("Pro, your file type is wrong!!!"); } //Determine whether the drive letter is W if (path.startsWith("W")) { //The judgment description starts with W can be judged //Indicates a problem //Feedback problem --- passing exception class objects up //Feedback: the code behind the problem is not executed throw new FileNotExitException(); } //Read file contents return "Document content"; } } //Custom exception class //Custom exception classes inherit exception classes other than RunTimeException class and subclasses //Custom exceptions are compile time exceptions by default class FileNotExitException extends Exception{ } class FilesNotFoundException extends Exception{ /* //Privatization attribute private String message; //Provides a get method to get the privatized property @Override public String getMessage() { return message; } //The parameterized structure assigns a value to the privatization attribute public FilesNotFoundException(String message){ this.message=message; }*/ //The parameterized constructor assigns a value to the privatization attribute of the parent class public FilesNotFoundException(String message){ super(message); } }