当前位置:网站首页>Common APIs and exception mechanisms

Common APIs and exception mechanisms

2022-06-25 16:19:00 weixin_ forty-eight million six hundred and forty-four thousand

1 Biginteger

1.1 summary

1 Integer Class is int The wrapper class , The maximum integer value that can be stored is 231-1,,long Classes are also finite , The maximum value is 263-1. If you want to represent a larger integer , Whether basic data types or their wrapper classes There's nothing we can do ,

2 java.math Bag Biginteger Can represent immutable integers of any precision .

Biginteger Some uses of

package Bigintrger;

import java.math.BigDecimal;
import java.math.BigInteger;

public class Biginteger_01 {
	public static void main(String[] args){
		// You must pass in the string 
		BigInteger v1 = new BigInteger("123123");
		BigDecimal v2 = new BigDecimal(20);
		BigDecimal v3 = new BigDecimal(20);
		//+
		BigDecimal result = v2.add(v3);
		System.out.println(result);
		//-
		result = v2.subtract(v3);
		System.out.println(result);
		//*
		result = v2.multiply(v3);
		System.out.println(result);
		//  /
		result = v2.divide(v3);
		System.out.println(result);
		// %
		result =v2.remainder(v3);
		System.out.println(result);
	}

}

adopt add  substract  multiply  divide  remainder They can be added separately reduce ride except and Remainder operation

in addition It can also be used to do factorial operations

package Bigintrger;

import java.math.BigInteger;

public class Biginteger_02 {
	public static void main(String[] args){
		System.out.println(test(120));
		System.out.println(test1(120));
		System.out.println(Long.MAX_VALUE);
	}
	public static long test(long n){
		long result = 1;
		for(int i = 1;i<=n;i++){
			result*=i;
		}
		return result;
	}
	public static BigInteger test1(long n){
		BigInteger result = new BigInteger("1");
		for(int i = 1;i<=n;i++){
			result = result.multiply(new BigInteger(i+""));
		}
		return result;
	}

}

use test The method does not yield 120 The factorial , But with BigIntefer You can get 120 The factorial result of

2 Math

2.1 summary

java.lang.math Provide some static methods for scientific calculation , The parameter and return value types of the method are generally double type .

package Math;

public class Math_02 {
	public static void main(String[] args){
		// The absolute value 
		System.out.println(Math.abs(-3));
		// Rounding up , If there is a decimal, carry it 
		System.out.println(Math.ceil(2.000001));
		// Rounding down , Discard decimals 
		System.out.println(Math.floor(2.99999));
		// Returns the maximum value 
		System.out.println(Math.max(2.3, 4.1));
		// Returns the smallest value 
		System.out.println(Math.min(2.3, 4.1));
		// Square root 
		System.out.println(Math.sqrt(16));
		// Open Cube 
		System.out.println(Math.cbrt(8));
		// Returns a random number greater than or equal to zero and less than one 
		// Nature is Random Of nextDouble Method 
		System.out.println(Math.random());
		// Round to the nearest five , When the decimal is greater than 0.5  Just   Into the 1, Less than 0.5 Give up , When the decimal is 0.5 At the end of the day , Take even number 
		//2.5=2, 3.5=4 , 2.50001 = 3
		System.out.println(Math.rint(2.50001));
		System.out.println(Math.rint(9.5));
		//N Of M The next power ,2 Of 3 The next power 
		System.out.println(Math.pow(2, 3));

abs Take the absolute value

ceil Rounding up

floor Rounding down

max min Returns the maximum and minimum values

sqrt Square root

cbrt Open Cube

random A random number greater than or equal to zero and less than one

rint Round to the nearest five

pow The next power

3 Exception mechanism

3.1 Common abnormal

1 Null pointer exception

2 The subscript crossing the line

3 Type conversion exception

4 Stack memory overflow

3.2 summary

Exception is Java A mechanism for identifying response errors is provided in

There are many reasons for the abnormality , such as

  1. The user entered illegal data
  2. The file to be opened does not exist
  3. The connection is broken during network communication
  4. JVM out of memory
  5. Some of these exceptions are caused by user errors , Some are caused by program errors , Others are caused by physical errors .
  6. 3.3Exception
    1.   summary
  7. Exception Is the parent of all exception classes . Divided into non RuntimeException and RuntimeException .

  8. Not RuntimeException 
    It refers to the exceptions that need to be caught or handled during program compilation , Such as IOException、 Custom exception, etc . Belong to checked abnormal .
  9. RuntimeException 
    It refers to the exception that does not need to be caught or handled during program compilation , Such as :NullPointerException etc. . Belong to unchecked abnormal . It is usually caused by the carelessness of programmers . Such as null pointer exception 、 An array 、 Type conversion exception, etc .
package java_exception;
/**
 *   abnormal   In fact, it is a wrong statement , stay java in   There is a special class that simulates all exceptions and errors  Throwable , Is the parent of all exception classes 
 * 
 * try...catch... :  Handling exceptions , Generally used for client 
 * 
 * throws :  Throw an exception , It is generally used for the server 
 * 
 * throw :  Abnormal source point 
 * 
 * finally :  Statements that must be executed 
 * @ author   Dawn Education - Liuchengzhi 
 * @ Time  2022 year 1 month 17 On the afternoon of Sunday 9:44:57
 */
public class Exception_01 {
	public static void main(String[] args){
		System.out.println("---------");
		// The divisor cannot be zero 0
		int a = 10;
		int b = 0;
		if (b!=0){
			int c = a/b;
		}else{
			System.out.println(" Divisor cannot be zero ");
		}
		System.out.println("=======");
	}

}

 

package java_exception;

import java.io.FileInputStream;



/**
 *  try{
 * 		 High risk code 
 * 		 Just make mistakes , Is executed catch,try The remaining code in is no longer executed 
 * 		 If nothing goes wrong ,try You can successfully complete , also catch No more execution 
 * }catch( Exception types   Variable ){
 * 		 Treatment scheme ;
 * @ author   Dawn Education - Liuchengzhi 
 * @ Time  2022 year 1 month 17 On the afternoon of Sunday 9:55:04
 */
public class Exception_02 {
	public static void main(String[] args){
		try{
			// Load the corresponding file 
			FileInputStream fis = new FileInputStream("123.text");
			System.out.println("======");		
		}catch(Exception e){
			// Print error stack frame and information , Generally used by programmers to debug 
			e.printStackTrace();
			// Get error message location , Generally used for feedback to clients 
			String msg = e.getMessage();
			System.out.println(msg);
			System.out.println("xxxxxx");
		}
		// Do not execute at the end of the life cycle 
		System.out.println(1111);
	}

}
public class Exception_03 {
	public static void main(String[] args){
		try{
			m1();
		}catch(Exception e){
			//e.printStackTrace();
			//TODO Measures to solve the errors 
		}
		System.out.println("====");
	}// throws  Multiple exceptions can be thrown at the same time , Multiple are separated by commas 
	public static void m1() throws FileNotFoundException,Exception {
		m2();
	}

	public static void m2() throws FileNotFoundException {
		m3();
	}

	public static void m3() throws FileNotFoundException {
		new FileInputStream("123");
	}

}
public class Exception_04 {
	public static void main(String[] args) {
		//  When try below   When there may be multiple exceptions , And each exception corresponds to a different solution 
		//  Need to write more than one catch Different treatment 
		//  If the solution is consistent , Then write only one  exception  that will do 
		try {
			new FileInputStream("123");
			String stre = null;
			System.out.println(stre.trim());
		} catch (FileNotFoundException e) {
			System.out.println(" The specified file cannot be found ");
		} catch (IOException e) {
		} catch (NullPointerException e) {
			System.out.println(" Can't be empty ");
		} catch (Exception e) {
			System.out.println(" Other anomalies ");
		}
	}

}
public class Exception_05 {
	public static void main(String[] args) {
		try {
			new FileInputStream("123");
		} catch ( FileNotFoundException | NullPointerException e) {
			// TODO: handle exception
		}
	}
}
public class Exception_06 {
	public static void main(String[] args) {
		try {
			FileInputStream fis = new FileInputStream("123");
			System.out.println(fis);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			System.out.println(" Must be implemented ");
		}
	}
}
public class Exception_07 {
	public static void main(String[] args) {
		int result = m1();
		// 11
		System.out.println(result);
	}

	public static int m1() {
		int i = 10;
		try {
			//  because finally There is return, So here return  Don't execute , however  i++ perform , After the compilation   Will be able to return Get rid of 
			return i++;
		} catch (Exception e) {
		} finally {
			System.out.println(i + "-------");
			return i;
		}
	}
}
public class Exception_08 {
	public static void main(String[] args) {
		//  Promote scope , Otherwise, we will not be able to access it 
		//  Add default , Otherwise, an error will be reported because the local variable has no default value 
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("D://123.txt");
			System.out.println(" Successful implementation ");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				//  Determine whether to open the resource 
				if (fis != null) {
					System.out.println(" Closed successfully ");
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
public class Exception_09 {
	public static void main(String[] args) {
		try(
				FileInputStream fis = new FileInputStream("123");
				) {
			//  operation 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
public class Exception_10 {
	public static void main(String[] args) {

	}
}
class A {
	public void m1() throws IOException {

	}
}
class B extends A {
	public void m1() throws IOException {

	}
}

原网站

版权声明
本文为[weixin_ forty-eight million six hundred and forty-four thousand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190534174253.html