Object class
Object is the top-level superclass of all classes. There are two methods that are often overridden by subclasses:
toString() and equals()
package object; /** * Object Is the top superclass of all classes There are several methods that are often overridden by subclasses, including toString and equals */ public class Demo { public static void main(String[] args) { Point p = new Point(1,2); /* Object The toString method has been implemented When directly inherited, the returned string content is the address information of the current object Format: class name @ address toString In the actual development of method, it is rarely written directly to call it, which is automatically executed inadvertently For example, when using system out. When println is output When concatenating with a string */ //System.out.println(Object obj) outputs the string returned by the given object toString to the console System.out.println(p); //String concatenation will call toString to convert non string objects into strings and then splice them String line = "This is a Point:" + p; System.out.println(line); Point p2 = new Point(1,2); System.out.println("p2:"+p2); /* For reference types, the value held by the variable is the address of the object ==Comparison is to compare whether the values of two variables are equal, so for reference types, it is to compare whether the addresses are equal, which means to compare whether they are the same object equals Is another method defined by Object. The purpose is to compare whether the contents of two objects are the same However, if the subclass does not override this method, it has no practical significance, because when the Object is implemented, it is still compared with = =! */ System.out.println(p == p2);//false System.out.println(p.equals(p2));//true } }
Packaging
java defines eight wrapper classes to solve the problem that basic types cannot directly participate in object-oriented development, so that basic types can exist in the form of objects through instances of wrapper classes
-
The wrapper classes of digital types inherit from Java Lang. number, while the wrapper classes of char and boolean are directly inherited from Object
-
Number is an abstract class, which defines some methods to enable the wrapper class to convert the basic type it represents to other number types
package integer; public class IntegerDemo1 { public static void main(String[] args) { //Convert base type to wrapper class int i = 123; //java recommends that we use the static method valueOf of the wrapper class to convert the basic type to the wrapper class instead of new directly Integer i1 = Integer.valueOf(i);//Integer reuses integer objects between - 128-127 Integer i2 = Integer.valueOf(i); System.out.println(i1==i2);//true System.out.println(i1.equals(i2));//true double dou = 123.123; Double dou1 = Double.valueOf(dou);//Double is direct new Double dou2 = Double.valueOf(dou); System.out.println(dou1==dou2);//false System.out.println(dou1.equals(dou2));//true //Convert wrapper class to base type int in = i1.intValue();//Gets the value of the base type represented in the wrapper class object double doub = i1.doubleValue(); System.out.println(in);//123 System.out.println(doub);//123.0 in = dou1.intValue();//There may be loss of precision from large type to small type! doub = dou1.doubleValue(); System.out.println(in);//123 System.out.println(doub);//123.123 } }
Common functions of packaging
package integer; public class IntegerDemo2 { public static void main(String[] args) { //1. The value range of the basic type it represents can be obtained through the packaging class //Get the maximum and minimum values of int? int imax = Integer.MAX_VALUE; System.out.println(imax); int imin = Integer.MIN_VALUE; System.out.println(imin); long lmax = Long.MAX_VALUE; System.out.println(lmax); long lmin = Long.MIN_VALUE; System.out.println(lmin); /* 2 The premise of converting a string to a basic type is that the string correctly describes the value that the basic type can save. Otherwise An exception will be thrown: NumberFormatException */ String str = "123"; // String str = "123.123";// This string cannot be parsed into int value! int d = Integer.parseInt(str); System.out.println(d);//123 double dou = Double.parseDouble(str); System.out.println(dou);//123.123 } }
Features of automatic disassembly and assembly box
JDK5 introduced a new feature: automatic disassembly and assembly box
This feature is recognized by the compiler When the compiler compiles the source code and finds that there are mutual assignments between basic types and reference types, it will automatically supplement the code to complete their conversion. This process is called automatic disassembly and assembly
package integer; public class IntegerDemo3 { public static void main(String[] args) { /* When the automatic unpacking feature is triggered, the compiler will supplement the code to convert the wrapper class to the basic type, and the following code will become: int i = new Integer(123).intValue(); */ int i = new Integer(123); /* Trigger the compiler auto boxing feature, and the code will be changed by the compiler to: Integer in = Integer.valueOf(123); */ Integer in = 123; } }
File class
Each instance of the File class can represent a File or directory (actually an abstract path) in the hard disk (File system)
Using File, you can:
-
1: Access the attribute information of the file or directory it represents, such as name, size, modification time, etc
-
2: Create and delete files or directories
-
3: Accessing subitems in a directory
However, File cannot access File data
public class FileDemo { public static void main(String[] args) { //Use File to access the demo in the current project directory Txt File /* When you create a File, you specify a path, which usually uses a relative path. The advantage of relative path is good cross platform. "./"Is the most used relative path, indicating "current directory", and where is the current directory Depending on the running environment of the program, when running a java program in idea, it is specified here The current directory is the project directory where the current program is located. */ // File file = new File("c:/xxx/xxx/xx/xxx.txt"); File file = new File("./demo.txt"); //Get name String name = file.getName(); System.out.println(name); //Gets the file size in bytes long len = file.length(); System.out.println(len+"byte"); //Is it readable or writable boolean cr = file.canRead(); boolean cw = file.canWrite(); System.out.println("Is it readable:"+cr); System.out.println("Is it writable:"+cw); //Hide boolean ih = file.isHidden(); System.out.println("Hide:"+ih); } }
Create a new file
The createNewFile() method can create a new file
package file; import java.io.File; import java.io.IOException; /** * Create a new File using File */ public class CreateNewFileDemo { public static void main(String[] args) throws IOException { //Create a new file in the current directory: test txt File file = new File("./test.txt"); //boolean exists() determines whether the File or directory actually exists at the location indicated by the current File if(file.exists()){ System.out.println("The file already exists!"); }else{ file.createNewFile();//Create the File represented by File System.out.println("File created!"); } } }
Create directory
mkDir(): creates the directory represented by the current File
mkDirs(): create the directory represented by the current File, and create all non-existent parent directories together
package file; import java.io.File; /** * Create directory using File */ public class MkDirDemo { public static void main(String[] args) { //Create a new directory under the current directory: demo // File dir = new File("demo"); File dir = new File("./a/b/c/d/e/f"); if(dir.exists()){ System.out.println("The directory already exists!"); }else{ // dir.mkdir();// When creating a directory, it is required that the directory must exist dir.mkdirs();//When creating a directory, all directories that do not exist on the path are created together System.out.println("Directory created!"); } } }
Delete directory
The delete() method can delete a directory, but only empty directories can be deleted.
package file; import java.io.File; /** * Delete a directory */ public class DeleteDirDemo { public static void main(String[] args) { //Delete the demo directory under the current directory File dir = new File("demo"); // File dir = new File("a"); if(dir.exists()){ dir.delete();//The delete method can only delete an empty directory System.out.println("Directory deleted!"); }else{ System.out.println("directory does not exist!"); } } }