Java学习笔记——Number类

Java学习笔记——Number类,第1张

目录

​ 一、xxxValue()

二、compareTo()

三、equals()

四、valueOf()

五、toString()

六、parsexxx()

七、代码 


Java中的抽象类Number类的子类包括:Byte、Short、Integer、Long、Float、Double。

Number类属于java.lang包。

下面简单介绍Number类中的方法。

 一、xxxValue()
  • byteValue():以 byte 形式返回指定的数值。
  • shortValue():以 short 形式返回指定的数值。
  • intValue():以 int 形式返回指定的数值。
  • longValue():以 long 形式返回指定的数值。
  • floatValue():以 float 形式返回指定的数值。
  • doubleValue():以 double 形式返回指定的数值。

代码实例1

Object ob1 = null;
Object ob2 = null;
Object ob3 = null;
Object ob4 = null;
Object ob5 = null;
Object ob6 = null;

Integer x = 10;

// 返回 byte 数据类型
ob1 = x.byteValue();
System.out.println("byteValue(): " + ob1);

// 返回 short 数据类型
ob2 = x.shortValue();
System.out.println("shortValue(): " + ob2);

// 返回 int 数据类型
ob3 = x.intValue();
System.out.println("intValue(): " + ob3);

// 返回 long 数据类型
ob4 = x.longValue();
System.out.println("longValue(): " + ob4);

// 返回 float 数据类型
ob5 = x.floatValue();
System.out.println("floatValue(): " + ob5);

// 返回 double 数据类型
ob6 = x.doubleValue();
System.out.println("doubleValue(): " + ob6);

输出结果1 

 Debug

 Debug后,可看到数据类型。

二、compareTo()
  • compareTo() 方法用于将 Number 对象与方法的参数进行比较;
  • 可用于比较 Byte、Short、Integer、Long、Float、Double;
  • 该方法用于两个相同数据类型的比较,两个不同类型的数据不能用此方法来比较;
  • 用法:x.compareTo(y);若 x == y: 返回0;若 x > y:返回1;若 x < y:返回-1;

代码实例2

/* compareTo() */
Integer a = 5;
System.out.println(a.compareTo(3));
System.out.println(a.compareTo(5));
System.out.println(a.compareTo(8));

输出结果2 

三、equals()
  • equals() 用于判断 Number 对象与方法的参数进是否相等;
  • 用法:x.equals(y);若 x 与 y的数值相等 且 数据类型相同,返回 true,否则 false;

代码实例3

/* equals() */
Integer c = 5;
Integer d = 10;
Integer e = 5;
Byte f = 5;

System.out.println("c.equals(d): " + c.equals(d));
System.out.println("c.equals(e): " + c.equals(e));
System.out.println("c.equals(f): " + c.equals(f));

输出结果3

其中,c 和 d 都是 Integer类型,数值分别为5和10,数值不相等,返回 false;c 和 e 都是Integer类型,且数值均为5,返回 true;c 和 f 数据类型不同,分别为 Integer 和 Short,数值均为5,返回false。

四、valueOf()
  • valueOf() 用于返回给定参数的原生 Number 对象值;
  • 用法: 
  1. Integer valueOf(int i):返回一个表示指定的 int 值的 Integer 实例。
  2. Integer valueOf(String s):返回保存指定的 String 的值的 Integer 对象。
  3. Integer valueOf(String s, int radix): 返回一个 Integer 对象,该对象中保存了用第二个参数提供的基数进行解析时从指定的 String 中提取的值。其中,radix为进制数。

代码实例4 

/* valueOf() */
Byte a1 = Byte.valueOf((byte) 1);
Short a2 = Short.valueOf((short)1);
Integer a3 = Integer.valueOf(1);
Long a4 = Long.valueOf(1);
Float a5 = Float.valueOf(1);
Double a6 = Double.valueOf(1);

System.out.println("a1: " + a1);
System.out.println("a2: " + a2);
System.out.println("a3: " + a3);
System.out.println("a4: " + a4);
System.out.println("a5: " + a5);
System.out.println("a6: " + a6);

输出结果4

Debug

五、toString()

toString() 方法用于返回以一个字符串表示的 Number 对象值;

代码实例5

/* toString() */
Integer p = 10;
System.out.println(p.toString());
System.out.println(Integer.toString(20));

输出结果5

六、parsexxx()
  • parseByte():将字符串解析为 byte 类型;
  • parseShort():将字符串解析为 short 类型;
  • parseInteger():将字符串解析为 int 类型;
  • parseLong():将字符串解析为 long 类型;
  • parseFloat():将字符串解析为 float 类型;
  • parseDouble():将字符串解析为 double 类型;

代码实例6

/* parsexxx() */
byte q1 = Byte.parseByte("1");
short q2 = Short.parseShort("2");
int q3 =Integer.parseInt("3");
int q4 = Integer.parseInt("444",16);
long q5 = Long.parseLong("1000");
float q6 = Float.parseFloat("6");
double q7 = Double.parseDouble("7");

System.out.println("Byte.parseByte q1: " + q1);
System.out.println("Short.parseShort q2: " + q2);
System.out.println("Integer.parseInt q3: " + q3);
System.out.println("Integer.parseInt q4 16" + q4);
System.out.println("Long.parseLong q5: " + q5);
System.out.println("Float.parseFloat q6: " + q6);
System.out.println("Double.parseDouble: " + q7);

输出结果6

七、代码 
public class Day4 {
    public static void main(String args[]) {
        /* xxxValue() */
        Object ob1 = null;
        Object ob2 = null;
        Object ob3 = null;
        Object ob4 = null;
        Object ob5 = null;
        Object ob6 = null;

        Integer x = 10;

        // 返回 byte 数据类型
        ob1 = x.byteValue();
        System.out.println("byteValue(): " + ob1);

        // 返回 short 数据类型
        ob2 = x.shortValue();
        System.out.println("shortValue(): " + ob2);

        // 返回 int 数据类型
        ob3 = x.intValue();
        System.out.println("intValue(): " + ob3);

        // 返回 long 数据类型
        ob4 = x.longValue();
        System.out.println("longValue(): " + ob4);

        // 返回 float 数据类型
        ob5 = x.floatValue();
        System.out.println("floatValue(): " + ob5);

        // 返回 double 数据类型
        ob6 = x.doubleValue();
        System.out.println("doubleValue(): " + ob6);

        /* compareTo() */
        Integer a = 5;
        System.out.println(a.compareTo(3));
        System.out.println(a.compareTo(5));
        System.out.println(a.compareTo(8));

        /* equals() */
        Integer c = 5;
        Integer d = 10;
        Integer e = 5;
        Byte f = 5;

        System.out.println("c.equals(d): " + c.equals(d));
        System.out.println("c.equals(e): " + c.equals(e));
        System.out.println("c.equals(f): " + c.equals(f));

        /* valueOf() */
        Byte a1 = Byte.valueOf((byte) 1);
        Short a2 = Short.valueOf((short)1);
        Integer a3 = Integer.valueOf(1);
        Long a4 = Long.valueOf(1);
        Float a5 = Float.valueOf(1);
        Double a6 = Double.valueOf(1);

        System.out.println("a1: " + a1);
        System.out.println("a2: " + a2);
        System.out.println("a3: " + a3);
        System.out.println("a4: " + a4);
        System.out.println("a5: " + a5);
        System.out.println("a6: " + a6);

        /* toString() */
        Integer p = 10;
        System.out.println(p.toString());
        System.out.println(Integer.toString(20));

        /* parsexxx() */
        byte q1 = Byte.parseByte("1");
        short q2 = Short.parseShort("2");
        int q3 =Integer.parseInt("3");
        int q4 = Integer.parseInt("444",16);
        long q5 = Long.parseLong("1000");
        float q6 = Float.parseFloat("6");
        double q7 = Double.parseDouble("7");

        System.out.println("Byte.parseByte q1: " + q1);
        System.out.println("Short.parseShort q2: " + q2);
        System.out.println("Integer.parseInt q3: " + q3);
        System.out.println("Integer.parseInt q4 16" + q4);
        System.out.println("Long.parseLong q5: " + q5);
        System.out.println("Float.parseFloat q6: " + q6);
        System.out.println("Double.parseDouble: " + q7);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/787420.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存