Question: What happens if the main method is declared private?
Answer: Can compile normally, but will prompt "Main method is not public" when run.
Question: What is the difference between a reference and a pass in Java?
Answer: A pass-through refers to an address rather than the value itself, and a pass value is a copy of the passed value.
Question: What else to consider if you want to override the Equals method of an object?
Answer: Hashcode.
1. If you overload equals, for example, based on the content of the object, while preserving the hashcode implementation, it is likely that the two objects are "equal" and hashcode are different.
This way, when you use one of them as a key to save to HashMap, hasotable, or HashSet, and then "equal" to find another as the key value to find them, it is not found at all.
With HashMap, if key is a custom class, you must override Hashcode () and Equals ().
For each object, the Hashcode () method can be used to generate an integer value (hash code) that, when processed, will be used as an array subscript to hold the object's corresponding entry (storing the object and its corresponding values). The Equals () method is used when inserting values or queries in HashMap. When the hash code for the insertion value or query value in HashMap is equal to the hash code in the array, the Equals method is used to compare whether the key value is equal, so you want to use the self-built object as the HashMap key. This object must be overridden to inherit the hashcode and Equals methods of the objects.
2. There is no hashcode () and Equals ()? Why do you have to rewrite, directly with the original not? In HashMap, if you want to compare keys for equality, use both functions at the same time! Because the Hashcode () method of a custom class inherits from the object class, its Hashcode code is the default memory address, so that even if there are two objects of the same meaning, the comparison is not equal, for example, two "sheep" objects are generated, and normal understanding of the two objects should be equal, But if you don't rewrite the Hashcode () method, the comparison is not equal!
HashMap in the comparison key is this, the first to find out the key hashcode (), compare its value is equal, if equal to compare equals (), if equal to think they are equal. If Equals () is unequal, they are not considered equal. If you only rewrite hashcode () do not override the Equals () method, when you compare Equals () only to see if they are the same object (that is, to make a comparison of memory addresses), it must be overridden by two methods. HashMap is used to determine whether the key is equal, in fact, is called HashSet to determine whether the element is equal.
Question: How is Java's "write Once, run everywhere" implemented?
Answer: Java programs are compiled into a class file consisting of bytecode that can be run on any platform, so Java is platform independent.
Question: Explain the function of each keyword in the declaration of public static void main (String args[])
Answer: The Public:main method is the first method that is called when the Java program is run, so it must be visible to the Java environment. So the visibility is set to Pulic.
The Static:java platform calls this method without creating an instance of the class, so this method must be declared as static.
The Void:main method does not return a value.
String is the type of the command-line input parameter, and args refers to the string array that the command line passes into.
Question: the difference between = = and equals
Answer: = = Compare two objects in memory is not the same object, that is, in memory storage location consistent. The values stored by two string objects are the same, but may be stored in different places in memory.
= = Comparison is a reference and the Equals method compares the content. public boolean equals (object obj) This method is provided by object objects and can be overridden by subclasses. The default implementation returns true only if the object and itself are compared, which is equivalent to = =. String, BitSet, Date, and file all override the Equals method, and for two string objects, a value equal means that they contain the same sequence of characters. For a wrapper class of a basic type, a value equal means the value of the corresponding base type.
Java interview questions, collecting