java基础学习API之Integer类 六-3

来源:互联网 发布:淘宝售假怎么申诉 编辑:程序博客网 时间:2024/06/15 03:40

Integer 类在对象中包装了一个基本类型 int 的值。Integer 类型的对象包含一个int 类型的字段。 


package wsj03;/** * Integer类的使用 * @author Angus *判断某个数据是否在int范围内 *static int MAX_VALUE :值为 2^31-1 的常量,它表示 int 类型能够表示的最大值。 static int MIN_VALUE :值为 -2^31 的常量,它表示 int 类型能够表示的最小值。 if(num > Integer.MIN_VALUE && num<Integer.MAX_VALUE){是int范围内} */public class IntegerDemo1 {public static void main(String[] args) {//部分方法使用//public static String toBinaryString(int i)  二进制//public static String toHexString(int i)十六进制//public static String toOctalString(int i) 八进制System.out.println(Integer.toBinaryString(60)); //111100System.out.println(Integer.toHexString(60)); //3cSystem.out.println(Integer.toOctalString(60)); //74}}

构造方法:


package wsj03;/** * Integer类的使用 * @author Angus *       基本数据类型        引用数据类型   byte             Byte   short            Short   int              Integer   long             long   float            Float   char             Character   boolean          Boolean   double           Double以Integer为例构造方法;Integer(int value) Integer(String s) //字符串必须是数字字符的字符串  英文字母abc等会报错 */public class IntegerDemo1 {public static void main(String[] args) {//把int类型变成Integer类型int num = 100;Integer i = new Integer(num);System.out.println(i.toString());//把String类型变成Integer类型//String s = "100as"; //java.lang.NumberFormatException: For input string: "100as"String s = "100"; //java.lang.NumberFormatException: For input string: "100as"Integer ii = new Integer(s);System.out.println(ii.toString());}}

常用方法:



package wsj03;/** *  * @author Angus *int-- String  * *String -- int */public class IntegerDemo2 {public static void main(String[] args) {//int-- String int num = 100;//方式一String s1 = num+"";//方式二String s2 = String.valueOf(num);//方式三//int --Integer--String Integer i = new Integer(num);String s3 = i.toString();//方式四   //public static String toString(int i)String s4 = Integer.toString(num);//String -- intString s = "100";//方式一//String --- Integer -- int Integer ii =new Integer(s);//public int intValue()int number = ii.intValue();//方式二  记住//public static Integer valueOf(String i)int number2 = ii.valueOf(s);}}

Integer在JDK5以后的新特性

package wsj03;/** *  * @author Angus * JDK5 以后新特性 * 自动装箱:也就是把基本类型赋值给引用类型 * 自动拆箱;把引用类型直接拆成基本类型 *  * 开发原则: * 只要是对象就要判断不为null的判断 */public class IntegerDemo2 {public static void main(String[] args) {Integer ii = 100;ii = ii+100;System.out.println(ii); //200/* * 数据参与运算要求类型一致 * 而现在ii 是Integer类型100是int类型 * 底层调用的是intvalue方法 * 也就是ii+100 * 其实是ii.intvalue+100 * 计算后是200 * 最后又做了一次装箱的操作 */Integer i = null;i= i+100;System.out.println(i);  // java.lang.NullPointerException//报错,空指针异常 //验证 i.intvalue 方法空指针}}

Integer面试:

package wsj03;/** *  * @author Angus * 关于Integer面试题 *   byte常量池 *   也就是byte范围内的值,直接赋值给Integer,是从常量池中获取的。 byte的值用的概率高常量池中长存 */public class IntegerDemo2 {public static void main(String[] args) {Integer i1 = new Integer(127);Integer i2 = new Integer(127);System.out.println(i1 == i2); //falseSystem.out.println(i1.equals(i2)); //trueSystem.out.println("-----------------------------");Integer i3 = new Integer(128);Integer i4 = new Integer(128);System.out.println(i3 == i4);//falseSystem.out.println(i3.equals(i4));//trueSystem.out.println("-----------------------------");Integer i5 = 128;Integer i6 = 128;System.out.println(i5 == i6);//falseSystem.out.println(i5.equals(i6));//trueSystem.out.println("-----------------------------");Integer i7 = 127;Integer i8 = 127;System.out.println(i7 == i8);//trueSystem.out.println(i7.equals(i8));//true}}


最后附上JDK使用文档API 下载



1 0
原创粉丝点击