当前位置:网站首页>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 :
- The user entered illegal data
- The file to be opened does not exist
- The connection is broken during network communication
- JVM out of memory
- Some of these exceptions are caused by user errors , Some are caused by program errors , Others are caused by physical errors .
- 3.3Exception
- summary
Exception Is the parent of all exception classes . Divided into non RuntimeException and RuntimeException .
- 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 . - 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 {
}
}边栏推荐
- Describe your understanding of the evolution process and internal structure of the method area
- Overall MySQL architecture and statement execution process
- Resolve the format conflict between formatted document and eslint
- Swift responsive programming
- 元宇宙系统的概念解析
- Sleep formula: how to cure bad sleep?
- Error: homebrew core is a shallow clone
- Nsurlsession learning notes (III) download task
- Alvaria宣布客户体验行业资深人士Jeff Cotten担任新首席执行官
- Golang open source streaming media audio and video network transmission service -lal
猜你喜欢

说下你对方法区演变过程和内部结构的理解

iVX低代码平台系列详解 -- 概述篇(一)

商城风格也可以很多变,DIY了解一下!

Lecun predicts AgI: big model and reinforcement learning are both ramps! My "world model" is the new way

Linux-MySQL数据库之高级SQL 语句一

揭秘GaussDB(for Redis):全面对比Codis
Gold three silver four, an article to solve the resume and interview

Blue Bridge Cup - practice system login
Practice of geospatial data in Nepal graph

GridLayout evenly allocate space
随机推荐
Inter thread synchronization semaphore control
iVX低代码平台系列详解 -- 概述篇(一)
error Parsing error: Unexpected reserved word ‘await‘.
Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022
JS add custom attributes to elements
Linux-MySQL数据库之高级SQL 语句一
Message format of Modbus (PLC)
元宇宙系统的概念解析
What is the NFT digital collection?
教务系统开发(PHP+MySQL)
Educational administration system development (php+mysql)
Describe your understanding of the evolution process and internal structure of the method area
10款超牛Vim插件,爱不释手了
What can one line of code do?
Write one file to the marked location of another file
什么是骨干网
Resolve the format conflict between formatted document and eslint
Built in function globals() locals()
Overall MySQL architecture and statement execution process
After flutter was upgraded from 2.2.3 to 2.5, the compilation of mixed projects became slower