包装类
1.基本数据类型对应的引用数据类型(包装类)
1. 概述: 就是基本类型对应的类( 包装类) , 我们需要将基本类型转成包装类, 从而让基本类型拥有类的特性( 说白了, 将基本类型转成包装类之后, 就可以使用包装类中的方法操作数据)
2. 为啥要学包装类:
a. 将来有一些特定场景, 特定操作, 比如调用方法传递包装类
比如: ArrayList 集合, 里面有一个方法add ( Integer i) , 此时我们不能调用add方法之后直接传递基本类型, 因为引用类型不能直接接收基本类型的值, 就需要先将基本类型转成包装类, 传递到add方法中
b. 将来我们还可以将包装类转成基本类型:
包装类不能直接使用+ - * / , 所以需要将包装类转成基本类型, 才能使用+ - * /
基本类型 包装类 byte Byte short Short int Integer long Long float Float double Double char Charactor boolean Boolean
2.Integer的介绍以及使用
2.1.Integer基本使用
1. 概述: Integer 是int 的包装类
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
位置 方法 说明 Byte static byte parseByte(String s) 将String转byte类型 Short static short parseShort(String s) 将String转成short类型 Integer static int parseInt(String s) 将String转成int类型 Long static long parseLong(String s) 将String转成long类型 Float static float parseFloat(String s) 将String转成float类型 Double static double parseDouble(String s) 将String转成double类型 Boolean static 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 Integer uid;
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中的属性变成包装类, 还可以使用包装类中的方法去操作此属性值