基本类型包装
就是转换用
8个类型 就是8个方法 除了int(Integer) 和char(Character)之外,其他6个都是首字母大写
字符串--->基本类型
得出结论
pare 基本数据类型 (String s)
基本数据类型转字符串3种:
① 双引号“” +基本类型
② 调用 valueOf 方法
③ 调用 toString 方法
public static void main(String[] args) {
//字符串->基本数据类型
String str="12";//字符串必须是一个整型的
int num=Integer.parseInt(str);
System.out.println(num+1);
double num2=Double.parseDouble(str);
System.out.println(num2);
//将基本类型-->字符串 第一种 双引号+基本类型
System.out.println(""+12+1);
//将int 类型转String 第二种 valueOf方法
String s1=String.valueOf(88);
String s2=String.valueOf(1.2);
System.out.println(s2+1);
//第三种 调用toString方法
String s3=Integer.toString(99);
System.out.println(s3+1);
}
调用String的valueOf方法;String.valueOf(34)
调用包装类中的toString方法;Integer.toString(34) ;
基本类型和对象转换
基本-->包装对象
包装-->基本
public static void main(String[] args) {
//基本数据类型-->包装类型对象
//1.
Integer in =new Integer(123);
Integer in2=new Integer("456");
//2.
Integer in3=Integer.valueOf(789);
Integer in4=Integer.valueOf("147");
//包装类型对象-->基本数据类型
int i1=in.intValue();
int i2=in2.intValue();
}
自动拆装箱
JDK1.5之后自动拆装箱
public static void main(String[] args) {
//jdk1.5以后自动拆装箱
//自动装箱:基本数据类型-->包装类型对象
Integer in=123;//Integer in=new Integer(123);
//自动拆箱:包装类型对象-->基本数据类型
int i=in+3;//int i=in.inValue()+3;
}
public static void main(String[] args) {
Integer in1=500;//Integer in1=new Integer(500);
Integer in2=500;//Integer in1=new Integer(500);
System.out.println(in1==in2);//false 当两边是对象时比地址
System.out.println(in1.equals(in2));//true equals比内容
//byte常量池 -128 - 127
Integer in3=500;//Integer in1=new Integer(500);
Integer in4=500;//Integer in1=new Integer(500);
System.out.println(in1==in2);//ture //因为byte类型 -128 - 127
System.out.println(in1.equals(in2));//true
}
pm
System类
常用方法:
gc:运行垃圾回收器
public class Person {
@Override
protected void finalize() throws Throwable {
System.out.println("对象被回收了");
}
}
-------------------------------------------------
package com.oracle.demo02;
//垃圾回收器
public class Demo02 {
public static void main(String[] args) {
new Person();
new Person();
new Person();
new Person();
new Person();
new Person();
//调用垃圾回收器
System.gc();
}
}
public static void main(String[] args) {
int[] arr={99,98,97,22,11,0};
int[] arr1=new int[3];
//将arr前三个数组元素赋值到arr1中
System.arraycopy(arr, 0, arr1, 0, 3);
//遍历
for(int i=0;i<arr1.length;i++){
System.out.println(arr1[i]);
}
}
Math类
数学工具的方法
public static void main(String[] args) {
//求绝对值
System.out.println(Math.abs(-1.2));
//向上取整:只要多一点就往上取
System.out.println(Math.ceil(12.1));
//向下取整
System.out.println(Math.floor(12.9));
//求两个值的最大值
System.out.println(Math.max(10,9));
//求两个值的最小值
System.out.println(Math.min(10,9));
//求次幂 2的10次方
System.out.println(Math.pow(2, 10));
//求随机数
System.out.println(Math.random());
//四舍五入的方法
System.out.println(Math.round(12.5));
}
Arrays类
技巧: 带s是array的工具类
常用方法
binarySearch:在数组中,查找元素值出现的位置,若没有,返回位置 - (这个值在的位置)-1。数组必须是有序的
int[] arr = {1,3,4,5,6};
int index = Arrays.binarySearch(arr, 4); //index的值为2
int index2= Arrasy.binarySearch(arr, 2); //index2的值为-2
Arrays类的方法练习
sort:数组中的排序(从小到大排序)
源arr数组元素{1,5,9,3,7}, 进行排序后arr数组元素为{1,3,5,7,9}
int[] arr = {1,5,9,3,7};
Arrays.sort( arr );
toString:数组内容的字符串
int[] arr = {1,5,9,3,7};
String str = Arrays.toString(arr); // str的值为[1, 3, 5, 7, 9]
大数据运算
BigInteger 常用方法
package com.oracle.demo02;
//大数据运算 加减乘除
import java.math.BigInteger;
public class Demo06 {
public static void main(String[] args) {
BigInteger in1=new BigInteger("100000000000000000000");
BigInteger in2=new BigInteger("100000000000000000000");
System.out.println(in1.add(in2));//加法
System.out.println(in1.subtract(in2));//减法
System.out.println(in1.multiply(in2));//乘法
System.out.println(in1.divide(in2));//除法
}
}
BigDecimal
double和float类型在运算中很容易丢失精度,造成数据的不准确性,Java提供我们BigDecimal类可以实现浮点数据的高精度运算
四则运算
package com.oracle.demo02;
//BigDecimal
import java.math.BigDecimal;
public class Demo07 {
public static void main(String[] args) {
//计算机是二进制,在计算时会丢失精度
System.out.println(0.09 + 0.01);
System.out.println(1.0 - 0.32);
System.out.println(1.015 * 100);
System.out.println(1.301 / 100);
//用BigDecimal运算
BigDecimal bd1=new BigDecimal("0.09");
BigDecimal bd2=new BigDecimal("0.01");
System.out.println(bd1.add(bd2));//加
BigDecimal bd3=new BigDecimal("1.0");
BigDecimal bd4=new BigDecimal("0.32");
System.out.println(bd3.subtract(bd4));//减
BigDecimal bd5=new BigDecimal("1.015");
BigDecimal bd6=new BigDecimal("100");
System.out.println(bd5.divide(bd6));//乘
BigDecimal bd7=new BigDecimal("1.301");
BigDecimal bd8=new BigDecimal("100");
System.out.println(bd1.add(bd2));//除
}
}