黑马程序员-包装类与数字处理类

来源:互联网 发布:淘宝网南极绒男童内衣 编辑:程序博客网 时间:2024/06/17 20:32
------- android培训、java培训、期待与您交流!---------

一、包装类 

java是一种面向对象语言,java中的类把方法与数据连接在一起,构成了自包含式的处理单元。但在java中不能定义基本数据类型对象,为了能将基本数据类型视为对象处理,

并能连接相关的方法,java为每个基本数据类型都提供了包装类。如下

1、基本数据类型对象包装类。

        byte               Byte

        short              Short

        int                  Integer

        long               Long

        boolean          Boolean

        float               Float

        double           Double

        char               Character

1.1、Integer

      java.lang包中的Integer类、Long类和Short类,分别将基本数据类型int、long、short封装成一个类,这些类的是Number的子类

Integer的两种构造方法:

方法一:

.Integer(int number)

该方法以一个int型变量作为参数来获取Integer对象

.Integer number = new Integer(8);

以一个int型变量作为参数来创建Integer对象

方法二:

Integer(String str)

该方法以一个String型变量作为参数来获取Integer对象

.Integer number = new Integer("25");

以一个int型变量作为参数来创建Integer对象

字符串转成基本数据类型。

         xxx a=Xxx.parseXxx(string);//必须传入对应类型的字符串。

           int a=Integer.parseInt("258");//转化成int类型

实例子:

class Demo{
public static void main(String[] args) {
String[] str={"2","5","8","3","6","9","1"};//定义一个数组
int sum=0;//定义int型变量来接收数组中所有数字的和
for (int i = 0; i < str.length; i++) {//遍历数组中的元素
int j = Integer.parseInt(str[i]);//将字符串转变成int类型
sum = sum+j;//将数组中各个元素相加
}System.out.println(sum);
}
}

1.2Boolean

1.构造方法:

Boolean(boolean value)

该方法创建一个表示value参数的bolean对象

Boolean(String str)

该方法以一个String型变量作为参数来获取Boolean对象

常用用法;

booleanValue(); //将Boolean对象的值以对应的boolean值返回

class Test10 {
public static void main(String[] args) {
Boolean b1=new Boolean(true);
Boolean b2= new Boolean("ok");
System.out.println(b1.booleanValue());//结果是true
System.out.println(b2.booleanValue());//结果是false
}
}

1.3Byte

构造方法

Byte(byte value)

通过这种方法创建Byte对象,可表示指定的值

例子:

byte b=10;

Byte by=new Byte(b);

1.4Charcter

构造方法

Charcter(char value)

该类的构造函数必须是char类型的的数据

创建对象:Charcter ch = new Charcter('a');

1.5Double

构造方法

Double(double value)

基于double参数创建Double类对象

Double(String str)

构造一个新分配的Double对象,表示 用字符串表示读到double类型的浮点值

2、十进制转成其他进制。

         toBinaryString();

         toHexString(); 

         toOctalString();

 

3、其他进制转成十进制。

         parseInt(String,radix);

         如:int a= Intager.parseInt("3c",16);

JDK1.5版本以后出现的新特性。

                         Integer x=new Integer(4);

         等效于:Integer x=4;//自动装箱。

        还可以直接进行运算:x=x+2;//x进行自动拆箱。变成了int类型。和2进行加法运算。再将和进行装箱赋给xx的拆箱动作等效:x.intValue()

4、示例

        Integer x=128;

        Integer y=128;

        x==y   false

        Integer m=127;

        Integer n=127;

        m==n  true

原因:

        因为mn指向了同一个Integer对象。因为当数值在byte范围内,对于新特性,如果该数组已经存在,则不会再开辟新的空间。

二、数字处理类

在解决实际问题时,对数字的处理是非常普遍的,如数学问题,随机问题、科学问题等为了应对以上的问题,java提供了处理相关问题的类,包括DecimaFormat类(用于格式化数字)、Math(为了解决数学问题提供了工具)等等。

2.1数字格式化

数字的格式化在解决问题非常普遍,比如超市的商品价格,需要保留两位有效数字

下面以实力说明数字格式化的使用

//使用实例化对象时设置格式化模式
public static void simgleFormat(String pattern,double value){
//实例化DecimalFormat对象
DecimalFormat d = new DecimalFormat(pattern);
String s =d.format(value);//将数字格式化
System.out.println(value+","+pattern+","+s);
}
//使用applyPattern()方法对数字进行格式化
public static void applyPatternMethod(String pattern,double value){
DecimalFormat df = new DecimalFormat();//实例化DecimalFormat对象
df.applyPattern(pattern);//调用applyPattern()方法设置格式化模板
System.out.println(value+","+pattern+","+df.format(value));
}

public static void main(String[] args) 
{
simgleFormat("###,###.###",122583.258);//调用simgleFormat()方法
simgleFormat("0000000.###kg",122583.258);//在数字后面加单位
applyPatternMethod("#.###%",0.258);//将数字转换成百分数形式
//将小数点后格式化为两位
applyPatternMethod("###.##",122583.258);

}
}

打印结果:

122583.258,###,###.###,122,583.258
122583.258,0000000.###kg,0122583.258kg
0.258,#.###%,25.8%
122583.258,###.##,122583.26

2.2Math类

为三角函数、对数函数和其他通用数学函数提供常数和静态方法。

public class Demo {

public static void main(String args[]) {
/**
* abs求绝对值
*/
System.out.println(Math.abs(-10.4)); // 10.4
System.out.println(Math.abs(10.1)); // 10.1

/**
* floor地板的意思,就是返回小的值
*/
System.out.println(Math.floor(-10.1)); // -11.0
System.out.println(Math.floor(10.7)); // 10.0
System.out.println(Math.floor(-0.7)); // -1.0
System.out.println(Math.floor(0.0)); // 0.0
System.out.println(Math.floor(-0.0)); // -0.0

/**
* max 两个中返回大的值,min和它相反,就不举例了
*/
System.out.println(Math.max(-10.1, -10)); // -10.0
System.out.println(Math.max(10.7, 10)); // 10.7
System.out.println(Math.max(0.0, -0.0)); // 0.0

/**
* random 取得一个大于或者等于0.0小于不等于1.0的随机数
*/
System.out.println(Math.random()); // 0.08417657924317234
System.out.println(Math.random()); // 0.43527904004403717


/**
* rint 四舍五入,返回double值 注意.5的时候会取偶数
*/
System.out.println(Math.rint(10.1)); // 10.0
System.out.println(Math.rint(10.7)); // 11.0

/**
* round 四舍五入,float时返回int值,double时返回long值
*/
System.out.println(Math.round(10.1)); // 10
System.out.println(Math.round(10.7)); // 11
System.out.println(Math.round(-10.6)); // -11
System.out.println(Math.round(-10.2)); // -10
/**
* 三角函数
*/
//取90度的正弦
System.out.println(Math.sin(90));
System.out.println(Math.cos(0));//余弦的0度
System.out.println(Math.toRadians(120));//取120度的弧度值

}
}




------- android培训、java培训、期待与您交流!---------
0 0
原创粉丝点击