1. BigInteger
1.1 概述
1 、Integer 类作为int 的包装类,能存储的最大整型值为231 - 1 ,Long 类也是有限的, 最大为263 - 1 。如果要表示再大的整数,不管是基本数据类型还是他们的包装类 都无能为力,更不用说进行运算了。
2 、java. math包的BigInteger 可以表示不可变的任意精度的整数。BigInteger 提供
所有 Java 的基本整数操作符的对应物,并提供 java. lang. Math 的所有相关方法。 另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、 位操作以及一些其他操作。
1.2 常用方法
public BigInteger abs ( ) :返回此 BigInteger 的绝对值的 BigInteger 。
BigInteger add ( BigInteger val) :返回其值为 ( this + val) 的 BigInteger
BigInteger subtract ( BigInteger val) :返回其值为 ( this - val) 的 BigInteger
BigInteger multiply ( BigInteger val) :返回其值为 ( this * val) 的 BigInteger
BigInteger divide ( BigInteger val) :返回其值为 ( this / val) 的 BigInteger 。整数 相除只保留整数部分。
BigInteger remainder ( BigInteger val) :返回其值为 ( this % val) 的 BigInteger 。
BigInteger [ ] divideAndRemainder ( BigInteger val) :返回包含 ( this / val) 后跟( this % val) 的两个 BigInteger 的数组。
BigInteger pow ( int exponent) :返回其值为 ( thisexponent) 的 BigInteger 。
1.3 使用
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) ;
1.4 阶乘
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;
}
2. Math
2.1 概述
java. lang. Math 提供了一系列静态方法用于科学计算。其方法的参数和返回
值类型一般为double 型。
2.2 常用方法
abs 绝对值 acos, asin, atan, cos, sin, tan 三角函数 sqrt 平方根
pow ( double a, doble b) a的b次幂 log 自然对数
exp e为底指数
max ( double a, double b)
min ( double a, double b)
random ( ) 返回0.0 到1.0 的随机数
long round ( double a) double 型数据a转换为long 型(四舍五入)
toDegrees ( double angrad) 弧度—> 角度
toRadians ( double angdeg) 角度—> 弧度
2.3 使用方式
System . out. println ( Math . abs ( - 3 ) ) ;
System . out. println ( Math . ceil ( 2.000000001 ) ) ;
System . out. println ( Math . floor ( 2.999999999999 ) ) ;
System . out. println ( Math . max ( 2.3 , 4.1 ) ) ;
System . out. println ( Math . min ( 2.3 , 4.1 ) ) ;
System . out. println ( Math . sqrt ( 16 ) ) ;
System . out. println ( Math . cbrt ( 8 ) ) ;
System . out. println ( Math . random ( ) ) ;
System . out. println ( Math . rint ( 2.50001 ) ) ;
System . out. println ( Math . rint ( 9.5 ) ) ;
System . out. println ( Math . pow ( 2 , 3 ) ) ;
3. 异常机制
3.1 碰到过的异常
1 空指针异常
2 下标越界异常
3 类型转换异常
4 栈内存溢出
3.2 概述
异常是Java 中提供的一种识别及响应错误情况的一致性机制。有效地异常处理能使程序更加健壮、易于调试。
异常发生的原因有很多,比如:
1 ) 用户输入了非法数据
2 ) 要打开的文件不存在
3 ) 网络通信时连接中断
4 ) JVM内存溢出
5 ) 这些异常有的是因为用户错误引起,有的是程序错误引起的,还有其它一些是因为物理错误引起的。
* 异常 其实就是错误的一种说法, 在java中 有一个专门模拟所有异常和错误的类 Throwable , 是所有异常类的父类
*
* try . . . catch . . . : 处理异常, 一般用于客户端
*
* throws : 抛出异常, 一般用于服务端
*
* throw : 异常源点
*
* finally : 必须执行的语句
3.3 继承体系
3.4 Error
系统内部错误,这类错误由系统进行处理,程序本身无需捕获处理。
比如:OOM ( 内存溢出错误) 、VirtualMachineError ( 虚拟机错误) 、StackOverflowError ( 堆栈溢出错误) 等,一般发生这种情况,JVM会选择终止程序。
3.5 Exception
3.5.1 概述
Exception 是所有异常类的父类。分为非RuntimeException 和RuntimeException 。
· 非RuntimeException
指程序编译时需要捕获或处理的异常,如IOException 、自定义异常等。属于checked异常。
· RuntimeException
指程序编译时不需要捕获或处理的异常,如:NullPointerException 等。属于unchecked异常。一般是由程序员粗心导致的。如空指针异常、数组越界、类型转换异常等。
3.5.2 常用方法
3.5.3 Try…catch…
* try {
* 高风险代码
* 只要出错, 就执行catch , try 中剩余代码不再执行
* 如果没出错, try 就可以顺利执行完成, 并且catch 不再执行
* } catch ( 异常类型 变量) {
* 处理方案;
try {
FileInputStream fis = new FileInputStream ( "123.txt" ) ;
System . out. println ( "=========" ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
String msg = e. getMessage ( ) ;
System . out. println ( msg) ;
System . out. println ( "xxxxxxxxxx" ) ;
}
System . out. println ( 1111 ) ;
* try {
* 高风险代码;
* } catch ( 异常类型 变量) {
* 处理措施;
* } catch ( 异常类型 变量) {
* 处理措施;
* } catch ( 异常类型 变量) {
* 处理措施;
* } . . . . .
*
* 可以写多个catch , 但是 从上往下 必须是从子类到父类, 或者没有继承关系
try {
new FileInputStream ( "123" ) ;
String stre = null ;
System . out. println ( stre. trim ( ) ) ;
} catch ( FileNotFoundException e) {
System . out. println ( "找不到指定文件" ) ;
} catch ( IOException e) {
} catch ( NullPointerException e) {
System . out. println ( "不能为空" ) ;
} catch ( Exception e) {
System . out. println ( "其他异常" ) ;
}
* try {
*
* } catch ( 异常类型1 | 异常类型2 | 异常类型3 . . . ) {
*
* }
*
* 同时捕捉多个异常, 并且多个异常类型直接用 | 隔开, 并且 不能有继承关系
* 如果有继承关系 则直接写父类即可
public static void main ( String [ ] args) {
try {
new FileInputStream ( "123" ) ;
} catch ( FileNotFoundException | NullPointerException e) {
}
}
3.5.4 Throws
* throws 抛出异常, 该方式并不会处理理解, 一种提醒机制, 告诉调用人员, 这里可能会有错误
*
* 如果 知道如何解决 则使用try 解决问题, 否则就要通过 throws 抛出问题
*
* 只要你的下家提醒了你, 要么你解决, 要么抛给上家
public static void main ( String [ ] args) {
try {
m1 ( ) ;
} catch ( Exception e) {
}
System . out. println ( "====" ) ;
}
public static void m1 ( ) throws FileNotFoundException , Exception {
m2 ( ) ;
}
public static void m2 ( ) throws FileNotFoundException {
m3 ( ) ;
}
public static void m3 ( ) throws FileNotFoundException {
new FileInputStream ( "123" ) ;
}
3.5.5 Finally
* finally : 必须执行的语句块, 不能单独使用, 必须和try 或者try . . catch . . 一起使用
*
* finally 只有一种不执行的情况, 那就是 System . exit ( 0 ) ;
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 ( "必须执行" ) ;
}
}
public static void main ( String [ ] args) {
int result = m1 ( ) ;
System . out. println ( result) ;
}
public static int m1 ( ) {
int i = 10 ;
try {
return i++ ;
} catch ( Exception e) {
} finally {
System . out. println ( i + "-------" ) ;
return i;
}
}
public static void main ( String [ ] args) {
FileInputStream fis = null ;
try {
fis = new FileInputStream ( "D://123.txt" ) ;
System . out. println ( "执行成功" ) ;
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( fis != null ) {
System . out. println ( "关闭成功" ) ;
fis. close ( ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
* java7新特征, 自动关闭资源
*
* try ( 开启资源语句; ) {
* 高风险代码;
* } catch ( 异常类型 变量) {
* 处理方案;
* }
public static void main ( String [ ] args) {
try (
FileInputStream fis = new FileInputStream ( "123" ) ;
) {
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
3.5.6 不能有更宽泛的异常
* 方法覆写, 不能比原方法有更宽泛的异常
*
* 子类方法抛出的异常, 不能大于父类异常, 也就是 小于等于 父类异常均可
* 要么和父类抛出的异常一致, 要么是父类抛出异常类的子类, 或者 直接不抛出异常
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 {
}
}
3.6 自定义异常类
3.6.1 需求
完成用户登陆功能
传入用户信息, 校验用户名和密码是否正确, 如果正确则返回该用户所有信息, 反之则提示错误信息
3.6.2 实体类
* 用户模块
public class User {
private String username;
private String password;
private String nickname;
public User ( String username, String password) {
super ( ) ;
this . username = username;
this . password = password;
}
@Override
public String toString ( ) {
return "User [username=" + username + ", password=" + password
+ ", nickname=" + nickname + "]" ;
}
public String getUsername ( ) {
return username;
}
public void setUsername ( String username) {
this . username = username;
}
public String getPassword ( ) {
return password;
}
public void setPassword ( String password) {
this . password = password;
}
public String getNickname ( ) {
return nickname;
}
public void setNickname ( String nickname) {
this . nickname = nickname;
}
public User ( String username, String password, String nickname) {
super ( ) ;
this . username = username;
this . password = password;
this . nickname = nickname;
}
public User ( ) {
super ( ) ;
}
}
3.6.3 异常类
* 自定义异常类
*
* 1 继承一个已有的异常类
* 如果是运行时异常, 则继承RuntimeException 反之则继承 Exception 即可
* 一般情况下, 我们写的 一定是编译时异常
* 2 公共的无参构造
* 3 公共的有参构造, 参数是错误信息, 然后通过super 把错误信息传递到父类
public class UserException extends Exception {
public UserException ( ) {
}
public UserException ( String msg) {
super ( msg) ;
}
}
3.6.4 Controller
public class UserController {
private static UserService userService = new UserService ( ) ;
public static void main ( String [ ] args) {
Scanner scanner = new Scanner ( System . in) ;
while ( true ) {
System . out. println ( "请选择功能 : \n \t 1 注册 2 登陆" ) ;
int value = scanner. nextInt ( ) ;
if ( value == 1 ) {
System . out. println ( "请输入用户名 : " ) ;
String username = scanner. next ( ) ;
System . out. println ( "请输入昵称 : " ) ;
String nickname = scanner. next ( ) ;
System . out. println ( "请输入密码 : " ) ;
String password = scanner. next ( ) ;
System . out. println ( "再次输入密码 : " ) ;
String password2 = scanner. next ( ) ;
while ( ! password. equals ( password2) ) {
System . out. println ( "两次密码不一致,请再次输入!" ) ;
System . out. println ( "请输入密码 : " ) ;
password = scanner. next ( ) ;
System . out. println ( "再次输入密码 : " ) ;
password2 = scanner. next ( ) ;
}
User user = new User ( username, password, nickname) ;
try {
userService. add ( user) ;
System . out. println ( "注册成功 : " + user) ;
} catch ( UserException e) {
System . out. println ( "注册失败 : " + e. getMessage ( ) ) ;
}
} else if ( value == 2 ) {
System . out. println ( "请输入用户名 : " ) ;
String username = scanner. next ( ) ;
System . out. println ( "请输入密码 : " ) ;
String password = scanner. next ( ) ;
User inputUser = new User ( username, password) ;
try {
User loginUser = userService. login ( inputUser) ;
System . out. println ( "登陆成功 : " + loginUser) ;
} catch ( UserException e) {
System . out. println ( "登陆失败 : " + e. getMessage ( ) ) ;
}
}
}
}
}
3.6.5 Data
* 模拟数据库
* public class Database {
private static User [ ] users = null ;
static {
users = new User [ 2 ] ;
users[ 0 ] = new User ( "admin" , "root" , "管理员" ) ;
users[ 1 ] = new User ( "test" , "1234" , "测试人员" ) ;
}
public static User [ ] getUser ( ) {
return users;
}
public static void add ( User user) {
User [ ] newUsers = new User [ users. length+ 1 ] ;
System . arraycopy ( users, 0 , newUsers, 0 , users. length) ;
newUsers[ newUsers. length- 1 ] = user;
users = newUsers;
}
}
3.6.6 Dao
public class UserDao {
public User queryUserByUsername ( String username) {
User [ ] users = Database . getUser ( ) ;
for ( int i = 0 ; i < users. length; i++ ) {
if ( username. equals ( users[ i] . getUsername ( ) ) ) {
return users[ i] ;
}
}
return null ;
}
public void add ( User user) {
Database . add ( user) ;
}
}
3.6.7 Service
public class UserService {
private UserDao userDao = new UserDao ( ) ;
public User login ( User user) throws UserException {
User oldUser = userDao. queryUserByUsername ( user. getUsername ( ) ) ;
if ( oldUser == null ) {
throw new UserException ( "用户名不存在" ) ;
}
if ( ! oldUser. getPassword ( ) . equals ( user. getPassword ( ) ) ) {
throw new UserException ( "密码不正确" ) ;
}
return oldUser;
}
public void add ( User user) throws UserException {
User oldUser = userDao. queryUserByUsername ( user. getUsername ( ) ) ;
if ( oldUser != null ) {
throw new UserException ( "用户名已存在" ) ;
}
userDao. add ( user) ;
}
}