//BigInteger
BigInteger i1 =newBigInteger("12343124341234");
BigInteger i2 =newBigInteger("374683744957");
BigInteger sum = i1.add(i2);// BigInteger转换成 long
System.out.println(i1.longValueExact());//BigDecimal
BigDecimal bd =newBigDecimal("123.233");
System.out.println(bd.multiply(bd));//BigDecimal 用 scale() 表示小数位数
System.out.println(bd.scale());//3
BigDecimal bd2 = bd.stripTrailingZeros();//去掉末尾的0
BigDecimal d3 =newBigDecimal("1234500");
BigDecimal d4 = d3.stripTrailingZeros();
System.out.println(d3.scale());// 0
System.out.println(d4.scale());// -2//比较BigDecimal的值是否相等,必须使用compareTo()而不能使用equals()。//生成 [min, max]范围内的随机数double min =0, max =0;double x = Math.random();// x 的范围是[0,1]// x - 0 / 1 - 0 = random - min / max - min
x = x *(max - min)+ min;int y =(int) x;//随机整数// 安全的随机数
SecureRandom sr = null;try{
sr = SecureRandom.getInstanceStrong();}catch(NoSuchAlgorithmException e){
sr =newSecureRandom();}int[] n =newint[3];
sr.nextInt();//填充随机数
publicclasshello{publicstaticvoidmain(String[] args)throws UnsupportedEncodingException
{// 异常处理/*
必须捕获的异常,包括Exception及其子类,但不包括RuntimeException及其子类,
这种类型的异常称为Checked Exception。
不需要捕获的异常,包括Error及其子类,RuntimeException及其子类。
在方法定义的时候,使用 throw xx表示该方法可能抛出的类型,
只要是方法声明的checked exception, 不在调用层捕获,也必须在更高层中捕获,
最终必须在main()方法中捕获,
*/byte[] bs =toGBK("中文");
System.out.println(Arrays.toString(bs));}staticbyte[]toGBK(String s)throws UnsupportedEncodingException
{return s.getBytes("GBK");}staticbyte[]toGB(String s){try{return s.getBytes("GBK");}catch(UnsupportedEncodingException e){
System.out.println(e);
e.printStackTrace();// 打印异常栈return s.getBytes();}}}//多catch语句,多个catch从上到下匹配,多个catch语句只能有一条语句被执行//finally 语句: 确保有无错误都被执行
String a ="12";
String b ="x9";// TODO: 捕获异常并处理int c =stringToInt(a);int d =stringToInt(b);
System.out.println(c * d);}staticintstringToInt(String s)throws NumberFormatException
{
s = s.trim();
StringBuilder ss =newStringBuilder();for(int i =0; i < s.length(); i++)if(s.charAt(i)>=(int)'0'&&s.charAt(i)<=(int)'9'){}int res =-1;try{
res = Integer.parseInt(s);}catch(NumberFormatException e){
e.printStackTrace();}finally{return res;}//抛出异常// try {// process1();// } catch (Exception e)// {// e.printStackTrace();// }//因为只能抛出一个异常,没有被抛出的异常被称为被屏蔽的异常//如果想要获知所有的异常,// 先用origin保存原始异常,然后调用throwable.addSuppressed()将原始异常添加
Exception origin = null;try{
System.out.println(Integer.parseInt("abc"));}catch(Exception e){
origin = e;throw e;}finally{
Exception e =newIllegalArgumentException();if(origin != null){
e.addSuppressed(origin);}throw e;}}staticvoidprocess1(){try{process2();}catch(NullPointerException e){thrownewIllegalArgumentException(e);}}staticvoidprocess2(){thrownewNullPointerException();}
Logger logger = Logger.getLogger(hello.class.getName());
logger.info("Start process...");try{"".getBytes("invalidCharsetName");}catch(UnsupportedEncodingException e){// TODO: 使用logger.severe()打印异常
logger.severe(e.toString());}
logger.info("Process end.");// 使用日志,为了替代System.out.println() 可以定义到格式
JDK的Logging定义了7个日志级别,从严重到普通:
SEVERE
WARNING
INFO
CONFIG
FINE
FINER
FINEST
因为默认级别是INFO,因此,INFO级别以下的日志,不会被打印出来。
使用日志级别的好处在于,调整级别,就可以屏蔽掉很多调试相关的日志输出。
Logger logger = Logger.getGlobal();
logger.info("start process...");
logger.warning("memory is running out...");
logger.fine("ignored");
logger.severe("process will be terminated");