zoukankan      html  css  js  c++  java
  • 基本类型包装类、System类、Math类、Arrays类、大数据运算

    1 基本类型包装类

    Java中想对8种基本数据类型进行复杂操作很困难。

    实际程序界面上用户输入的数据都是以字符串类型进行存储的。

    程序开发中,需要把字符串转换成指定的基本数据类型。

    1.1基本数据类型对象包装类

    定义:java将基本数据类型值封装成了对象,提供更多的操作基本数值的功能。

    8种基本类型对应的包装类:

     

    Tipsint对应的是Integerchar对应的Character,其他6个都是基本类型首字母大写。

    1.2字符串与基本数据类型的转换

    1.2.1字符串转成基本类型(这些方法分别在8个类里面

     

    例:

    public class Changes {
    	public static void main(String[] args) {
    		String str="12";
    		System.out.println(str+1);
    		
    		//字符串转int
    		int strInt=Integer.parseInt(str);
    		System.out.println(strInt+1);
    		
    		//字符串转double
    		String s2="12.2";
    		double s2d=Double.parseDouble(s2);
    		System.out.println(s2d+1);		
    	}
    }
    

    注意必须是正确的数值,如果乱写会报格式异常:

    1.2.2基本数值转成字符串(三种方式)

    1基本类型直接与””相连接 (直接与空串连接)

    2调用StringvalueOf方法(静态方法)

     

    3)包装类的toString方法(只有第一个是Object的重写

    例:

    //基本数据类型转字符串
    public class Change2 {
    	public static void main(String[] args) {
    		//直接加空串
    		String s1=12+"";
    		
    		//valueOf方法
    		String s2=String.valueOf(6.6);
    		System.out.println(s1+s2);
    		
    		//toString方法
    		String s3=Integer.toString(6666);
    		System.out.println(s3+1);
    	}
    }
    

    1.3基本类型和对应的包装类对象转换

    1.3.1基本数值---->包装对象(两种方法)

      11构造方法

      2valueof方法

    1.3.2包装对象---->基本数值(重要)

      intValue方法

    例:

    public class Change3 {
    	public static void main(String[] args) {
    		//基本类型转包装类
    		//1构造方法
    		Integer in=new Integer(12);
    		Integer in2=new Integer("123");
    		
    		//2valueof方法
    		Integer in3=Integer.valueOf(45);
    		Integer in4=Integer.valueOf("456");
    		
    		//包装类转基本类型
    		int i=in.intValue();		
    	}
    }
    

    1.4自动装箱拆箱

    基本类型可以使用运算符直接进行计算,但是引用类型不可以。

    引用数据类型变量的值必须是new出来的内存空间地址值

    1.4.1概念

    自动拆箱:对象自动直接转成基本数值

    自动装箱:基本数值自动直接转成对象

    例:

    public class Autochange {
    	public static void main(String[] args) {
    		//自动装箱
    		Integer in=1; //相当于Integer in=new Integer(1);
    		
    		//自动拆箱
    		int sum=in+2; //相当于int sum=in.intValue()+2;
    		
    		System.out.println(sum);		
    	}
    }
    

    前面学过集合,例如ArrayList<Integer>,里面的类型是Integer,但是当用add()方法时,

    加进去的是数字,这就是自动装箱。

    1.4.2 jdk1.5以后自动装箱拆箱才可以用

    例:在项目上,右键---Properties

     

    可以看到报错了。

    1.4.3自动装箱byte常量池

    例:

    public class Demo1 {
    	public static void main(String[] args) {
    		Integer in1=128;
    		Integer in2=128;
    		System.out.println(in1==in2);
    		System.out.println(in1.equals(in2));
    	}
    }
    

    虽然是自动装箱,但是地址不同,只是值相同。 

    但是,如果是:

    public class Demo1 {
    	public static void main(String[] args) {
    		//byte范围内
    		Integer in3=50;
    		Integer in4=50;
    		System.out.println(in3==in4); //指向同一个地址
    		System.out.println(in3.equals(in4));
    		
    	}
    }
    

    当数据在byte范围内,数据在常量池中,进行自动装箱,不会新创建对象空间而是使用已有的空间。所以地址也相同了。

    2 System类

    2.1定义

    System类代表程序所在系统,提供了对应的一些系统属性信息,和系统操作。

    System类不能手动创建对象,因为构造方法被private修饰

    System类中的都是static方法,类名访问即可

    2.2字段

    (这个以后学习IO流时再了解)

    2.3常用方法

    1)currentTimeMillis() 获取当前系统时间与1970010100:00点之间的毫秒差值

    2)exit(int status) 用来结束正在运行的Java程序。参数是数值,传入0为正常状态,其他为异常状态。

    例:

    public class demo1 {
    	public static void main(String[] args) {
    		for(int i=0;i<10;i++){
    			if(i==5){
    				System.exit(0);
    			}
    			System.out.print(i+" ");
    		}
    	}
    }
    

    3)gc() 用来运JVM中的垃圾回收器,完成内存中垃圾的清除。

    (gc的链接)

     

    4)getProperty(String key) 用来获取指定键(字符串名)中所记录的系统属性信息

    例:

    public class Demo2 {
    	public static void main(String[] args) {
    		//获取系统所有属性信息
    		System.out.println(System.getProperties());
    	}
    }
    

    5)复制数组

    例:

    public class Demo3 {
    	public static void main(String[] args) {
    		int[] src={1,2,3,4,5};
    		int[] desc=new int[5];
    		System.arraycopy(src, 1, desc, 1, 3); //两个数组长度一定要对才行
    		for(int i=0;i<desc.length;i++){
    			System.out.print(desc[i]+" ");
    		}
    	}
    }
    

    注意数组长度,否则会报数组越界异常。

     

    2.4练习

    1)验证for循环打印数字1-9999所需要使用的时间(毫秒)

    public class Demo4 {
    	public static void main(String[] args) {
    		long before=System.currentTimeMillis();
    		for(int i=1;i<10000;i++){
    			System.out.println(i);
    			
    		}
    		long after=System.currentTimeMillis();
    		System.out.println("程序执行的时间为"+(after-before));
    	}
    }
    

    2)src数组中前3个元素,复制到dest数组的前3个位置上

    public class Demo05 {
    	public static void main(String[] args) {
    		int[] src={1,2,3,4,5};
    		int[] dest={6,7,8,9,10};
    		System.arraycopy(src, 0, dest, 0, 3);	
    		
    		for(int i=0;i<dest.length;i++){
    			System.out.print(dest[i]+" ");
    		}
    	}
    }
    

    4)循环,随机生成100-999之间的的三位数并进行打印该数,当该数能被10整除时,结束运行的程序

    import java.util.Random;
    public class Demo06 {
    	public static void main(String[] args) {
    		Random r=new Random();		
    		while(true){
    			int n=r.nextInt(900)+100;
    			System.out.println(n);
    			if(n%10==0){
    				System.exit(0);
    			}
    		}
    	}
    }
    

    3 Math类

    数学工具类

    类似这样的工具类,其所有方法均为静态方法,并且一般不会创建对象。

    3.1常用方法

     

    例:

    public class MathTest {
    	public static void main(String[] args) {
    		//向上取整
    		System.out.println(Math.ceil(2.6));
    		
    		//向下取整
    		System.out.println(Math.floor(2.6));
    		
    		//取次幂
    		System.out.println(Math.pow(2, 10));
    		
    		//取随机数
    		System.out.println(Math.random());
    		
    		//四舍五入
    		System.out.println(Math.round(2.3));
    		System.out.println(Math.round(2.5));
    	}
    } 
    

    注意:除了四舍五入,其他的返回值都是double

    记忆:

    4 Arrays类

    此类包含用来操作数组(比如排序和搜索)的各种方法。需要注意,如果指定数组引用为 null,则访问此类中的方法都会抛出空指针异常NullPointerException

    null.调用 都是空指针异常

    4.1常用方法

    例:

    import java.util.Arrays;
    public class ArrayTest {
    	public static void main(String[] args) {
    		method1();
    		method2();
    		method3();
    	}
    	
    	//查找一个有序数组中某个值的位置
    	public static void method1(){
    		int[] arr={1,5,9,11,13,15};
    		System.out.println("method1的结果为:"+Arrays.binarySearch(arr, 11));		
    	}
    	
    	//升序排序
    	public static void method2(){
    		System.out.println("method2的结果为:");
    		int[] arr={1,8,2,7,5,10,9};
    		Arrays.sort(arr);
    		for(int i=0;i<arr.length;i++){
    			System.out.print(arr[i]+" ");
    		}
    		
    		System.out.println();		
    		char[] ch={'z','a','c','A'};
    		Arrays.sort(ch);		
    		for(int i=0;i<ch.length;i++){
    			System.out.print(ch[i]+" ");
    		}		
    	}
    	
    	//数组转字符串
    	public static void method3(){
    		System.out.println();
    		int[] arr={1,5,6,8,2,3};
    		String str=Arrays.toString(arr);
    		System.out.println("method1的结果为:"+str);
    	}	
    }
    

    说明:

    1)sort方法,数组中的元素升序排序(字符数组是按ASCII值排)

    2toString方法数组转为(带着数组格式的)String

    3binarySearch,底层采用二分查找的算法(https://baike.so.com/doc/6740981-6955489.html

    必须是有序数组

    如果该值不存在,则返回下标=-该值应该所在的位置-1

    例:

    public static void method1(){
    	int[] arr={1,5,9,11,13,15};
    	System.out.println("Arrays.binarySearch(arr, 12));		
    }
    

    结果为-5

    4.2练习:

    定义一个方法,接收一个数组,数组中存储10个学生考试分数,该方法要求返回考试分数最低的后三名考试分数。

    import java.util.Arrays;
    public class ArraysTest2 {
    	public static void main(String[] args) {
    		double[] score={100,60,78,99,77.5,45.5,90,97,33,88.5};
    		double[] score2=sorts(score);
    		
    		System.out.println("后三名成绩为:");
    		for(int i=0;i<3;i++){
    			System.out.println(score2[i]);
    		}
    	}
    	
    	public static double[] sorts(double[] score){
    		Arrays.sort(score);
    		return score;
    	}
    }
    

    5大数据运算

    java中long型为最大整数类型。超过long型的整数已经不能被称为整数了,它们被封装成BigInteger对象。

    BigInteger类中,实现四则运算都是方法来实现,并不是采用运算符。

    5.1常用构造方法

     

    5.2运算

    例:

    import java.math.BigInteger;
    public class MyTest {
    	public static void main(String[] args) {
    		BigInteger bin1=new BigInteger("1111111111111111111111111111111111111111111111");
    		BigInteger bin2=new BigInteger("9999999999999999999999999999999999999999999999");
    		//加法
    		System.out.println(bin1.add(bin2));		
    		//减法
    		System.out.println(bin2.subtract(bin1));
    		//乘法
    		System.out.println(bin1.multiply(bin2));
    		//除法
    		System.out.println(bin2.divide(bin1));	
    	}
    }
    

    5.3 BigDecimal类

    例:

    public class MyTest {
    	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);	
    	}
    }
    

    结果总是进不上位,是丢失精度的。

     

    doublefloat类型在运算中很容易丢失精度,造成数据的不准确性,所以java提供BigDecimal类可以实现浮点数据的高精度运算

    5.3.1常用构造方法

     

    建议浮点数据以字符串形式给出,因为参数结果是可以预知的。

    5.3.2除法运算的保留和选择舍入模式

     

    对于浮点数据的除法运算,和整数不同,可能出现无限不循环小数,因此需要对所需要的位数进行保留和选择舍入模式。 

    scale是保留的小数位数

    第二个参数中:向上取整,向下取整,四舍五入,比较常用。 

    例:

    import java.math.BigDecimal;
    public class MyTest {
    	public static void main(String[] args) {
    		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("111.301");
    		BigDecimal bd6=new BigDecimal("100");
    		System.out.println(bd5.divide(bd6, 2, BigDecimal.ROUND_CEILING)); //保留两位小数,向上取整	
    	}
    }
    

  • 相关阅读:
    c++ socket 出现绑定失败的一个特殊原因。Bind failed Error:10049
    解决OCX 在 非开发电脑上注册出错的问题
    JAVASCRIPT 调用 其他应用程序的方法
    JAVASCRIPT 调用 OCX 的那些坑
    关于socket通信bind()返回值错误:10049
    WPF LiveChart示例
    .NET Core 2.1 IIS 部署 出现500.19 错误
    文件上传大小限制
    winform httpclient 多文件上传
    VS Code中添加程序集安装包即添加DLL引用
  • 原文地址:https://www.cnblogs.com/hzhjxx/p/10094096.html
Copyright © 2011-2022 走看看