Java学习日记——封装类、数字处理类、Math类 20190302

这篇博客详细介绍了Java中的封装类,包括Integer、Long和Short的构造方法、常用方法以及常量。此外,还探讨了Boolean类的特性。在数字处理部分,讲解了数字格式化、Math类的数学运算,如三角函数、指数和取整函数,以及如何生成随机数。最后,提到了大数字运算,如BigInteger和BigDecimal的使用。
摘要由CSDN通过智能技术生成

LEARNING

一、封装类

1、Integer—Java.lang包中的Integer类(int)、Long类(long)和Short类(short)封装
①构造方法

 Integer number = new Integer(7);        //int变量做参数
 Integer number = new Integer("45");    //String变量做参数

②常用方法
byteValue()——以byte类型返回该Integer值
compareTo(Integer anotherInteger)——在数字上比较两个Integer对象,相等返回0
equals(Object IntegerObj)——比较此对象与指定对象是否相等
intValue()——以int类型返回该Integer值
shortValue()——同上,以short
toString()——返回一个表示该Integer值的String对象
valueOf(String str)——返回该String值的int对象
parseInt(String str)——返回包含在由str指定的字符串中的数字的等价整数值
比如:

String str[] = {"20","21"};
 for(i = 0;i<str.length;i++)
         int myint = Integer.parsenlnt(str[i]);

③常量
MAX_VALUE——int可取最大值,2^31-1
MIN_VALUE——int可取最小值,-2^31
SIZE——以二进制补码形式表示int值的位数,32
TYPE——int的class实例

2、Boolean——包装boolean值在一个对象中
①构造方法:Boolean b = new Boolean(true);或者"b"

②常用方法
booleanValue()——返回以boolean值
equals(Object obj)——判断对象与obj是否相等,返回ture
parseBoolean(String s)——将字符串解析为boolean
toString()——返回该boolean值的字符串对象
valueOf(String s)——用一个指定字符串表示boolean值

③常量
TRUE
FALSE
TYPE

二、数字处理类

1、数字格式化 Java中数字小于0.001用科学计数法,大于1000000也是
java.text.DecimalFormat

import java.text.DecimalFormat;
public class Decimal{
//***~~方法1:使用DecimalFormat对象格式化数字~~ ***
static public void SimgleFormat(String pattern,double value){
   DecimalFormat myFormat = new DecimalFormat(pattern);
   String output = myFormat.format(value);
   System.out.println(value+" "+pattern+" "+output);   
 }


//~~***2:applyPattern()方法对数字进行格式化***~~ 
    static public void UseApplyPatternMethodFormat(String pattern,double value){
       DecimalFormat myFormat = new DecimalFormat();
       myFormat.applyPattern(pattern);
       System.out.println(value+" "+pattern+" "+myFormat.format(value)); 
    }
    //~~***方法3:setGroupingSize()     和setGroupingUsed(false)***~~ 
static public void SetGrouping(int pattern,double value){
   DecimalFormat myFormat = new DecimalFormat();
   myFormat.setGroupingSize(pattern);
   String output = myFormat.format(value);
   System.out.println("将数字分"+pattern+"组为:"+output);
   myFormat.setGroupingUsed(false);
   String output2 = myFormat.format(value);
   System.out.println("不分组:"+output2);
}
    public static void main(String[] args){
          SimgleFormat("###,###.###",123456.789);
          SimgleFormat("00000000.###kg",123456.789);
          SimgleFormat("000000.000",123.78);
          UseApplyPatternMethodFormat("#.###%",0.789);
          UseApplyPatternMethodFormat("###.##",123456.789);
          UseApplyPatternMethodFormat("0.00\u2030",0.789);
          SetGrouping(3,123456.789);
     }
    }

在这里插入图片描述
2、数学运算 ——Math类 Math.数学方法
三角函数

  public class Mathmatic{  
    public static void main(String[] args){
         //90正弦,0余弦
      System.out.println("90:"+Math.sin(Math.PI/2));
      System.out.println("0:"+Math.cos(0));
    //60正切
      System.out.println("60:"+Math.tan(Math.PI/3));
    //2²除以2的反正弦、反余弦
      System.out.println("2^2/2反正:"+Math.asin(Math.sqrt(2)/2));
      System.out.println("2^2/2反余:"+Math.acos(Math.sqrt(2)/2));
    //120的弧度值
      System.out.println("120:"+Math.toRadians(120.0));
    //π/2的角度
      System.out.println("π/2:"+Math.toDegrees(Math.PI/2));
     }
    }

使用GBK编码无法映射一些字符,使用UTF-8编码
还有指数函数、取整函数、取最值、绝对值函数等等

3、随机数

①Math类中的random()方法
Math.random()——默认产生大于等于0.0小于1.0的double型随机数
对该函数稍加处理:
*(int)(Matn.random()n)——返回大于等于0小于n的随机数
m+(int)(Math.Random()n)——返回大于等于m小于m+n的随机数
(char)(Math.random()(‘z’-‘a’+1))
——返回a~z任意字符

②java中的Random类——java.util.Random
Random r = new Random();
Random类的具体使用方法参考API文档。

4、大数字运算

java.math.BigIngeter
public BigIngeter(String val) val为十进制字符串
如:BigInteger 2Instance = new Bigeteger("2"); //将十进制2转换成BigInteger形式

java.math.BigDecimal
小数的大数字运算

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值