Java - 封装类

来源:互联网 发布:linux系统环境变量设置 编辑:程序博客网 时间:2024/06/15 07:43

基本数据类型和封装类对照表

基本数据类型封装类  byte    Byte  short     Short  int     Integer  long       Long  float       Float  double     Double  char       Character  booleanBoolean

基本概念

封装类功能将基本数据类型封装当对象操作为基本数据类型提供各种转换功能封装类一旦被创建,内容永不改变,如需封装新的内容,创建新对象通过修改封装类引用的指向来实现如同修改对象值封装类都是final的类,不能被继承

Integer

finalextends Numberimplements Comparable<Integer>

Integer常用方法

package com.itlwc;public class Test {public static void main(String[] args) {// int转型为Integernew Integer(10);Integer.valueOf(10);// int转型为StringInteger.toString(10);// int转型为String带进制Integer.toString(10, 2);Integer.toString(10, 8);Integer.toString(10, 16);Integer.toBinaryString(10);Integer.toOctalString(10);Integer.toHexString(10);// String转型为Integernew Integer("10");Integer.valueOf("10");Integer.decode("10");// String转型为Integer带进制Integer.valueOf("10", 2);Integer.valueOf("10", 8);Integer.valueOf("10", 16);// String转型为intInteger.parseInt("10");// String转型为int带进制Integer.parseInt("10", 2);Integer.parseInt("10", 8);Integer.parseInt("10", 16);// 返回符号 1为正 -1为负 0为0Integer.signum(10);// 返回bytenew Integer(10).byteValue();// 返回shortnew Integer(10).shortValue();// 返回intnew Integer(10).intValue();// 返回longnew Integer(10).longValue();// 返回doublenew Integer(10).doubleValue();// 返回floatnew Integer(10).floatValue();// 比较此对象与指定对象new Integer(10).equals(10);// 在数字上比较两个 Integer 对象new Integer(10).compareTo(10);// 最大值最小值int max = Integer.MAX_VALUE;int min = Integer.MIN_VALUE;}}

Integer的缓存机制

public class Test {public static void main(String[] args) {Integer a = 100;/* * 如果使用这种方法为i赋值 * Integer类有一个缓存机制 * Integer类认为认为-128~127之间的整数是我们经常用到的数值 * 因为100在这个范围就不生成对象,使用缓存好的对象 */Integer b = 100;if (a == b) {System.out.println("a==b");} else {System.out.println("a!=b");}Integer aa = 200;// 因为200不在这个范围生成新的对象Integer bb = 200;if (aa == bb) {System.out.println("aa==bb");} else {System.out.println("aa!=bb");}// 如果使用构造方法就没有缓存Integer aaa = new Integer(100);Integer bbb = new Integer(100);if (aaa == bbb) {System.out.println("aaa==bbb");} else {System.out.println("aaa!=bbb");}}}打印:a==baa!=bbaaa!=bbb

valueOf() VS parseXxx()

valueOf()可以以基本数据类型和字符串为参数,返回的是封装类对象的引用parseXxx()只能以字符串为参数,返回的是基本数据类型的值

isNaN()

判断Double,Float对象封装的值是否是NaN只有浮点数对应的封装类具有该方法该方法被重载了一个无参数的,非静态的,boolean isNaN()一个是有参数的,静态的,static boolean isNaN(x)x代表float或者double

案例

package com.itlwc;public class Test {public static void main(String[] args) {Double d = new Double(12.0%0);if(d.isNaN()){System.out.println("封装的对象值为NaN");}else{System.out.println("封装的对象值为"+d);}}}/*打印结果:封装的对象值为NaN*/

java.math.BigInteger

package com.itlwc;import java.math.BigInteger;public class Test {public static void main(String[] args) {BigInteger bi1 = new BigInteger("222222222222222222222");BigInteger bi2 = new BigInteger("111111111111111111111");System.out.println("bi1+bi2: "+ bi1.add(bi2));System.out.println("bi1-bi2: "+ bi1.subtract(bi2));System.out.println("bi1*bi2: "+ bi1.multiply(bi2));System.out.println("bi1/bi2: "+ bi1.divide(bi2));System.out.println("bi1%bi2: "+ bi1.mod(bi2));System.out.println("bi1的负数: "+ bi1.negate());//取bi1的符号if(bi1.signum()==1){System.out.println("bi1为整数");}else{System.out.println("bi1为负数");}//bi1与bi2的大小if(bi1.compareTo(bi2)>0){System.out.println("bi1大于bi2");}else{System.out.println("bi1小于bi2");}}}/*打印结果:bi1+bi2: 333333333333333333333bi1-bi2: 111111111111111111111bi1*bi2: 24691358024691358024641975308641975308642bi1/bi2: 2bi1%bi2: 0bi1的负数: -222222222222222222222bi1为整数bi1大于bi2*/

java.math.BigDecimal 精确计算

package com.itlwc;import java.math.BigDecimal;public class Test {public static void main(String[] args) {BigDecimal bd1 = new BigDecimal("3.8");BigDecimal bd2 = new BigDecimal("0.7");BigDecimal bd3 = new BigDecimal("3.800");System.out.println("bd1 = " + bd1);System.out.println("bd2 = " + bd2);System.out.println("bd3 = " + bd3);System.out.println("bd1 + bd2 = " + bd1.add(bd2));System.out.println("bd1 - bd2 = " + bd1.subtract(bd2));System.out.println("bd1 * bd2 = " + bd1.multiply(bd2));System.out.println("bd1 / bd2 = "+ bd1.divide(bd2, BigDecimal.ROUND_HALF_DOWN));System.out.println("bd3 / bd2 = "+ bd3.divide(bd2, BigDecimal.ROUND_HALF_UP));// 将bd1与bd2进行比较if (bd1.compareTo(bd3) == 0) {System.out.println("bd1 = " + bd1 + " bd3 = " + bd3+ " 则bd1与bd3是相等的!!");}// 1BigDecimal bigD1 = BigDecimal.ONE;// 10BigDecimal bigD2 = BigDecimal.TEN;// 0BigDecimal bigD3 = BigDecimal.ZERO;}}/*打印结果:bd1 = 3.8bd2 = 0.7bd3 = 3.800bd1 + bd2 = 4.5bd1 - bd2 = 3.1bd1 * bd2 = 2.66bd1 / bd2 = 5.4bd3 / bd2 = 5.429bd1 = 3.8 bd3 = 3.800 则bd1与bd3是相等的!!*/