Anonymous inner class
Anonymous inner class refers to an inner class without a name, which must be defined on the basis of an abstract class or interface.
Example: Using anonymous inner classes to simplify class definition
interface Message{ //定义接口
public void print();
}
public class Test1_1_4_9 {
public static void main(String args[]){
fun(new Message(){ //直接实例化接口对象
public void print(){ //匿名内部类中覆写print()方法
System.out.println("Hello World");
}
}); //传递匿名内部类实例化
}
public static void fun(Message msg){ //接收接口对象
msg.print();
}
}
//结果
//Hello World
Basic data type wrapper
Java provides corresponding classes for basic data types. These classes are called wrappers.
Object packaging class (direct subclass of Object): Character, Boolean.
Numeric packaging class (Direct subclass of Number): Byte, Short, Integer, Long, Float, Double.
About the definition of Number class:
Number is an abstract class, which defines a total of 6 operation methods: intValue (), doubleValue (), floatValue (), byteValue (), shortValue (), longValue ()
Boxing and unboxing operations
Boxing operation: change the basic data type into the form of a packaging class;
each packaging class construction method can receive variables of its own data type;
unboxing operation: take out the packaged data from the packaging;
use inheritance from the Number class A series of xxxValue () methods are completed.
Example: using int and Integer
public class Test1_1_5_11 {
public static void main(String args[]){
Integer obj=new Integer(10); //将基本数据类型装箱(构造方法实例化)
int temp=obj.intValue(); //将基本数据类型拆箱
System.out.println(temp*2); //数学计算
}
}
//结果
//20
Example: using double and Double
public class Test1_1_5_11 {
public static void main(String args[]){
Double obj=new Double(10.2); //装箱
double temp=obj.doubleValue(); //拆箱
System.out.println(temp*2);
}
}
//结果
//20.4
Example: Automatic packing and unpacking
public class Test1_1_5_11 {
public static void main(String args[]){
Integer obj=10; //自动装箱(直接装箱实例化)
int temp=obj; //自动拆箱
obj++;
System.out.println(temp*obj);
}
}
//结果
//110
If you use the direct boxing instantiation method, the same heap memory space will be used, and the packaging object instantiated by the constructor will open up a new heap memory space. Therefore, the most reliable method for equal comparison of packaging data is equals ().
Use the Object class to receive all data types
Process:
1. Basic data type
2. Automatic packing (become an object)
3. Upward transformation to Object
Example: Use Object to receive data types
public class Test1_1_5_11 {
public static void main(String args[]){
Object obj=10; //先自动装箱后再向上转型,此时不能进行科学计算
//Object不可能直接向下转型
//所以要取出基本数据类型必须首先向下转型为指定的包装类
int temp=(Integer)obj; //向下变为Integer后自动拆箱
System.out.println(temp*2);
}
}
//结果
//20
Data type conversion
Commonly used methods to change the String data type to basic data types
Integer class: public static int parseInt (String s);
Double class: public static double parseDouble (String s);
Boolean class: public static boolean parseBoolean (String s);
Example: Change the string into int data
public class Test1_1_5_1 {
public static void main(String args[]){
String str="123"; //字符串
int temp=Integer.parseInt(str); //将字符串转换为int型数据
System.out.println(temp*2);
}
}
//结果
//246
Note: If you want to convert a string data into a number, you must ensure that the characters defined in the string are all numbers, if there are non-numeric characters, then the conversion will be abnormal.
Example: Convert basic data type to String type data
public class Test1_1_5_1 {
public static void main(String args[]){
int num=100;
String str=String.valueOf(num); //变为String型
System.out.println(str.replaceAll("0","9"));
}
}
//结果
//199