达内课程-基本类型包装类BigInteger、BigDecimal使用
来源:互联网 发布:淘宝无线端首页模板 编辑:程序博客网 时间:2024/06/08 05:48
void f(Object obj){ //参数引用类型,保存的是内存地址}f(123){ //基本类型}
调用f()方法不能直接传一个基本类型,可以把基本类型封装成对象
八种基本类型
byte1 Byte
short2 Short
int4 Integer
long8 Long
float4 Float
double8 Double
boolean1 Character
char2 Boolean
java.lang.Number
数字包装类的抽象父类
子类:
Byte
Short
Integer
Long
Float
Double
BigDecimal
BigInteger
方法
取出内部封装的基本类型值
byteValue()
shortValue()
intValue()
longValue()
floatValue()
doubleValue()
Integer
创建对象
1、Integer i = new Integer(51);
2、Integer i = Integer.valueOf(51);
两种创建对象方式区别
Integer类中,存在Integer实例缓存数组
范围:-128 - 127
如果是范围内的值,使用存在的缓存对象
如果不是范围内的值,会新建对象
而第一种方式,一直在创建新对象
栗子
public class Test { public static void main(String[] args) { Integer a = new Integer(51);//新分配内存 Integer b = Integer.valueOf(51);//访问缓存对象 Integer c = Integer.valueOf(51);//访问缓存 /*三个新的内存地址 * Integer a = new Integer(888);//新分配内存 Integer b = Integer.valueOf(888);//新分配内存 Integer c = Integer.valueOf(888);//新分配内存*/ //b和c指向的内存地址是相同的 System.out.println(a == b); System.out.println(b == c); //Integer类中重写了equals方法 //比较内部封装的值 System.out.println(a.equals(b)); System.out.println(a.byteValue()); System.out.println(a.shortValue()); System.out.println(a.intValue()); System.out.println(a.longValue()); System.out.println(a.doubleValue()); System.out.println(a.floatValue()); } }
输出结果
false
true
true
51
51
51
51
51.0
51.0
方法
1、从Number继承的6个方法
2、字符串解析成int
Integer.parseInt(“255”); //按十进制解析成255
Integer.parseInt(“11111111”,2);//按2进制解析成255
Integer.parseInt(“377”,8);//按8进制解析成255
Integer.parseInt(“ff”,16);//按16进制解析成255
3、进制转换
Integer.toBinaryString(255);//二进制解析成”11111111”
Integer.toOctalString(255);//八进制解析成”377”
Integer.toHexString(255);//十六进制解析成”ff”
栗子
public class Test { public static void main(String[] args) { System.out.println(Integer.parseInt("255")); System.out.println(Integer.parseInt("11111111",2)); System.out.println(Integer.parseInt("377",8)); System.out.println(Integer.parseInt("ff",16)); System.out.println(Integer.toBinaryString(255)); System.out.println(Integer.toOctalString(255)); System.out.println(Integer.toHexString(255)); } }
输出结果
255
255
255
255
11111111
377
ff
Double
创建对象
Double b = new Double();
Double b = Double.valueOf();
方法
1、从Number类继承的6个方法
2、字符串解析成double类型
Double.parseDouble(“3.14”);
3、判断浮点数特殊值
Infinity:无穷大
NaN:not a number
Double.isInfinite(double d)
Double.isNaN(double d)
自动装箱
Integer a = 52;//给一个Integer类型的对象不能直接赋一个基本类型值,a保存的是一个内存地址。52自动封装成一个对象,把这个对象的地址赋值给a。这个对象是自动创建的,是自动装箱。
编译成:Integer a = Integer.valueOf(52);
自动拆箱
int i= a;//定义一个int类型变量,i应该直接存一个值,a保存的是一个内存地址。a自动取出对象中的值,然后把值赋值给i
编译成:int i = a.intValue();
有了自动装箱和自动拆箱,写代码变得很简单
a = a +2;
a是一个对象,自动拆箱成一个值,+2,再封装成一个对象,把内存地址赋值给a
编译成:
a = Integer.valueOf(a.intValue()+2)
自动拆箱要当心null值
BigDecimal
作用:
做精确的浮点运算
浮点数的运算是不精确的
2-1.9 = 0.100000009
4.35*100 = 434.999999…4
创建对象:
BigDecimal bd = BigDecimal.valueOf(1.9);
方法
add(BigDecimal bd)+
subtract(BigDecimal bd)-
multiply(BigDecimal bd)*
divide(BigDecimal bd)/
divide(BigDecimal bd,保留位数,舍入方式)
setScale(保留位数,舍入方式) 舍入运算,运算结果会封装成新的大数字对象返回
2-1.9
a.subtract(b)
结果会封装成一个新的大数字对象
BigInteger
作用:
做超大整数运算
栗子
public static void main(String[] args) { System.out.println("输入降落时间"); double t = new Scanner(System.in).nextDouble(); //1/2*9.8*t*t BigDecimal a = BigDecimal.valueOf(0.5); BigDecimal b = BigDecimal.valueOf(9.8); BigDecimal c = BigDecimal.valueOf(t); double d = a.multiply(b).multiply(c.pow(2)).doubleValue(); System.out.println("降落距离:"+d+"米"); }
输出结果
输入降落时间2降落距离:19.6米
栗子:阶乘
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="阶乘"/> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout>
java
public class MainActivity extends Activity implements View.OnClickListener{ private EditText editText; private Button button; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText); button = (Button) findViewById(R.id.button); textView = (TextView) findViewById(R.id.textView); button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: f(); break; default: break; } } private void f() { int a = Integer.parseInt(editText.getText().toString()); /*long r = a; for(int i=a-1;i>=1;i--){ r*=i; }*/ //超大整数运算 BigInteger r = BigInteger.valueOf(a); for(int i=a-1;i>=1;i--){ BigInteger bi = BigInteger.valueOf(i); r = r.multiply(bi); } textView.setText(r.toString()); }}
栗子:setScale
BigDecimal b = new BigDecimal(2.345); b = b.setScale(2,BigDecimal.ROUND_HALF_UP); System.out.println(b.doubleValue());
输出结果
2.35
取值方法
1、从Number继承的6个取值方法
2、toString
- 达内课程-基本类型包装类BigInteger、BigDecimal使用
- 达内课程-基本类型包装类Math、SimpleDateFormat、Date、DecimalFormat方法使用
- JAVA包装类、BigDecimal类、BigInteger类
- java BigInteger BigDecimal类
- BigInteger和BigDecimal类
- 基本类型包装类
- 达内课程-JAVA八种基本类型
- 【Java 大数类型】BigInteger和BigDecimal
- BigInteger类和BigDecimal类
- Java-BigDecimal类,BigInteger类
- 高精度运算类(BigInteger,BigDecimal)
- 类:BigDecimal,BigInteger,Math,System
- 基本类型与包装类
- 基本类型与包装类
- Java基本类型包装类
- 基本类型的包装类
- 【javaSE】基本类型包装类
- 【Java】基本类型包装类
- Runtime类
- Linus(二)重要的热键tab ,ctrl+c,ctrl+d以及开关机命令
- centos7 YUM安装lnmp
- mysql-函数CASE WHEN 统计多个字段
- 欢迎使用CSDN-markdown编辑器
- 达内课程-基本类型包装类BigInteger、BigDecimal使用
- 分割线收藏
- bzoj 1264: [AHOI2006]基因匹配Match
- js基础-客户端存储(Cookie、Storage、IndexedDB)
- java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符 ``` *### SQL: SELECT COUNT(ID) AS VALU
- 金融科技&大数据产品推荐:氪信XBehavior — 基于高维行为语言处理技术的信贷风险评估
- 有关于深度优先搜索的二三事儿(《算法设计与实现》摘要)
- 使用Docker镜像和仓库
- linux cifs自动挂载windows硬盘或文件夹