包装类-Integer的介绍及使用 基本类型和String之间的转换(Java)

包装类

1.基本数据类型对应的引用数据类型(包装类)

1.概述:就是基本类型对应的类(包装类),我们需要将基本类型转成包装类,从而让基本类型拥有类的特性(说白了,将基本类型转成包装类之后,就可以使用包装类中的方法操作数据)
    
2.为啥要学包装类:
  a.将来有一些特定场景,特定操作,比如调用方法传递包装类
    比如:ArrayList集合,里面有一个方法add(Integer i),此时我们不能调用add方法之后直接传递基本类型,因为引用类型不能直接接收基本类型的值,就需要先将基本类型转成包装类,传递到add方法中
  b.将来我们还可以将包装类转成基本类型:
    包装类不能直接使用+ - * /,所以需要将包装类转成基本类型,才能使用+ - * /
基本类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharactor
booleanBoolean

2.Integer的介绍以及使用

2.1.Integer基本使用

1.概述:Integerint的包装类
2.构造:  不推荐使用了,但是还能用
  Integer(int value)
  Integer(String s) s必须是数字形式
public class Demo01Integer {
    public static void main(String[] args) {
        Integer i1 = new Integer(10);
        System.out.println("i1 = " + i1);

        Integer i2 = new Integer("10");
        System.out.println("i2 = " + i2);

        System.out.println("================");

        Boolean b1 = new Boolean("true");
        System.out.println("b1 = " + b1);

        Boolean b2 = new Boolean("false");
        System.out.println("b2 = " + b2);

        Boolean b3 = new Boolean("True");
        System.out.println("b3 = " + b3);
    }
}

1.装箱:将基本类型转成对应的包装类
2.方法:
  static Integer valueOf(int i)  
  static Integer valueOf(String s)  
public class Demo02Integer {
    public static void main(String[] args) {
        Integer i1 = Integer.valueOf(10);
        System.out.println("i1 = " + i1);

        Integer i2 = Integer.valueOf("100");
        System.out.println("i2 = " + i2);
    }
}
1.拆箱:将包装类转成基本类型
2.方法:
  int intValue();
public class Demo03Integer {
    public static void main(String[] args) {
        Integer i1 = Integer.valueOf(10);
        System.out.println("i1 = " + i1);

        int i = i1.intValue();
        System.out.println("(i+10) = " + (i + 10));
    }
}

2.2.自动拆箱装箱

1.拆箱和装箱很多时候都是自动完成的
public class Demo04Integer {
    public static void main(String[] args) {
        Integer i = 10;//发生了自动装箱了
        Integer sum = i+10;//发生了自动拆箱装箱
        System.out.println("sum = " + sum);
    }
}

public class Demo05Integer {
public static void main(String[] args) {
  Integer i1 = 100;
  Integer i2 = 100;
  System.out.println(i1==i2);

  Integer i3 = 128;
  Integer i4 = 128;
  System.out.println(i3==i4);
}
}

3.基本类型和String之间的转换

3.1 基本类型往String转

1.方式1:
  + 拼接
2.方式2:String中的静态方法
  static String valueOf(int i) 
  private static void method01() {
        int i = 10;
        String s1 = i+"";
        System.out.println(s1+1);

        System.out.println("============");

        String s = String.valueOf(10);
        System.out.println(s+1);
    }

3.2 String转成基本数据类型

每个包装类中都有一个类似的方法:  parseXXX
位置方法说明
Bytestatic byte parseByte(String s)将String转byte类型
Shortstatic short parseShort(String s)将String转成short类型
Integerstatic int parseInt(String s)将String转成int类型
Longstatic long parseLong(String s)将String转成long类型
Floatstatic float parseFloat(String s)将String转成float类型
Doublestatic double parseDouble(String s)将String转成double类型
Booleanstatic boolean parseBoolean(String s)将String转成boolean类型
    private static void method02() {
        int number = Integer.parseInt("1111");
        System.out.println(number+1);
    }
1.在实际开发过程中如何定义一个标准javabean
  定义javabean的时候一般会将基本类型的属性定义成包装类型的属性  
public class User {
    //private int uid;//用户id
    private Integer uid;//用户id
    private String username;//用户名
    private String password;//密码

    public User() {
    }

    public User(Integer uid, String username, String password) {
        this.uid = uid;
        this.username = username;
        this.password = password;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
1.举例:如果uid为Integer,默认值是null
2.将来javabean中的数据都是和数据库表联系起来的,我们可以将javabean中的数据添加到表中
  如果表中的uid为主键自增的,此时添加语句时uid中的数据不用我们单独维护赋值了,添加语句的sql语句就可以这样写:
  insert into user(uid,username,password) values (NULL,'金莲','36666');

3.到时候,我们需要将javabean中封装的数据获取出来放到sql语句中,如果uid为主键自增,而且javabean中的uid为包装类型,默认值为NULL,这样就不用单独维护uid的值了,也不用先给javabean中的uid赋值,然后在保存到数据库中了,咱们就可以直接使用uid的默认值,将默认值放到sql语句的uid列中
    
4.而且将javabean中的属性变成包装类,还可以使用包装类中的方法去操作此属性值    
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值