public class Main
{ /**
* * A:为什么会有基本类型包装类
* 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。
* B:常用操作
* 常用的操作之一:用于基本数据类型与字符串之间的转换。
* C:基本类型和包装类的对应
*
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
*/
public static void main(String[] args) {
System.out.println(Integer.toBinaryString(60));
System.out.println(Integer.toHexString(60));
System.out.println(Integer.toOctalString(60));
//Integer的两种常用字段
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
demo1();
demo2();
//String 变成 int
String s = "100";
Integer i1 = new Integer(s);
int i4=i1.intValue();
System.out.println(i4);
//其中s必须是要字符串类型的数字! 将字符串解析为int parseXxx类型方法的用法(其中 Character没有这个方法)
int i5=Integer.parseInt(s);
System.out.println(i5);
double d1=Double.parseDouble(s); //将字符串解析为double
System.out.println(d1);
String s2 = "true";
boolean b1 = Boolean.parseBoolean(s2);//将字符串解析为boolean
System.out.println(b1);
}
public static void demo2() {
//int 变成String 的四种方法 推荐用第一种和第二种
int i = 123456789;
String s = i+" ";
System.out.println(s);
String s2 = String.valueOf(i);
System.out.println(s2);
Integer i2 = new Integer(i);
String s3 =i2.toString();
System.out.println(s3);
String s4=Integer.toString(i);
System.out.println(s4);
}
public static void demo1() {
//Integer的两种构造方法
Integer i1 = new Integer(100);
System.out.println(i1);
//Integer i2 = new Integer("abc"); // java.lang.NumberFormatException 数字格式转换错误
Integer i2 = new Integer(129);
Integer i3 = new Integer("128");
System.out.println(i2);
}
}
经典面试题目 (以及Integer的源码分析!)
public class Demo5_Integer {
/**
* @param args
*/
public static void main(String[] args) {
Integer i1 = new Integer(97);
Integer i2 = new Integer(97);
System.out.println(i1 == i2); //比较地址值 false
System.out.println(i1.equals(i2)); //一般对象都会重写euqals方法 true
System.out.println("-----------");
Integer i3 = new Integer(197);
Integer i4 = new Integer(197);
System.out.println(i3 == i4); //false
System.out.println(i3.equals(i4)); //true
System.out.println("-----------");
Integer i5 = 127;
Integer i6 = 127;
System.out.println(i5 == i6); //true
System.out.println(i5.equals(i6)); //true
System.out.println("-----------");
Integer i7 = 128;
Integer i8 = 128;
System.out.println(i7 == i8); //false
System.out.println(i7.equals(i8)); //true
/*
* -128到127是byte的取值范围,如果在这个取值范围内,自动装箱就不会新创建对象,而是从常量池中获取
* 如果超过了byte取值范围就会再新创建对象
*
* public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high) //i>= -128 && i <= 127
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
*/
}
}
================================================================
BigInteger
* A:BigInteger的概述
* 可以让超过Integer范围内的数据进行运算
* B:构造方法
* public BigInteger(String val)
* C:成员方法
* public BigInteger add(BigInteger val)
* public BigInteger subtract(BigInteger val)
* public BigInteger multiply(BigInteger val)
* public BigInteger divide(BigInteger val)
* public BigInteger[] divideAndRemainder(BigInteger val)
import java.math.BigInteger;
public class Demo4_BigInteger {
public static void main(String[] args) {
//long num = 123456789098765432123L;
//String s = "123456789098765432123";
BigInteger bi1 = new BigInteger("100");
BigInteger bi2 = new BigInteger("2");
System.out.println(bi1.add(bi2)); //+
System.out.println(bi1.subtract(bi2)); //-
System.out.println(bi1.multiply(bi2)); //*
System.out.println(bi1.divide(bi2)); ///(除)
BigInteger[] arr = bi1.divideAndRemainder(bi2); //取除数和余数
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
BigDecimal
* A:BigDecimal的概述
* 由于在运算的时候,float类型和double很容易丢失精度,演示案例。
* 所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal
* 不可变的、任意精度的有符号十进制数。
* B:构造方法
* public BigDecimal(String val)
* C:成员方法
* public BigDecimal add(BigDecimal augend)
* public BigDecimal subtract(BigDecimal subtrahend)
* public BigDecimal multiply(BigDecimal multiplicand)
* public BigDecimal divide(BigDecimal divisor)
* D:案例演示
* BigDecimal类的构造方法和成员方法使用
十进制表示1/3
0.3333333333333333333333333333333333333333
import java.math.BigDecimal;
public class Demo5_BigDecimal {
public static void main(String[] args) {
//System.out.println(2.0 - 1.1);
/*BigDecimal bd1 = new BigDecimal(2.0); //这种方式在开发中不推荐,因为不够精确
BigDecimal bd2 = new BigDecimal(1.1);
System.out.println(bd1.subtract(bd2));*/
/*BigDecimal bd1 = new BigDecimal("2.0"); //通过构造中传入字符串的方式,开发时推荐
BigDecimal bd2 = new BigDecimal("1.1");
System.out.println(bd1.subtract(bd2));*/
BigDecimal bd1 = BigDecimal.valueOf(2.0); //这种方式在开发中也是推荐的
BigDecimal bd2 = BigDecimal.valueOf(1.1);
System.out.println(bd1.subtract(bd2));
}
}