- Method Rewrite essentials:
1. A method in a subclass returns a value type, a method name, and a parameter list exactly the same as a method in the parent class. 2. The modifier level of a subclass method cannot be lower than the method access level of the parent class. 3. The range of exception types thrown by subclasses cannot be greater than the range of exceptions thrown by the parent class.
- Get a class that corresponds to class objects:
1. Class syntax for using classes .2. Through the GetClass () method of the class object;3. Using the Forname () method of the class object to obtain4. For packaging classes, you can pass. The type syntax is obtained;
1. Final is used in the class to indicate that the method cannot be inherited. 2. Final is used for variables that indicate that the variable cannot be modified. 3. Final is used for methods that indicate that the method cannot be overridden.
Package com.cao.basics;/** * Final keyword probe * @author caoqingrong * */public class Test {private final int a;private String NA Me;public Test () {a = 3;} Public Test (String name) {this.name = name;} /** * This class cannot be compiled by, because the final decorated property A may not be initialized * The final decorated member variable has the following centralized initialization: * 1. Assign values directly when declaring variables; * 2. Assign values in the constructor method, but be aware that, if there are multiple construction methods, the values are assigned in each construction method. * 3. If the member variable also has a static keyword modifier, it can only be assigned when its declaration is made. */}
Take a look at the following example:
public class Test {//Use the final keyword to declare a reference type member variable private static final StringBuffer sb = new StringBuffer ();p ublic static void Main ( String[] args) {sb.append ("abc"); The line is compiled via//SB = new StringBuffer (); The line compilation does not pass the/** * Summary: The member variable for the reference type of the final keyword modification is non-modifiable and its contents can be modified */}}
public class Arraryinterfacetest {public static void main (string[] args) {i[] I = new i[2];//compiled through, array holds the address of the object//i i2 = new I ( );//compilation does not pass, cannot instantiate interface type i[0] = new J (); i[1] = new J ();}} Interface I{}class J implements i{}
Java exceptions are inherited from the Java.lang.Throwable class, and Java exceptions fall into two categories:
(1) Checked Exception: Must handle, capture processing or continue to throw, except run-time exception, the other is to check the exception;
Common checked exception:nosuchmethodexception, illegalclassformatexception, Dataformatexception, ClassNotFoundException, SQLException
(2) Uncheckedexception (i.e.: runtimeexception): Can not do any processing, can also be captured or thrown; runtime exceptions are inherited from RuntimeException, Refers to the superclass of exceptions that are thrown during the normal operation of a Java virtual machine.
Common Runtimeexception:nullpointerexception, Unknowntypeexception, SystemException, ClassCastException, Bufferoverflowexception, IllegalArgumentException, etc.
- Why do public final modified variables often add static adornments? (public static Final sts = "A";)
Answer: In order to save memory, because for the final decorated properties, in each object can not be modified, can read only, so with static decoration, as a class variable, this can save space.
Package Com.cao.basics;import java.util.hashset;import Java.util.iterator;import java.util.set;/** * HashCode Explore * @ Author Caoqingrong * */public class Hashsettest {public static void main (string[] args) {set<string> Set = new HASHS Et<string> (); System.out.println (Set.add ("xyz")); TrueSystem.out.println (Set.add ("abc")); TrueSystem.out.println (Set.add ("xyz")); FalseFor (iterator<string> iter = Set.iterator (); Iter.hasnext ();) {System.out.println (Iter.next ());} System.out.println ("================== see below =======================");/*======================================= ===========================*/set<people> PS = new hashset<people> (); System.out.println (Ps.add (New People ("Zhangsan")); TrueSystem.out.println (Ps.add (New People ("Lisi")); TrueSystem.out.println (Ps.add (New People ("Zhangsan")); Truefor (iterator<people> iter = Ps.iterator (); Iter.hasnext ();) {System.out.println (Iter.next (). GetName ());} /** * Summary: * 1. When you add an object to a collection set, first the collection calculation is incrementedThe hashcode code of the object, according to the hashcode code to calculate a position to store to add the object, if there is no existing object, then set set that the object does not exist in the collection, * added directly. If an object already exists, the collection calls the Equals method of the object, judging to increase the object's comparison with an existing object, if False is assumed to not exist in the collection, and if true, the object is considered to be present and will not be added to the collection. * 2. When overriding the Equals method, you must override the Hashcode method. There is a principle: if two objects of a class are compared using the Equals method, the result is true, then two objects must have the same hashcode. */}}class people{private String name;public people (string name) {this.name = name;} Public String GetName () {return name;} @Overridepublic boolean equals (Object obj) {if (this = = obj) {return true;} if (obj instanceof people) {if (null! = obj && this.name.equals (((people) obj). Name) {return true;}} return false;} @Overridepublic int hashcode () {return Name.hashcode ();}}
- Title: Please say the Hashcode method, the Equals method, the relationship between Hashset,hashmap?
1.HashSet the bottom layer is implemented using HashMap;
The key of 2.HashMap is the object that is put into hashset, value is a Objec constant T object;
3.HashMap bottom is implemented with arrays.
4. When an object is added to the HashMap, the object's Hashcode method is called to get its hashcode value, and then the index of an array is computed based on that value (one position of the computed array).
5. The object to be incremented is compared to the object at that location (the Equals method), and if it is the same, replace the value of the existing object (entry type) at that location, if it is not the same, and continue repeating the process along the chain of entry. If the same object is not found at the end of the chain, the object is added to the array at this time, and the entry object at that position in the array is linked to the object.
6. For HashSet and HashMap, this is done to improve the efficiency of the element lookup, so that the lookup time does not change with the size of the set or map.
- Title: The difference between ArrayList, LinkedList and vector?
Both 1.ArraryList and vectors are implemented through arrays, ArrayList all methods are not synchronous, the vector is mostly public in sync, so vectors are thread-safe, and because of this, without regard to multithreading, Arrarylist efficiency is high.
The 2.LinkedList bottom adopts the chain list realization, therefore the linkedlist increment and the deletion element's efficiency is very high, but the ArrayList is relatively worse, but because the arrarylist array realizes, its element is the order storage, therefore its element retrieval speed quickly, Better than LinkedList.
- What is the difference between the topic:& and &&?
They can all be used as logical operators, the difference is && short circuit, and & does not short circuit. Additionally & is a bitwise operator, such as 3&5, with a result of 1. (converted into binary and transport)
- About Inner classes:
Package Com.cao.basics;import Java.util.date;public class Innerclasstest {@SuppressWarnings ("deprecation") public String getdatestr (date date) {//NOTE: The parameter date is an instance of an anonymous inner class, not an instance of date. return date.tolocalestring ();} public static void Main (string[] args) {innerclasstest ICT = new Innerclasstest (); string result = Ict.getdatestr (new Date () {//Anonymous inner class, which is a subclass of Date public String tolocalestring () {// Rewrite the toLocaleString () method of the date class to return "Hell world!";}); SYSTEM.OUT.PRINTLN (result);/** * Summary: Java has four kinds of internal classes, respectively, * 1. Static inner class; statically inner class * 2. member inner classes; member Inner class * 3. Local inner class; loc Al Inner class * 4. Anonymous internal classes; anonymous inner class */}}
- About generics
Package Com.cao.basics;import Java.util.arraylist;import Java.util.linkedlist;import Java.util.list;public class genericextendstest {public void method1 (list<object> List) {}public void Method2 () {method1 (new arraylist< Object> ()); Method1 (new arraylist<string> ()); Method1 (new arraylist<integer> ());} Are method1, METHOD2 compiled and passed? Answer: Do not pass. public void method3 (list<? extends object> List) {}public void method4 () {method3 (New arraylist<object> ()); METHOD3 (New arraylist<string> ()); Method3 (new linkedlist<integer> ()); Are method3, METHOD4 compiled and passed? Answer: Pass. public void Method5 (list<?> List) {}public void Method6 () {METHOD5 (New arraylist<object> ()); Method5 (new Arraylist<string> ()); Method5 (new linkedlist<integer> ());} Are METHOD5, Method6 compiled and passed? Answer: Pass. /** * Conclusion: * 1. Inheritance of generics arraylist<object> inherits list<object> type, and arraylist<string> is not inherited list<object> . * 2.list<?> equivalent to list<? Extends object> * * * *}
- Multithreading: Wait, notify, Notifyall, and sleep methods are the connections and differences?
1. If a thread invokes the Wair method of an object, the thread must have a lock on the object (in other words, if a thread invokes the wait method for an object, the wait method must be in synchronized).
2. If a thread invokes the wait method for an object, the thread releases the lock on that object.
3. In Java objects, there are two types of pools (lock pool and wait pool)
4. If a thread invokes the wait method for an object, the thread enters the lock pool of the object (releasing the lock), and if another thread calls the Notify or Notifyall method of the object at some point in the future, the object waits for the pool to go up into the object's lock pool. To wait for the lock on the object, and if the lock succeeds, the thread will continue to execute along the path after wait.
5.sleep (long time), if a thread calls the Sleep method, it does not lose ownership of the lock on the object while sleeping.
- About synchronrized Keywords
1. In all synchronized methods of an object, at some point there can be only one thread that accesses these synchronized methods.
2. If a method is a synchronized method, then the Sycchronized keyword indicates that the current object is locked (that is, this);
3. If a synchronized mode is static, then the Synchronized keyword indicates that the object lock corresponds to the class object locked. (Each class has only one class object, regardless of the number of objects generated).
Package Com.cao.basics.thread;public class Threadsynchtest {public static void main (string[] args) {c c = new C (); T1 T1 = new T1 (c);//c = new C (); T2 t2 = new T2 (c); Thread t = new thread (t2); T1.start (); try {thread.sleep ($);} catch (Interruptedexception e) {e.printstacktrace ();} T.start ();}} Class C{//private Object Object1 = new Object (),//private object object2 = new Object ();p ublic synchronized void Hello () { C0/>//static//synchronized (object2) {try {thread.sleep ();} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("HELLO");//}}public synchronized void World () {//synchronized (Object1) {System.out.println (" World ");//}}}class T1 extends thread{private c c;p ublic T1 (c c) {this.c = C;} @Overridepublic void Run () {C.hello ();}} Class T2 implements Runnable{private C C;public T2 (c c) {this.c = C;} @Overridepublic void Run () {C.world ();}}
- Can a character in Java represent a man?
Yes, a char in Java represents a length of 16 bits. As follows:
public class Chartest {public static void main (string[] args) {char c = ' good '; System.out.println (c);}}
Find a job review of the Java Foundation