1面向对象(下)
(1)final修饰变量,方法,类,系统不容许为final变量重重新赋值。
(2)final不能派生子类
(3)使用final修饰的类,java容许实现必可[变类]
2.基本类型的包装类:
byte Byte
short Short
int 【Integer】
long Long
char 【Character】
float Float
double Double
boolean Boolean
自动装箱:把基本类型变量自动赋值给包装类变量,或者object变量(object是父类对象,子类变量直接赋值给父类变量
自动拆箱:相反
自动拆箱,装箱针对相同的类项之间转化。
parseXXX(String s)用法用于将包装类转化为基本类型。
public class AutoBoxUnBox {
public static void main(String[] args) {
// TODO Auto-generated method stub
String a="123";
int b=Integer.parseInt(a);
System.out.println(b);
}
}
如果a赋值“a”;
public class AutoBoxUnBox {
public static void main(String[] args) {
String a="a";
int b=Integer.parseInt(a);
System.out.println(b);
}
}
出现异常为:
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at AutoBoxUnBox.main(AutoBoxUnBox.java:10)
String 类中提供valueof()将基本类型转化为字符串类项
public class AutoBoxUnBox {
public static void main(String[] args) {
String a="123";
int b=Integer.parseInt(a);
System.out.println(b);
String str=String.valueOf(5.124f);
System.out.println(str);
}
}
3。==与equals
==是判断两个基本类型的变量是否相等。
equals是变量应用类型的是否相等。。
public class TestBox {
public static void main(String args[]){
String str1=new String("hello");
String str2=new String("hello");
System.out.println(str1.equals(str2));
System.out.println(str1==str2);
}
}