你们回想下,在次日咱们学习Java中的基本数据类型时,说Java中有8种基本的数据类型,但是这些数据是基本数据,想对其进行复杂操做,变的很难。怎么办呢java
在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的。而程序开发中,咱们须要把字符串数据,根据需求转换成指定的基本数据类型,如年龄须要转换成int类型,考试成绩须要转换成double类型等。那么,想实现字符串与基本数据之间转换怎么办呢数组
Java中提供了相应的对象来解决该问题,基本数据类型对象包装类:java将基本数据类型值封装成了对象。封装成对象有什么好处?能够提供更多的操做基本数值的功能dom
8种基本类型对应的包装类以下:ide
其中须要注意int对应的是Integer,char对应的Character,其余6个都是基本类型首字母大写便可函数
parseXXX(String s);其中XXX表示基本类型,参数为能够转成基本类型的字符串,若是字符串没法转成基本类型,将会发生数字转换的问题 NumberFormatException工具
System.out.println(Integer.parseInt("123") + 2); //打印结果为 125
将基本数值转成字符串有3种方式学习
调用包装类中的toString方法;Integer.toString(34)大数据
使用int类型与Integer对象转换进行演示,其余基本类型转换方式相同ui
基本数值---->包装对象spa
Integer i = new Integer(4);//使用构造函数函数 Integer ii = new Integer("4");//构造函数中能够传递一个数字字符串
Integer iii = Integer.valueOf(4);//使用包装类中的valueOf方法 Integer iiii = Integer.valueOf("4");//使用包装类中的valueOf方法
int num = i.intValue();
在须要的状况下,基本类型与包装类型能够通用。有些时候咱们必须使用引用数据类型时,能够传入基本数据类型
好比: 基本类型能够使用运算符直接进行计算,可是引用类型不能够。而基本类型包装类做为引用类型的一种却能够计算,缘由在于,Java”偷偷地”自动地进行了对象向基本数据类型的转换
相对应的,引用数据类型变量的值必须是new出来的内存空间地址值,而咱们能够将一个基本类型的值赋值给一个基本类型包装类的引用。缘由一样在于Java又”偷偷地”自动地进行了基本数据类型向对象的转换
Integer i = 4;//自动装箱。至关于Integer i = Integer.valueOf(4); i = i + 5;//等号右边:将i对象转成基本数值(自动拆箱) i.intValue() + 5; 加法运算完成后,再次装箱,把基本数值转成对象
在API中System类介绍的比较简单,咱们给出定义,System中表明程序所在系统,提供了对应的一些系统属性信息,和系统操做
System类不能手动建立对象,由于构造方法被private修饰,阻止外界建立对象。System类中的都是static方法,类名访问便可。在JDK中,有许多这样的类
arraycopy方法,用来实现将源数组部分元素复制到目标数组的指定位置
- 验证for循环打印数字1-9999所须要使用的时间(毫秒)
```
public static void main(String[] args) { long start = System.currentTimeMillis(); for (int i=0; i<10000; i++) { System.out.println(i); } long end = System.currentTimeMillis(); System.out.println("共耗时毫秒:" + (end-start) ); } ```
- 练习二:将src数组中前3个元素,复制到dest数组的前3个位置上
复制元素前:src数组元素[1,2,3,4,5],dest数组元素[6,7,8,9,10]
复制元素后:src数组元素[1,2,3,4,5],dest数组元素[1,2,3,9,10]
```
public static void main(String[] args) { int[] src = new int[]{1,2,3,4,5}; int[] dest = new int[]{6,7,8,9,10}; System.arraycopy( src, 0, dest, 0, 3); 代码运行后:两个数组中的元素发生了变化 src数组元素[1,2,3,4,5] dest数组元素[1,2,3,9,10] } ```
- 练习三:循环生成100-999之间的的三位数并进行打印该数,当该数能被10整除时,结束运行的程序
public static void main(String[] args){
Random random = new Random();
while(true){ int number = random.nextInt(900)+100; //0-899 + 100 if (nmumber % 10 == 0) { System.exit(0); } } }
Math 类是包含用于执行基本数学运算的方法的数学工具类,如初等指数、对数、平方根和三角函数
相似这样的工具类,其全部方法均为静态方法,而且通常不会建立对象。如System类
double d1 = Math.abs(-5); // d1的值为5 double d2 = Math.abs(5); // d2的值为5
double d1 = Math.ceil(3.3); //d1的值为 4.0 double d2 = Math.ceil(-3.3); //d2的值为 -3.0 double d3 = Math.ceil(5.1); // d3的值为 6.0
double d1 = Math.floor(3.3); //d1的值为3.0 double d2 = Math.floor(-3.3); //d2的值为-4.0 double d3 = Math.floor(5.1); //d3的值为 5.0
double d1 = Math.max(3.3, 5.5); //d1的值为5.5 double d2 = Math.max(-3.3, -5.5); //d2的值为-3.3
double d1 = Math.min(3.3, 5.5); //d1的值为3.3 double d2 = Math.max(-3.3, -5.5); //d2的值为-5.5
double d1 = Math.pow(2.0, 3.0); //d1的值为 8.0 double d2 = Math.pow(3.0, 3.0); //d2的值为27.0
double d1 = Math.round(5.5); //d1的值为6.0 double d2 = Math.round(5.4); //d2的值为5.0
double d1 = Math.random();
此类包含用来操做数组(好比排序和搜索)的各类方法。须要注意,若是指定数组引用为 null,则访问此类中的方法都会抛出空指针异常NullPointerException
//源arr数组元素{1,5,9,3,7}, 进行排序后arr数组元素为{1,3,5,7,9} int[] arr = {1,5,9,3,7}; Arrays.sort( arr );
int[] arr = {1,5,9,3,7}; String str = Arrays.toString(arr); // str的值为[1, 3, 5, 7, 9]
int[] arr = {1,3,4,5,6}; int index = Arrays.binarySearch(arr, 4); //index的值为2 int index2= Arrasy.binarySearch(arr, 2); //index2的值为-1
public static int[] method(double[] arr){ Arrays.sort(arr); //进行数组元素排序(元素值从小到大进行排序) int[] result = new int[3]; //存储后三名考试分数 System.arraycopy(arr, 0, result, 0, 3);//把arr数组前3个元素复制到result数组中 return result; }
java中long型为最大整数类型,对于超过long型的数据如何去表示呢.在Java的世界中,超过long型的整数已经不能被称为整数了,它们被封装成BigInteger对象.在BigInteger类中,实现四则运算都是方法来实现,并非采用运算符
BigInteger类的构造方法:
构造方法中,采用字符串的形式给出整数
public static void main(String[] args) { //大数据封装为BigInteger对象 BigInteger big1 = new BigInteger("12345678909876543210"); BigInteger big2 = new BigInteger("98765432101234567890"); //add实现加法运算 BigInteger bigAdd = big1.add(big2); //subtract实现减法运算 BigInteger bigSub = big1.subtract(big2); //multiply实现乘法运算 BigInteger bigMul = big1.multiply(big2); //divide实现除法运算 BigInteger bigDiv = big2.divide(big1); }
在程序中执行下列代码,会出现什么问题? 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); double和float类型在运算中很容易丢失精度,形成数据的不许确性,Java提供咱们BigDecimal类能够实现浮点数据的高精度运算
构造方法以下:
建议浮点数据以字符串形式给出,由于参数结果是能够预知的
public static void main(String[] args) { //大数据封装为BigDecimal对象 BigDecimal big1 = new BigDecimal("0.09"); BigDecimal big2 = new BigDecimal("0.01"); //add实现加法运算 BigDecimal bigAdd = big1.add(big2); BigDecimal big3 = new BigDecimal("1.0"); BigDecimal big4 = new BigDecimal("0.32"); //subtract实现减法运算 BigDecimal bigSub = big3.subtract(big4); BigDecimal big5 = new BigDecimal("1.105"); BigDecimal big6 = new BigDecimal("100"); //multiply实现乘法运算 BigDecimal bigMul = big5.multiply(big6); BigDecimal b1 = new BigDecimal("1.301"); BigDecimal b2 = new BigDecimal("100"); // 除法 BigDecimal divide = b1.divide(b2); // 四舍五入 System.out.println(divide.setScale(2, BigDecimal.ROUND_HALF_UP)); }
对于浮点数据的除法运算,和整数不一样,可能出现无限不循环小数,所以须要对所须要的位数进行保留和选择舍入模式
基本类型包装类
基本类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
自动装箱、自动拆箱
经常使用方法
System类:系统属性信息工具类
Arrays类: 数组操做工具类
Math类:数学运算工具类