java开发者的福音, 功能齐全的Integer类

Integer类介绍
public final class Integer
extends Number
implements Comparable<Integer>
1.继承自Number类的方法
public byte byteValue() 
Returns the value of the specified number as a byte.
public double doubleValue() 
Returns the value of the specified number as a double.
public float floatValue() 
Returns the value of the specified number as a float.
public int intValue() 
Returns the value of the specified number as an int.
public long longValue() 
Returns the value of the specified number as a long.
public short shortValue() 
Returns the value of the specified number as a short.
Integer类实现Number类的五个抽象方法, 可以轻松实现基本数字类型的转换。
public class number {
	public static void main(String args[]) {
		Integer i = new Integer(21);
		Integer ii = new Integer(129); //大于 byte类型最大存储范围
		byte i1 = i.byteValue();
		double i2 = i.doubleValue();
		float i3 = i.floatValue();
		long i4 = i.longValue();
		short i5 = i.shortValue();
		byte i6 = ii.byteValue();
		System.out.println(i1);  //输出21
		System.out.println(i2);  //输出21.0
		System.out.println(i3);  //输出21.0
		System.out.println(i4);  //输出21
		System.out.println(i5);  //输出21
		System.out.println(i6);	 //输出-127, 无法正确表示数值
		
	}
}
当Integer表示的数字大于byte 或short 的最大表示范围时, 使用转换方法并输出, 编译器并不会抛出异常, 而是输出一个不正确的值。
2.构造方法
public Integer(int value) 
Constructs a newly allocated Integer 
public Integer(String s) 
Constructs a newly allocated Integer object that represents the int value indicated by the String parameter.
两种构造方法, 一个直接采用十进制int 传值, 一个采用String 传值, 不过String 不能有非数字内容, 而且只能是十进制。
public class number {
	public static void main(String args[]) {
		Integer i1 = new Integer(55);   //正常编译
		Integer i2 = new Integer("56"); //正常编译
		Integer i3 = new Integer("9a1");//抛出NumberFormatException
		System.out.println(i1);  //输出55
		System.out.println(i2);  //输出56
		System.out.println(i3);  //不可输出
	}
}
3.进制转换 or 转码decode
static Integer	decode(String nm)
Decodes a String into an Integer.
Decodes a String into an Integer. Accepts decimal, hexadecimal, and octal numbers given by the following grammar:
DecodableString:
Signopt DecimalNumeral
Signopt 0x HexDigits
Signopt 0X HexDigits
Signopt # HexDigits
Signopt 0 OctalDigits
Sign:
-
+
传入的字符串可以是十进制整数, 0x/0X/# + 十六进制整数或 0 + 八进制整数, 数字前面可以添加正负号。不按规则输入参数会抛出NumberFormatException。返回的是一个Integer实例。
public class number {
	public static void main(String args[]) {
		Integer i1 = Integer.decode("-0x22");		//传入十六进制数 22
		Integer i2 = Integer.decode("#83AE77"");	//传入十六进制数83AE97	
		Integer i3 = Integer.decode("073");		//传入八进制数 73
		Integer i4 = Integer.decode("Sumsang_boom");	//传入不规则字符串, 抛出NumberFormatException
		System.out.println(i1);  //输出十进制数 -34
		System.out.println(i2);  //输出十进制数 8629879
		System.out.println(i3);  //输出十进制数 59
		System.out.println(i4);  //无法输出
	}
}
4.进制转换的另一种方式
static String  toBinaryString(int i)
Returns a string representation of the integer argument as an unsigned integer in base 2.
static String  toHexString(int i)
Returns a string representation of the integer argument as an unsigned integer in base 16.
static String  toOctalString(int i)
Returns a string representation of the integer argument as an unsigned integer in base 8.
将传入的十进制整数值, 分别以二进制字符串, 八进制字符串和十六进制字符串返回.。返回的是字符串, 可对字符串做后续parseInt()转换处理。
public class number {
	public static void main(String args[]) {
		int i = 912;
		String s1 = Integer.toBinaryString(i);
		String s2 = Integer.toOctalString(i);
		String s3 = Integer.toHexString(i);
		System.out.println(s1);  //输出1110010000
		System.out.println(s2);  //输出1620
		System.out.println(s3);  //输出390
	}
}
5.二进制信息的处理
public static int  bitCount(int i)
Returns the number of one-bits in the two's complement binary representation of the specified int value.
public static int  highestOneBit(int i)
Returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value.
public static int  lowestOneBit(int i)
Returns an int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified int value.
方法将传入的整数转为二进制序列处理, 进而统序列中1的个数, 计算出现1的最高位和最低位。
public class number {
	public static void main(String args[]) {
		for (int  i= 10; i < 17;i++) {
			System.out.println(i + " :" + Integer.toBinaryString(i)
					+ "  个数:" + Integer.bitCount(i));
			System.out.println("最高位: " + Integer.highestOneBit(i)
					+ "  最低位: " + Integer.lowestOneBit(i));
		}
		
	}
}
运行结果:
10 :1010  个数:2
最高位: 8  最低位: 2
11 :1011  个数:3
最高位: 8  最低位: 1
12 :1100  个数:2
最高位: 8  最低位: 4
13 :1101  个数:3
最高位: 8  最低位: 1
14 :1110  个数:3
最高位: 8  最低位: 2
15 :1111  个数:4
最高位: 8  最低位: 1
16 :10000  个数:1
最高位: 16  最低位: 16
6.含有数字信息的字符串转换为整形
public static int parseInt(String s)
                    throws NumberFormatException
Parses the string argument as a signed decimal integer. 
public static Integer valueOf(String s, int radix)
                       throws NumberFormatException
第二个方法再次包含了进制转换的功能,第二个参数指定任意进制信息,重磅头条的任意进制转换。结果返回一个十进制计数的整形。
public class number {
	public static void main(String args[]) {
		int a = Integer.parseInt("55");
		int b = Integer.parseInt("EF31", 16);
		int c = Integer.parseInt("coana", 30);
		System.out.println(a);  //输出55
		System.out.println(b);  //输出61233
		System.out.println(c);  //输出10377700
	}
	
}
由于结果返回一个整数, 更多的进制转换用到这个方法。














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值