枚举
枚举指由一组固定的常量组成的类型
类型安全、易于输入、代码清晰
(列举出某一个属性所需要的正确值,限制赋值范围)
package GatherDemo06;
//枚举方法:Genders关键字
public enum Genders {
男,女,东方不败;
}
--------------------------------------------------------------
package GatherDemo06;
public class Student {
public String name;
public Genders gender;
public static void main(String[] args) {
Student student = new Student();
student.name = "张三";
//性别枚举
student.gender = Genders.男;
}
}
包装类
包装类把基本类型数据转换为对象
1)每个基本类型在java.lang包中都有一个
相应的包装类
包装类的作用
1)提供了一系列实用的方法
2)集合不允许存放基本数据类型数据,存
放数字时,要用包装类型
包装类的构造方法
1)所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例(对象)
2) 除Character类外,其他包装类可将一个字符串作为参数构造它们的实例Number类型包装类使用字符串作为参数构造实例的时候,字符串需要是能够转换为数字的字符串否则就会报NumberFormatException(数字格式化异常)
public Type(type value)
如:Integer i=new Integer(1);
public Type(String value)
如:Integer i=new Integer("123");
注意事项
1)Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false
2)当Number包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常
包装类的常用方法
语法:
XXXValue():包装类转换成基本类型
如:
Integer integerId=new Integer(25);
int intId=integerId.intValue();
toString()方法:以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)
String sex=Character.toString('男');
String id=Integer.toString(25);
String sex='男'+"";
String id=25+"";
parseXXX()方法:把字符串转换为相应的基本数据类型数据(Character除外)(字符串->基本类型)
int num=Integer.parseInt("36");
boolean bool=Boolean.parseBoolean("false");
public static type parseType(String type)
valueOf()方法
1)所有包装类都有如下方法(基本类型->包装类)
public static Type valueOf(type value)
如: Integer intValue = Integer.valueOf(21);
2)除Character类外,其他包装类都有如下方法(字符串->包装类)
public static Type valueOf(String s)
如: Integer intValue = Integer.valueOf("21");
包装类代码:
package TypeConversion.Demo01;
public class WrapClassDemo01 {
public static void main(String[] args) {
//包装类常用方法: XXXValue():包装类转换成基本类型
//包装类
/*byte by = 12;
Byte byte1 = new Byte(by);*/
//包装类转换成基本类型(byte)
Byte byte1=new Byte("12");
byte num1 = byte1.byteValue();
System.out.println(num1);
//包装类转换成基本类型(boolean)
Boolean bool = new Boolean("True");
boolean num2 = bool.booleanValue();
System.out.println(num2);
//包装类转换成基本类型(Char)
Character ch = new Character('a');
char num3 = ch.charValue();
System.out.println(num3);
/*toString():以字符串形式返回包装对象表示的基
本类型数据(基本类型- >字符串)*/
byte num4 = 10;//byte基本类型
String str1 = Byte.toString(num4);
System.out.println(str1);
String str2=Boolean.toString(false);//布尔型基本类型
System.out.println(str2);
//parseXXX():把字符串转换为相应的基本数据类型数据(CharactJer除外) (字符串->基 本类型)
int num5 = Integer.parseInt("30");
System.out.println(num5);
boolean num6 = Boolean.parseBoolean("qwe");//值为:false
System.out.println(num6);
//value0f():所有包装类都有如下方法(基本类型->包装类)public static Type value0f(type value)
Integer int1=Integer.valueOf(100);
System.out.println(int1);
//value0f( ) :除Character类外, 其他包装类都有如下方法(字符串->包装类)public static Type value0f(String s)
Long long1=Long.valueOf("200");
System.out.println(long1);
}
}
package TypeConversion.Demo01;
public class WrapClassDemo01 {
public static void main(String[] args) {
//包装类常用方法: XXXValue():包装类转换成基本类型
//包装类
/*byte by = 12;
Byte byte1 = new Byte(by);*/
//包装类转换成基本类型(byte)
Byte byte1=new Byte("12");
byte num1 = byte1.byteValue();
System.out.println(num1);
//包装类转换成基本类型(boolean)
Boolean bool = new Boolean("True");
boolean num2 = bool.booleanValue();
System.out.println(num2);
//包装类转换成基本类型(Char)
Character ch = new Character('a');
char num3 = ch.charValue();
System.out.println(num3);
/*toString():以字符串形式返回包装对象表示的基
本类型数据(基本类型- >字符串)*/
byte num4 = 10;//byte基本类型
String str1 = Byte.toString(num4);
System.out.println(str1);
String str2=Boolean.toString(false);//布尔型基本类型
System.out.println(str2);
//parseXXX():把字符串转换为相应的基本数据类型数据(CharactJer除外) (字符串->基 本类型)
int num5 = Integer.parseInt("30");
System.out.println(num5);
boolean num6 = Boolean.parseBoolean("qwe");//值为:false
System.out.println(num6);
//value0f():所有包装类都有如下方法(基本类型->包装类)public static Type value0f(type value)
Integer int1=Integer.valueOf(100);
System.out.println(int1);
//value0f( ) :除Character类外, 其他包装类都有如下方法(字符串->包装类)public static Type value0f(String s)
Long long1=Long.valueOf("200");
System.out.println(long1);
}
}
装箱和拆箱
基本类型和包装类的自动转换
Integer intObject = 5;
int intValue = intObject;
装箱:基本类型转换为包装类的对象
拆箱:包装类对象转换为基本类型的值
拆箱、装箱代码:
package TypeConversion.Demo01;
public class BoxingUnboxingDemo02 {
public static void main(String[] args) {
/*
*拆箱和装箱:实现的是基本数据类型和包装类类型之间的自动转换
*/
byte num1 = 10;
// Byte byte1 = new Byte(num1);
//装箱:基本数据类型直接赋值给基本类型的
Byte byte1 = null;
//拆箱:包装类类型数据直接赋值给基本类型的变量
Integer int1 = new Integer("123");
int num2 = int1;
//JDK1.5后,允许基本数据类型和包装类型进行混合数学运算
Integer int2 = new Integer("223");
int num3 = 300;
int sum= int2+num3;
Integer int3 = int2+num3;
}
}