当前位置:网站首页>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
- Why does golang's modification of slice data affect the data of other slices?
- What is the NFT digital collection?
- Golang open source streaming media audio and video network transmission service -lal
- Shuttle pop-up returns to the upper level
- Catheon gaming appointed mark Aubrey, former Asia Pacific head of Activision Blizzard, as CEO
- 赫尔辛基交通安全改善项目部署Velodyne Lidar智能基础设施解决方案
- Golang uses Mongo driver operation - increase (Advanced)
- 商城风格也可以很多变,DIY 了解一下!
- 加载本地cifar10 数据集
猜你喜欢
error Parsing error: Unexpected reserved word ‘await‘.
Dino: Detr with improved detecting anchor boxes for end to end object detection
Educational administration system development (php+mysql)
商城风格也可以很多变,DIY了解一下!
Alvaria announces Jeff cotten, a veteran of the customer experience industry, as its new CEO
The style of the mall can also change a lot. DIY can learn about it!
Don't underestimate the integral mall, its role can be great!
How to reload the win10 app store?
说下你对方法区演变过程和内部结构的理解
Yadali brick playing game based on deep Q-learning
随机推荐
js 给元素添加自定义属性
Beginner bug set
Popular cross domain
Navicat Premium 15 for Mac(数据库开发工具)中文版
Precautions for function default parameters (formal parameter angle)
Create raspberry PI image file of raspberry pie
绕过技术聊'跨端'......
基于神经标签搜索,中科院&微软亚研零样本多语言抽取式摘要入选ACL 2022
Report on Hezhou air32f103cbt6 development board
Free books! AI across the Internet paints old photos. Here is a detailed tutorial!
Prototype chain analysis
Don't underestimate the integral mall, its role can be great!
Check whether the port number is occupied
Golang uses Mongo driver operation - increase (Advanced)
error Parsing error: Unexpected reserved word ‘await‘.
不要再「外包」AI 模型了!最新研究发现:有些破坏机器学习模型安全的「后门」无法被检测到
Prototype mode
Time wheel and implementation analysis of time wheel in go zero
Describe your understanding of the evolution process and internal structure of the method area
f_read 函数[通俗易懂]