I need to record some of the things I saw in Sunday, or I will forget.
Hashcode, equals:
1 Each Java object has a hashcode and equals method.
The ultimate Java class is the object class, so how does the object class label itself, that is, how the object class distinguishes each other. is to infer by using their hashcode and equals.
(Hashcode is implemented through the hash algorithm)
2 The JVM every new object, will be thrown into a hash (hash table), so that the next time you compare or get the object can be based on the object of the hashcode to the table to take, can improve efficiency.
We have to know that Java has a memory limit, so it's theoretically not possible to be new infinitely.
Compare Hashcode First, if there are other data on this list with equals.
We may ask, that is not the direct use of equals, of course, this is possible. But we want to see the hashcode efficiency problem, hashcode the main purpose is to reduce each call equals.
3 The question of rewriting hashcode and equals:
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/
In general, we do not need to rewrite these two functions. For objects like integer double, Java has rewritten these two functions for us, which is why as long as two string contents their equals are the same, according to reason, because equals compares the object's reference.
What needs to be rewritten: when we use the HashMap in the map, we can't find the class if we have to override it with a custom class as a key value. Because the class is inherited from object, it uses the object's Hashcode
So two values the same data, in this will become two numbers.
is to use a custom key to rewrite it.
/ o:
File class: Can be files, or can be file directories. By using the file class, we can get some properties of the files.
Stream: This is a series of data anyway, I don't know.
Java Input and output: (1) Character output, input stream (Writer, Reader) (2) byte output, input stream. (InputStream, OutputStream)
(as if there were stdio)
Difference
(1) Different reading and writing units
Byte streams are in bytes (8bit), and the character stream is in characters, and each read byte is looked at by the word Fu Ying shot.
(2) different processing objects
The byte stream can handle all types of objects (Pictures, videos, text ...). )
Character streams can only be type of characters
Note: A byte stream cannot be converted directly to character output, because the units that are read are different, and the array bytes can be converted to string in the output.
Package Com.hxw.io;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
public class FileCount {
/**
* We write a small program to detect the length of the file, do not look at this program is very long, you ignore the try catch block found after a few lines just.
*/
publicstatic void Main (string[] args) {
TODO auto-generated method stubs
int count=0; Statistics file byte length
Inputstreamstreamreader = null; File input stream
try{
Streamreader=newfileinputstream (New File ("D:/david/java/java Advanced/files/tiger.jpg"));
The file address in/*1.new file () can also be written as a D:\\david\\java\\java advanced \\files\\tiger.jpg, the previous \ is used to
* For conversion, the FileInputStream has a buffer, so it must be closed after use, or it may cause memory to be full, data loss.
*/
while (StreamReader.Read ()!=-1) {//reads file bytes and increments the pointer to the next byte
count++;
}
SYSTEM.OUT.PRINTLN ("---length is:" +count+ "byte");
}catch (Final IOException e) {
TODO automatically generated catch blocks
E.printstacktrace ();
}finally{
try{
Streamreader.close ();
}catch (IOException e) {
TODO automatically generated catch blocks
E.printstacktrace ();
}
}
}
}
The above program every read a self I have to use to FileInputStream, I output the result is "---length is: 64982 bytes", then carried out 64,982 times operation! It may be imagined that if the file is very large, such an operation would be a big problem, so the concept of the buffer is drawn. You can change StreamReader.Read () to StreamReader.Read (BYTE[]B) The number of bytes read by this method equals the length of the byte array, and the read data is stored in a byte array, returning the number of bytes read. Java I/o default is not buffered flow, the so-called "buffer" is the first to get from the stream of a byte sequence is called the buffer of an internal byte array, then you can get this whole block of byte data at once, no buffered stream can read only one byte, one byte at a time. There are two special input streams to implement buffering function, one is our common bufferedinputstream.
Author: blog Park Guo Jiasheng haha