包装类
java是面向对象的语言,即一切皆对象,即使是基础数据类型也有相对应的类,称之为包装类。
byte-Byte
short-Short
int-Integer
long-Long
char-Character
float-Fload
double-Double
boolean-Boolean
将基本数据类型转换为包装类称之装箱,将包装类转换为基本数据类型称之为拆箱 java int m=100; //装箱 Integer obj=m; //拆箱 int m_1=obj; System.out.println("ism"+m+"ism_1"+m_1);
Object
Object 类是一个特殊的类,是所有类的父亲,java允许把任何类型的对象赋值给Object类的变量,当我们定义一个类时如果没有指定父类,那么它的父类默认是Object类
clone 创建与该对象的类相同的新对象
equals 比较两个对象是否相等
finalize 当垃圾回收器确定不存在该对象的更多引用时,对象垃圾回收器调用该方法
getClass 返回一个对象运行时的实例类
hashCode 返回该对象的hash码
notify 激活在该对象监视器上的一个线程
notifyAll 激活该对象监视器上的全部线程
toString 返回该对象的字符串表示
wait 在其他线程调用该对象的激活线程方法钱,导致当前线程等待
```java Object.toString() //toString方法实际上返回的是类名和hashCode 但是很多类重写的toString方法 class Index {
} public class demo { public static void main(String[] args) { Index i=new Index(); System.out.println(i); System.out.println(i.toString()); } } //重写toString方法
class Index { private String name; private int age;
public Index(String name, int age) {
this.name=name;
this.age=age;
}
public String toString(){
return "姓名"+this.name+"年龄"+age;
}
} public class demo { public static void main(String[] args) { Index s=new Index("Test",18); System.out.println("类的信息是"+s.toString()); } }
// equals方法 System.out.println("欢迎来到用户数据馆路系统"); Scanner input=new Scanner(System.in); System.out.println("请输入用户名"); String userName=input.next(); System.out.println("请输入密码"); String passWord=input.next(); if(userName.equals("admin") && passWord.equals("123456")){ System.out.println("登录成功"); }else{ System.out.println("登陆失败"); } ```
Interger
Integer类是int类的包装类,提供了int类型和String类型互相转换的方法还有其他的一些常量和方法
常见方法
byteValue 以byte类型返回Integer的值
shortValue 以short类型返回该Interger的值
intValue 以int类型返回该Integer的值
toString 返回该Integer值的String
equals 比较两个对象
compareTo 在数字上比较两个对象
valueOf 返回保存指定的String值的Integer对象
parseInt 将数字字符串转换为int
toHexString 将数字转换未八进制
tobinaryString 将数字转换未二进制
toOctalString 将数字转换为十六进制
常量
MAX_VALUE 可取的最大值
MIN_VALUE 可取的最小值
SIZE 二进制补码形式表示比特位
TYPE 基本类型的实例
```java String strnum="256"; int num=Integer.parseInt(strnum); //字符串转整型 int number=255; String numberstr=Integer.toString(number); //整型转字符串 System.out.println(num); System.out.println(numberstr); System.out.println(Integer.toHexString(number)); //八进制 System.out.println(Integer.toBinaryString(number)); //二进制 System.out.println(Integer.toOctalString(number)); //十六进制 System.out.println(Integer.MAXVALUE); System.out.println(Integer.MINVALUE); System.out.println(Integer.TYPE); System.out.println(Integer.SIZE);
```
Float
Float类是基本数据类型float的包装类
常用方法
byteValue
doubleValue
floatValue
intValue
longValue
shortValue
isNaN
isNaN
toString
valueOf
parseFloat
常量
MAX_VALUE
MIN_VALUE
MAX_EXPONENT 可能具有的最大指数
MIN_EXPONENT 可能具有的最小指数
MIN_NORMAL 最小标准值的常量
NaN 非数字值的常量
TYPE 实例
SIZE 二进制补码比特位数
java String num="127.1"; float num_float=Float.parseFloat(num); float num_1= 19.1f; String num_1str=Float.toString(num_1); System.out.println(num_float); System.out.println(num_1str);
Double
Doubel类是doluble基本类型的包装类
常用方法
byteValue
doubleValue
floatValue
intValue
longValue
shortValue
isNaN
isNaN
toString
valueOf
parseDouble
常量
MAX_VALUE
MIN_VALUE
NAN
NEGATIVE_INFINITY
POSITIVE_INFINITY
SIZE
TYPE
java double num=127.61; String num_str="127.168"; Double double_num=new Double(127.56); System.out.println(Double.toString(num)); System.out.println(double_num.byteValue()); System.out.println(double_num.isNaN()); System.out.println(Double.isNaN(1));
Number
Number是一个抽象类,他是 Integer,Float,Double,Byte,Short等类的父类,
常用方法
byteValue
doubleValue
floatValue
intValue
longValue
shortValue
java // 抽象类不能直接实例化但是我们可以实例化他的子类 Number num=new Double(127.56); System.out.println(num.toString()); System.out.println(num.intValue()); System.out.println(num.floatValue()); System.out.println(num.doubleValue()); System.out.println(num.shortValue());
Character
Character是基本数据类型char的包装类
常用方法
Character 狗仔一个新分配的Character,标志指定的char值
charValue 返回此charactr值
equals
isDigit 判断指定字符是否为数字
isLetter 判断指定字符是否为字母
isLetterOrDigit 判断指定字符是否为字母或者数字
isLowerCase 判断指定字符是否为小写字母
isUpperCase 判断指定字符是否为大写字母
toLowerCase 将字符参数转换为小写
toUpperCase 将字符参数转换为大写
实战例子太长所以不放了
Boolean
Boolean是boolean的包装类
常用方法
booleanValue 返回该boolean的值
equals 两个boolean比较
parseBoolean 将字符串参数解析为boolean值
toString 将boolean转换为字符串
vakueOf 返回一个用指定的字符表示的boolean值
常量
TRUE
FALSE
TYPE
java Boolean b=new Boolean(true); Boolean a=new Boolean("ok"); System.out.println(b); System.out.println(a); System.out.println(b.booleanValue()); System.out.println(b.parseBoolean("true")); System.out.println(b.valueOf("a"));
Byte类
Byte类是byte基本数据类型的包装类
常用方法
byteValue 以一个byte值返回byte对象
compareTo 比较两个Byte对象
doubleValue
intValue
parseByte(String)
toString
valueOf
equals
常量
MIN_VALUE
MAX_VALUE
SIZE
TYPE
System
System类的构造方法是private的无法创建该类的对象也无法实例化,System代表java程序的运行平台
System有三个静态变量,分别是PrintStream out(输出流) InputStream in(输入流)和PrintStream err(错误输出流)
```java System.out.println("请输入字符按回车键盘结束"); int a; try { a=System.in.read(); if(a != '\r'){ System.out.println((char) a); a=System.in.read();
}
}catch (IOException e){
System.out.println(e.toString());
}finally {
System.err.println();
}
```
常用的成员方法
exit() 退出程序
gc() 请求系统进行垃圾回收
getProperty("key") 获取系统属性
key值
java.version
java.home
os.name
os.version
user.name
user.home
user.dir
java System.out.println(System.getProperty("java.version")); System.out.println(System.getProperty("java.home")); System.out.println(System.getProperty("os.name")); System.out.println(System.getProperty("os.version")); System.out.println(System.getProperty("user.name")); System.out.println(System.getProperty("user.home")); System.out.println(System.getProperty("user.dir"));