JAVA language
jdk+notepad compilation
Compile javac Hello.java
Execute java Hello
type of data
byte b=123;//The maximum value of the 8-bit integer is 7 times 2 minus one, and the first bit is the sign bit
short s=32156;//The maximum is 15 times of 2-1
int i=101;//31
long l=123;63
float s=3.14;
double d=3.14;
boolean ok=true;
char c='a';
3.14 By default double is followed by f
float s=3.14f; (F is not case sensitive
java unsigned
character
Can be assigned to int
Can be assigned the value 'ha'
'\u0043'//u ni kou de character set
One character in Java is two bytes and can store Chinese characters.
array
int[] a=new int [10]; //Define an int array of size 10 //java must create a new one int[] a= (new int[] can also be added here) {1,2,3,4,5};//assignment int [][]a=new int[3][4];//two-dimensional array for(int i=0;i<a.length;i++) { Stystem.out.println(a[i]); }Print array
string
String s="Hello";//String is essentially a class rather than a basic type System.out.println(s); System.out.println(s.substring(1,4));//Intercept part of the output for(int i=0;i<s.length();i++) { System.out.println(s.charAt(i)); }
String comparison
Stirng s1=new String("Hello");//This is the essence of the program① String s2="Hello"; //This is the abbreviation ②
Stirng s1=new String("Hello"); Stirng s2=new String("Hello"); String s3="Hello";
①new assignment cannot be shared with each other
②The values assigned by the equal sign can be shared with each other.
Things coming out of new must be on the heap
The type of class in java is equivalent to mylok in C
String content comparison (not address
System.out.println(a==b);//This is an equal sign comparison, what is compared is the address System.out.println(s1.equals(s2));
Variables of subclass type can be assigned to parent class
equals是object
kind
To create a class, you must first create a new one (equivalent to initialization)
student =new student();//Equivalent to an initialization
Objects must be created on the heap and not on the stack
private int id;
After setting private protection, use methods to modify and obtain data in the class
The names of the classes can be the same, but the classes cannot be exactly the same (parameters
Java memory is automatically cleaned after it is released, no need to do it manually
piece
Static block new is executed only once multiple times
Ordinary block new is executed once
Static variables only have one copy of memory
constant
Java's final is equivalent to C's const for protection.
Bag
package cn.edu.hit;//This is a package cn.edu.hit.Student s=new cn.edu.hit.Student();//Call the Student in the package
course code
class Student{ private int id; private String name; public Student(){ } public Student(int id){ this.id=id; } public Student(String name){ this(0); this.name=name; } public Student(int id,String name){ this(name); } public void finalize(){ System.out.println("Goodbye!"); } public int getId(){ return this.id; } public void setId(int id){ this.id=id; } public String getName(){ return this.name; } public void setName(String name){ this.name=name; } } public class Hello{ public static void main(String[] args){ Student s=new Student(); s.setId(1); s.setName("Alice"); System.out.println(s.getId()); System.out.println(s.getName()); s=null;//Manually turn him into garbage System.gc();//Forced cleaning } }
Class inheritance
public class Student extends Preson//This is a kind of inheritance
The constructor of the subclass must call the constructor of the parent class (default parameterless constructor)
super();//Actively call the parent class constructor super.;//Actively call the parent class method this();//Actively call your own constructor this.;//Actively call your own method
! ! ! When defining a parameterized constructor for a subclass, you must also define a parameterless constructor for it.
The constructor cannot be inherited and must be called with super
Array-like
ss[0]=new Student(" "," "," ");
This is called initialization
sort
in Arrays.sort(ss);
abstract class
public abatract class Person{ }
Abstract classes cannot be instantiated, cannot be new, and can only be inherited as a parent class.
interface
class Dog implements Animal;
Interfaces play a certain role in type conversion, and accessing interfaces can also wash out original functions.
Exception handling
public static int div(int x,int y)throws{ try{ } catch(){ throw } } finally{//will be executed in the end }
try throws throw catch finally
File processing
1. Create a file: You can use File
the class's constructor to create a file object, and then use createNewFile()
methods to create the actual file.
File file = new File("path/to/file.txt"); file.createNewFile();
2. Write to file: You can use FileWriter
the or BufferedWriter
class to write data to a file. These classes provide various methods for writing to files, such as write()
and append()
.
FileWriter writer = new FileWriter("path/to/file.txt"); writer.write("Hello, World!"); writer.close();
3. Read the file: You can use FileReader
the or BufferedReader
class to read the contents of the file. These classes provide various methods for reading files, such as read()
and readLine()
.
FileReader reader = new FileReader("path/to/file.txt"); int character; while ((character = reader.read()) != -1) { System.out.print((char) character); } reader.close();
4. Delete files: You can use File
class delete()
methods to delete files.
File file = new File("path/to/file.txt"); file.delete();
These are just some examples of common methods in file operations, Java provides many more classes and methods to handle file and directory operations. Remember to handle possible exceptions when performing file operations and use appropriate exception handling mechanisms.
Binary file operations
For binary file operations, you can use Java's InputStream
and OutputStream
classes to read and write binary data. Here are some common ways to do it:
1. Reading binary files: You can use FileInputStream
classes to read the contents of binary files.
try (InputStream inputStream = new FileInputStream("path/to/file.bin")) { int bytesRead; byte[] buffer = new byte[1024]; while ((bytesRead = inputStream.read(buffer)) != -1) { // Process the read data } } catch (IOException e) { e.printStackTrace(); }
2.Write binary data to a file: You can use FileOutputStream
classes to write binary data to a file.
try (OutputStream outputStream = new FileOutputStream("path/to/file.bin")) { byte[] data = {0x00, 0x01, 0x02, 0x03}; outputStream.write(data); } catch (IOException e) { e.printStackTrace(); }
3. Use BufferedInputStream
and BufferedOutputStream
: If you need to improve the efficiency of reading and writing, you can use BufferedInputStream
the and BufferedOutputStream
class to buffer the input and output streams.
try (InputStream inputStream = new BufferedInputStream(new FileInputStream("path/to/file.bin")); OutputStream outputStream = new BufferedOutputStream(new FileOutputStream("path/to/file.bin"))) { // read and write operations } catch (IOException e) { e.printStackTrace(); }
Please note that when working with binary files, you need to be particularly careful with the format and order of the data to ensure that the binary data is parsed and generated correctly. In addition, you need to understand the structure of the binary file and the specifications of the specific format to correctly handle its data.