Common Mistakes and difficulties in Java Learning (1)

Source: Internet
Author: User
Tags float range map class modulus

1. Scope: public, private, and protected. The differences are as follows: scope: current Class, same package, Child Class, other package, public √ protected √ × friendly √ × private √ × when no write is performed, friendly 2 and Anonymous Inner Class (anonymous internal class) can extends be inherited?) other classes and implements (implemented) interface (interface) A: anonymous internal classes are internal classes without names. It cannot be extends (inherited) other classes, but an internal class can be used as an interface and implemented by another internal class. the difference between Collection and Collections A: Collection is the upper-level interface of the Collection class, and Its Inheritance interfaces mainly include Set and List. collections is a help class for collection classes. It provides a series of static methods for searching, sorting, thread security, and other operations on various sets. string s = new String ("jzy"); creates several objects. abstract classes do not necessarily contain abstract methods. The exception thrown by overwriting methods cannot be more restrictive than the original method. 6. You cannot use this in static methods. 7. compile MyClass. after java, three bytecode files are obtained: MyClass. class, MyClasslittle $. class MyClass $1. class. what is the answer? There are two internal classes in the MyClass class: one is named little, and the other is anonymous. how to throw an exception when the method encounters an exception and does not know how to handle it? 9. class loading and calling sequence of constructor Methods: First recursively Load Static members/code blocks of the parent class (first of the object ); load the static members of the class in sequence. Call constructor: Call the constructor of the parent class recursively. By default, the same parameter of the parent class is called. You can also specify a parameter of the parent class in the first line. Then, initialize the static code of the parent class ---> initialize the static code of the subclass -- >== (if you do not create an instance when creating an instance, will not be executed later) initialize the non-static code of the parent class --- >==== initialize the parent class constructor ---> initialize the non-static code of the subclass ---> initialize the subclass constructor = 10. in exception handling, if the code in try may produce multiple exceptions, multiple catch statements can be matched. If the parameter type in catch has a parent class subclass relationship, the parent class should be placed behind it, child classes are placed in front. 11. the while LOOP judgment conditions are generally program results, And the for loop judgment conditions are generally non-program results 12. one interface implements multiple interfaces using extends such as: public interface new interface extends transportation tools, metal products {} 13. array rotation 90 ° Coordinate Transformation assume that the array is a [n] [n], one of the elements is a [x] [y], after Rotating 90 degrees clockwise, the coordinates of the element are a [y] [n-x] 14. all Class objects are actually Class instances 15. annotation (Annotation) is introduced in JDK5.0 and later versions. It can be used to create documents, track code dependencies, and even perform basic compile-time checks. Annotation exists in the Code as '@ annotation name'. According to the number of annotation parameters, we can divide the annotation into three types: Tag annotation, single value annotation, and complete annotation. They do not directly affect the semantics of the program. They only exist as annotations (identifiers). We can program the reflection mechanism to access these metadata. In addition, you can select whether the annotation in the code exists only at the source code level during compilation, or it can also appear in the class file. 16. inheritance of methods and attributes (both method property subclass and Parent class are declared) Parent a = new Child ();. showData (); // call the subclass method System. out. println (. i); // call the attributes of the parent class 17. instance variable: defined in the class but not in any method. (New variables have initial values) local variables: variables defined in the method. Local variables must be assigned values before calculation, and the instance variables have been assigned initial values. This is a major difference between local variables and instance variables. 18. constructor Note: constructor is called when an object is generated, but not the constructor generates an object. The constructor is automatically called during object generation and cannot be called using commands. Only one constructor is used in the generation cycle of an object. Once the object is generated, the constructor fails. Multiple constructor methods can be constructed, but the parameter tables of multiple constructor methods must be different. Different parameter sequences belong to different constructor Methods: public student (string name, int) {} public student (int a, string name) {} is two different constructor methods. In the constructor, this indicates other constructor methods of this class: student () {}; student (string n) {this (); // indicates that student () is called ()} if student (int a) is called, this (int a) is used ). Note: When using this to call other constructor methods, this must be the first statement, followed by other statements. 19. thisThis indicates the current object. Public void printNum () {Int number = 40; System. out. println (this. number);} at this time, the instance variable is printed, not the local variable, that is, the variable defined in the class rather than the method. This. number indicates the instance variable. Who calls this. number then who is the number method of the current (this) object. If you do not specify who calls the method, the default value is this. This must be written when distinguishing between instance variables and local variables. 20. encapsulation: make the object attributes as private as possible, and make the object methods as public as possible. Private indicates that this Member property is the private property of this class. Public indicates that the property (method) is Public; Private indicates that the property (method) is accessible only within the class (visible inside the class ). (If you want to use private, you also need to use the set and get methods for other methods to call. This ensures unified access to attributes and facilitates maintenance of access permissions and validity of attribute data.) If there are no special circumstances, the attribute must be private, and the method should be public. 21. Inheritance. If the private method in the parent class is called by the quilt class, an error is returned during compilation. The subclass of the constructor of the parent class cannot be inherited, so there is no overwrite problem. (Non-constructor is acceptable) if the subclass accesses the constructor of the parent class, the system prompts that the method cannot be accessed during compilation. For method modifiers, The subclass method has a wider range than the method of the parent class. If the parent class is public, an error occurs if the subclass is private. The reason why the constructor runs the parent class before running the subclass is that the constructor cannot overwrite the constructor. When constructing an object, the system first constructs the parent class object and then the Child class object. Construct the sequence of an object: (Note: This step is also used to construct the parent class Object) ① recursively construct the parent class object; ② call the class member attribute to assign the initial value statement in sequence; ③ constructor of this class. Super () indicates calling the constructor of the parent class. Super () must be placed in the first line like this. This () is used to call the constructor of This class. If no constructor is defined, the super () method of the parent class is called (). To develop a good programming habit: add the default constructor without parameters for the parent class. Thinking: if we do not define a construction method without parameters and construct a construction method with parameters in the program, if there is no parameter in the method, will the system still call the constructor with parameters? It should not. 22. Polymorphism: Polymorphism refers to the type change during compilation, while the type remains unchanged during runtime. There are two types of polymorphism: ① polymorphism during compilation: Dynamic overloading during compilation; ② Runtime polymorphism: an object can have multiple types. Objects are objective and people's understanding of objects is subjective. For example, Animal a = new Dog (); view the format name; Dog d = (Dog). Declare the parent class to reference the subclass. (Think about the above format) Three Principles of polymorphism during runtime: (covering in Application) 1. The object remains unchanged (changing subjective understanding) 2. The call to an object can only be limited to methods of the compile-time type. For example, an error is returned when a runtime method is called. In the preceding example, Animal a = new Dog (); The compilation type of object a is Animal, and the runtime type is dog. Note: The compile-time type must be the parent class (or the same type) of the runtime type ). For statement: Dog d = (Dog). Force declare d as a type, and d is Dog (). Then d can call the runtime type. Note: a and d point to the same object. 23. Relational OPERATOR: instanceofa instanceof Animal; (this formula returns a Boolean expression.) a is an object variable, and Animal is a class name. The preceding statement is used to determine whether Animal labels can be attached to. Returns true if it can be pasted; otherwise, returns false. In the above question: a instanceof Animal returns True, a instanceof Dog also returns True, instanceof is used to determine whether to assign the previous object variable to the class name behind. Instanceof is generally used to determine whether a variable can be forcibly converted before forced type conversion. 24. In static methods, non-static members of the member class are not allowed, including member variables and methods, because classes are called at this time and there is no object concept. This. data is unavailable. Generally, the main method is a static method, so you can call the static method. The main method is a static method because it is the entrance of the entire software system, and there is no object in the system when you enter the portal, only class calls are allowed. Overwrite is not applicable to static methods. Static methods cannot be overwritten. (Static methods with the same name can be defined in the subclass, but there is no polymorphism. Strictly speaking, there is no polymorphism between methods and it cannot be called overwrite.) When static code blocks are modified (note: this code block must be out of any of these methods), so this code block can be loaded once when the code is loaded into the virtual machine to generate an object, and will no longer be executed. Generally, static code blocks are used to initialize static members. Static is usually used in Singleton mode development: Singleton is a design mode, which is higher than the syntax and ensures that a class has only one object in the system. 25. finalfinal can modify classes, attributes, and methods. When the final class is used, the class cannot be inherited, that is, the final class has no subclass. In this way, final can be used to ensure the consistency of actions during user calls and prevent the occurrence of subclass coverage. When a final attribute (variable) is modified, the attribute becomes a constant. JAVA uses final to define constants (note that constants must be capitalized in JAVA Naming Conventions): Final int AGE = 10; the constant address cannot be changed, however, the value saved in the address (that is, the object attribute) can be changed. Final can be used with static.? Static final int age = 10; in JAVA, the public static final combination is used to identify constants (fixed format ). When final is used to assign values in the constructor, the default value set by the system before the constructor is invalid relative to the constructor. Constant (here the constant refers to the instance constant: that is, the member variable) Value assignment: ① The Value assignment is explicitly declared during initialization. Final int x = 3; ② assign a value during construction. You can assign values to local variables at any time. Use final to define a method: This method is not overwritable. Public final void print () {}; methods can be defined using final to ensure method consistency (that is, they are not changed. If there is a final defined method in the parent class, the same method is inherited in the subclass. If the modifier private or static exists before a method, the system automatically adds final to the method. That is, the private and static methods are the final methods by default. Note: final does not involve inheritance. Inheritance depends on whether the class modifier is private, default, protected, or public. That is to say, whether to inherit depends on whether this method is visible to sub-classes. Final: modifying the basic data type indicates that the attribute cannot be rewritten. Modifying the referenced data type indicates that the application cannot point to other objects, but the content of the object can change. 26. abstractAbstract (Abstract) can modify classes and Methods Abstract. Although objects cannot be generated, they can be declared as compile-time types, but not runtime types. Final and abstract will never appear at the same time. Note: private void print () {}; this statement indicates the empty Implementation of the method. Abstract void print (); this statement indicates the abstraction of the method without implementation. If a class has an abstract method, the class must be an abstract class. If a class is an abstract class, there may be non-abstract methods. If a non-abstract class inherits an abstract class containing abstract methods, an error occurs during compilation. Because when a non-abstract class inherits an abstract method and only one class has an abstract method, this class must be an abstract class principle. This class must be an abstract class. This class conflicts with non-abstract classes, so an error is returned. Therefore, subclass methods must overwrite the abstract methods of the parent class. Method. Abstract AND static cannot be put together. Otherwise, an error occurs. (This is because static cannot be overwritten, and abstract must be overwritten to take effect .) 27. core concepts of interfaceJAVA: interfaces are similar to classes. A file can have only one public interface and the file name is the same. A public interface and a public class cannot be defined simultaneously in a file. In an interface, all methods are public and abstract methods. All attributes are public, static, and constant. Class must implement methods in the interface, otherwise it is an abstract class. The interfaces and classes in the implementation are the same. Public is not allowed in the interface, but public is not allowed in the process of implementing the interface in the subclass. (If public is left, an error is prompted during compilation: the object cannot implement methods from the interface .) ① An inheritance relationship can be defined between an interface and an interface, and multiple inheritance can be implemented between interfaces. For example, interface IC extends IA, IB {}; an interface is actually a standard and standard. ① Common attributes of different levels and system objects can be implemented through interfaces; write once as anywhere can be implemented through interfaces. take JAVA database connection as an example: JDBC standards; Data vendor implementation standards; user use standards. Interfaces are usually used to shield underlying differences. ② The interface is also used to maintain the stability of the architecture for the above reasons. JAVA has a special class: Object. It is the parent class of all classes in the JAVA System (direct parent class or indirect parent class ). The methods in this class can make all classes inherit. The following three methods are described as follows: (1) finalize method: The method called when an Object is reclaimed by garbage collection. (2) toString (): represents an object using a string. When we print the defined object directly, the returned value of toString () is implicitly printed. You can use a subclass as a toString () to overwrite the toString () of the parent class (). To get the desired representation, that is, when we want to describe an object in a custom way, we should overwrite toString (). 28. The String class is a special class in JAVA, and the String is a final class. The value of a String cannot be repeated. Therefore, a java vm (Virtual Machine) has a string pool dedicated to storing strings. If the String a = "hello" is encountered (note that there is no NEW, rather than creating a NEW String), the System Searches for "hello" in the String pool. At this time, there is no "hello" in the String pool ", then the system saves the string to the string pool, and then returns the address "hello" in the string pool to. If the system encounters String B = "hello" again, the system can find "hello" in the String pool ". The address is returned to B. In this case, a and B are the same. String a = "hello"; System. out. println (a = "hello"); the System returns true. Therefore, if you want to compare whether the two strings are the same (rather than whether their addresses are the same ). You can call equal: System. out. println (a. equal (B) for a. equal is used to compare the string order of two objects. A. equal (B) is a comparison of values of a and B. Note the following program: student a = new student ("LUCY", 20); student B = new student ("LUCY", 20); System. out. println (a = B); System. out. println (. equal (B); the returned results are both false. The following is the definition of equal (with this definition, return true or false) public boolean equals (Object o) {student s = (student) o; if (s. name. equals (this. name) & s. age = this. age) else return false;} if the value returned by equals () is the following process for implementing the Standard equals: public boolean equals (Object o) {if (this = o) return trun; // at this time, the two are the same if (o = null) return false; if (! O instanceof strudent) return false; // studeng s = (student) o for different classes; // forcibly convert if (s. name. equals (this. name) & s. age = this. age) return true; else return false;} "=" is a comparison address at any time, and such a comparison will never be overwritten. The above process is the standard process for implementing equals. 29. Java serialization when two processes perform remote communication, they can send various types of data to each other. Regardless of the type of data, it is transmitted on the network in the form of binary sequence. The sender needs to convert the Java object to a byte sequence, that is, the Java object serial number, before it can be transmitted over the network. The receiver needs to restore the byte sequence to a Java object, that is, deserialization. The process of converting a Java object to a byte sequence is called object serialization. The process of restoring a byte sequence to a Java object is called object deserialization. Object serialization has two main purposes: 1) the object's byte sequence is permanently stored on the hard disk, usually stored in a file; 2) transmits the object's byte sequence over the network. 30. Internal class encapsulation class: JAVA provides an encapsulation class for each simple data type, so that each simple data type can be loaded by the Object. In addition to int and char, the first letter of other types is capitalized as the encapsulation class. Conversion Method: int I = 10; String s = I + "; String s1 = String. valueOf (I); Int I = 10; Interger I _class = new integer (I); read the help documentation of javadoc. Additional content: "=" is a comparison address at any time, and this comparison will never be overwritten. Classes compiled by programmers and JDK classes are a cooperative relationship. (Because of polymorphism, JDK classes may be called, or JDK automatically calls our classes .) Note: In type conversion, the conversion between double, interger, and string is the most. 12.01 internal class: (Note: all internal classes can be used without internal classes. Using Internal classes can simplify the program and facilitate naming rules and hierarchical structure ). An internal class refers to defining another class within an external class. An internal class is a member of an external class and is attached to an external class. Internal classes can be static and can be modified using PROTECTED and PRIVATE. (The external class is not allowed: The External class can only use PUBLIC and DEFAULT ). Classification of internal classes: member Internal classes, local internal classes, static internal classes, and anonymous internal classes (graphics are required and must be mastered ). ① Member internal class: a member of an external class, which is in parallel with the attributes and methods of the external class. Instance variables of internal and external classes can coexist. Instance variable for accessing the internal class: this. Attribute instance variable for accessing the external class of the internal class: External class name. this. attribute. Advantages of the internal class of a member: (1) the internal class, as a member of the external class, can access the private members or attributes of the external class. (Even if the external class is declared as PRIVATE, it is still visible to internal classes .) (2) Use an internal class to define inaccessible attributes of an external class. In this way, the external class has lower access permissions than the external class's private. Note: Internal classes are a compilation concept. Once compiled successfully, they become completely different. For an external class named outer and its internal defined internal class named inner. After compilation, the outer. class and outer $ inner. class classes are displayed. (Compile a program test: In a TestOuter. java program, verify that several classes appear after internal classes are compiled.) The internal class of a member cannot have static attributes. (Why ?) If the external class accesses the internal class, use out. inner. when creating an internal class object, note that inner s = new inner () can be directly used inside the external class. (because the external class knows which class the inner is, it can generate an object .) To generate an internal Class Object (new) for external class, you must first create an external Class Object (the external class is available) and then generate an internal class object. Outer. Inner in = Outer. new. Inner (). Incorrect definition method: Outer. Inner in = new Outer. Inner (). Note: When Outer is a private class, the external class is private for its external access, so it is impossible to create an external class object, and thus cannot create an internal class object. ② Local internal class: the internal class defined in the method is called a local internal class. Similar to local variables, the public and private modifiers are not added before the local internal class, and the range is the code block that defines it. Note: The local internal class can not only access the instance variables of the external class, but also the local variables of the external class (but the local variables of the external class must be final at this time )?? A local internal class cannot be directly generated outside the class (to ensure that the local internal class is invisible to the outside ). To use a local internal class, you must generate an object and call the method of the object before calling its local internal class in the method. ③ Static internal class: (Note: the first three internal classes are similar to variables, so you can compare them with the reference variables.) static internal classes are defined in the class. static is used outside any method. Static internal classes can only access static members of external classes. Generate (new) a static internal class without external class members: this is the difference between the static internal class and the member internal class. Objects of static internal classes can be directly generated: Outer. Inner in = new Outer. Inner (), instead of generating external class objects. In this way, the static internal class is actually a top-level class. Static internal classes cannot be defined using private. Example: two classes have the same method: People {run () ;}machine {run () ;}at this time there is a robot class: class Robot extends People implement Machine. in this case, run () cannot be directly implemented. Note: When a method name conflict occurs between a class and an interface (or an interface and an interface), internal classes must be used for implementation. An interface cannot fully implement multi-inheritance. An interface can be used with an internal class to implement real multi-inheritance. ④ Anonymous internal class (must be mastered): An anonymous internal class is a special local internal class that implements interfaces through an anonymous class. IA is defined as an interface. Ia I = new IA () {}; Note: An anonymous internal class must be behind new. It is used to implicitly implement an interface or implement a class without a class name, according to polymorphism, we use the parent class name. Because it is a local internal class, all restrictions on the local internal class apply to it. An anonymous internal class is the only class without constructor. The system automatically names the anonymous internal class Out $1. class during compilation. If an object is of the interface type during compilation, the running type is the class that implements this interface. Because there is no constructor for anonymous internal classes, the scope of use is very limited. 31ExceptionException has two subclasses: Runtime exception (not checked exception) Non-Runtime exception (checked exception) (Note: no exceptions are detected during compilation, and the program syntax errors are checked during compilation, exception is a concept of program errors during running .) In Exception, all non-unchecked exceptions are checked exceptions and no other exceptions exist !! The unchecked exception is caused by the programmer's failure to perform the necessary checks due to his negligence and errors. It must be a virtual machine internal exception (such as a null pointer ). To cope with non-checked exceptions is to develop good inspection habits. Checked exceptions are inevitable. You must define the response methods for checked exceptions. Check exceptions must be beyond the scope of virtual machines. (For example, "file not found") How to Handle checked exceptions (all checked exceptions must be handled): first, understand the mechanism of exception formation: when an exception occurs in a statement in a method, it throws (throws) An exception object, and the subsequent statements do not return the upper-level method. After the upper-level method receives the exception object, it is possible to handle this exception or forward it to its upper level. There are two ways to handle received checked exceptions: throws and try. Note: The error method may be JDK or a program written by a programmer. throw must be used no matter who writes the program. For example, public void print () throws Exception. For method a, if it defines throws Exception. When Method B is called to return an exception object, method a does not process it, but returns the exception object to the upper-level. If no methods are processed, the system returns the object to the primary method, the program is aborted. (Avoid using methods returned by all methods, because a very small exception will interrupt the program ). If there is a row of throw new Exception () in the method program and an error is returned, the subsequent program will not be executed. Because after an error is returned, the subsequent programs certainly have no chance to execute, so JAVA considers that future programs are unnecessary. If an error is found in Try, that is, the catch clause after try will not be executed. A try can follow up multiple catch statements to handle different situations. Only one catch can be matched for a try. We can write multiple catch statements, but we cannot write the exception location of the parent type before the child type commit iton, because the parent type must be matched before the child type, and all the child types become nonsense. JAVA compilation error. After try and catch, you can use the finally clause again. The Code statements in the statement are executed in any way (because of this feature of the finally clause, the statements for releasing resources and disabling connections are generally written in it ). If a check (throw) exception is written in the program but the possible check result is not processed, the program reports an error. Exception has a message attribute. When using catch, you can call: Catch (IOException e) {System. out. println (e. message ()}; Catch (IOException e) {e. printStackTrace ()}; The above statement tells us the process of the error type, which is very useful in debugging. There are two principles in development: ① how to control the try scope: According to the linkage and relevance of the operation, if the error thrown by the previous program code block affects the running of the subsequent program code, in this case, the two program codes are associated and should be placed in the same try. ① For the exceptions that have been found, there are two solutions: throw (positive) and try catch (negative. Place try catch in a location that can handle exceptions well (that is, place it in a location that can handle exceptions ). If there is no processing capability, continue to throw. When we define an exception class, we must make it inherit initon or RuntimeException. Throw is a statement used to Throw exceptions. Throws indicates that if an exception is thrown in a lower-level method, this method will not be processed and will continue to be thrown up. Throws follow the exception type. 32. assert is a debugging tool followed by a Boolean expression. If the expression result is true, the program is not affected. If a low-level error occurs for a false system, the assert information appears on the screen. Assert is only used for debugging. After the product compilation is complete, the online assert code will be deleted. If the exception thrown by the subclass method is the parent type thrown by the parent class method, the compilation will fail: The subclass cannot overwrite the parent class. Conclusion: The subclass method cannot throw more exceptions than the parent method. The exception thrown by the subclass is the same as that thrown by the parent class, or the child type of the exception thrown by the parent class. Or the child type does not throw an exception. If the parent type does not have throws, throws cannot appear for the child type. You can only use try catch. 33. HashMap uses arrays at the underlying layer, while HashSet uses HashMap at the underlying layer. The HashSet class has the HashMap attribute (how can we check attributes in the API ). HashSet is actually a HashMap of the (key. null) type. There is a key value but no value. Because of the preceding reasons, the implementation of TreeSet and TreeMap is similar. Note: TreeSet and TreeMap consume a lot of time, so they are rarely used. HashSet VS TreeSet: HashSet consumes a lot of space. Because of its sorting function, the resource consumption is very high. We should try to use it as little as possible, and it is best not to use it again. For the above reason, we try to use HashSet instead of TreeSet unless it must be sorted. Similarly, HashMap VS TreeMap: Generally, HashMap is used for sorting. The difference between HashMap VS Hashtable (note that the first letter in the table is lowercase here) is somewhat similar to ArrayList and Vector. Hashtable is a heavyweight component, considering concurrency, when the security requirements are high. 34. hashcode consistency during Java application execution, when calling the hashCode method for the same object multiple times, the same integer must be returned consistently, the premise is that the information used for equals comparison is not modified. If the two objects are equal according to the equals (Object) method, the hashCode method must generate the same integer for each Object in the two objects. Note: the equals (Object) method mentioned here refers to the equals method that has not been overwritten by the subclass in the Object class. If two hashCode () returns the same result, the equals methods of the two objects are not necessarily equal. If the two objects are not equal according to the equals (java. lang. Object) method, calling the hashCode Method on any of the two objects does not necessarily generate different integer results. However, programmers should realize that generating different integer results for unequal objects can improve the performance of the hash table. Edit this section to overwrite the HashMap object and obtain the corresponding Value based on the hashCode of its Key. When the equals method of the parent class is rewritten, The hashcode method is also rewritten to make the two equal objects obtain the same HashCode. In this way, when the object is a Key in the Map class, the two objects whose equals are true obtain the same value, which is more practical. 1: hashCode of the Object class. The structure of the memory address of the returned Object after processing. Because the memory address of each Object is different, the hash code is also different. 2: hashCode of the String class. According to the content of the String contained in the String class, a hash code is returned based on a special algorithm. As long as the String content is the same, the returned hash code is also the same. 3: For the Integer class, the returned hash code is the value of the Integer contained in the Integer object. For example, Integer i1 = new Integer (100) and i1.hashCode is 100. It can be seen that two Integer objects of the same size return the same hash code. 35. The close () method of the io stream in the stream is controlled by the programmer. Because the input and output streams have exceeded the boundary of the VM, resources may not be reclaimed. Principle: all resources that span the Virtual Machine boundary must be shut down by the programmer and do not expect garbage collection. Classes ending with Stream are byte streams. (Not important) pipeline stream: it is also a node stream used to exchange data for two threads. PipedOutputStreamPipedInputStream output stream: connect (input stream) RondomAccessFile class allows random access to the file GetFilepoint () to know the pointer position in the file and locate it using seek. Mode ("r": random read; "w": Random write; "rw": random read/write) The InputStreamread of an I/O package is a bridge conversion class that streams data from bytes to bytes. This class can be used to set the character conversion mode. OutputStreamred: the character-to-byte Bufferread has readline () to facilitate character input. In an I/O Stream, all input methods are blocking methods. Bufferwrite adds a buffer to the output character because it has few methods, so it uses the parent class printwrite. It can use byte stream objects, and there are many methods. A stream object is called object serialization, but not all objects can be serialized. Only one interface must be implemented when the class is implemented: Serializable under the IO package ). This interface does not have any methods. Such an interface is called a tag interface. Class Student implements Serializable serializes an object to a persistent medium through a stream. Transient is used to modify attributes. Transient int num; indicates that this attribute is ignored when attributes are serialized (that is, ignore and not make them persistent ). All attributes must be serializable, especially when some attributes are also objects. Judge whether an attribute or object can be serialized: Serialver. The Serialver TestObject (TestObject must be compiled) 36. toString () toString method is a "self-describing" method. to output self-useful information, rewrite this method. 37. suppressWarnings: Warning 38 when the compilation is canceled. generally, a Thread uses the Runnable interface instead of the Thread class. Runnable can achieve data sharing 39. static methods can only be inherited. An initial value of 40 must be assigned to interface member variables. float Type 32 bits (not the same as int), where 1 bits are symbol bits, the index is 8 bits, and the ending number is 23 bits. It should be emphasized that the float precision is 23 bits (that is, if the number of 23 bits can be accurately expressed, the float will be truncated if it exceeds the value ). Decimal places indicate precision based on the length of the ending number. For example, if pi is 3.14, the precision is 2 bits, and pi is 3.1415, and the precision is 4 bits. What is interesting is that the int precision is greater than float, because the int precision is 31 BITs, which is greater than float. Because the int type range is (-2 ^ 31 )~ (2 ^ 31-1), while the float range is (-2 ^ 128 )~ (-2 ^ 128-1), so remember: int type data can represent a smaller range than float type, int type data represents a greater precision than float. The double type can represent 64-bit, of which 1-bit symbol bit, 11-bit index, 52-bit ending number. The double precision is more accurate than the int type, but not long; the double range is greater than long. Note that the literal value of a floating point is double by default, the suffix D d is double, and f F is float. As shown in figure-20 below, the compile error occurs. The principle is that the int and long character types are a 16-bit unsigned integer and a 2-digit number, this value is the unicode encoded value of a character. 41. rule of modulus: The result symbol of modulus is always the same as that of divisor. int a = 5; int B =-3; int c = a % B; divisor is 5, the modulo result is 2int a =-5; int B = 3; int c = a % B; the divisor is-5, and the modulo result is-2. 42. Stack, all local variables in Java are allocated in the stack memory (including the variables declared in the method and the parameters of the method ). Java method calls use stack implementation. recursive calls are stack implementation. During recursion, all the temporary variables should be allocated according to the recursive depth. The stack overhead is very high and the performance is poor. Be sure not to exceed the stack size, and the condition must be given, otherwise, stack overflow error occurs. Stack storage is risky. Stack Overflow occurs when data is stored in this mode. On windows, the default JVM stack space is 64 MB, you can also modify the optimization parameters without further explanation. The storage feature of stack memory space is "first-in-first-out". The storage features of heap memory space are different from those of stacks, similar to a wide range of scattered Sands, you can "stack" local variables and heap object space allocation anywhere. U. Java local variables and method parameters are allocated in the stack. The size is allocated according to the variable type. U. The object is allocated in the heap, allocate space according to declared attributes (instance variables) in the class. ü space size of the basic type variable: the space size of the basic type, the value is of the basic type. U. The value of the referenced variable is the address value of an object, the reference variable uses the address to reference a heap object. the space occupied by the referenced type variable and the value management are "transparent (invisible)" and managed by the Java System: variable space and value management are transparent. this is a local variable. After the constructor call ends, it disappears like the parameter 43. passing rules of java method parameters: value-based passing ü Java method parameters are passed in only one way, value-based passing, is the copy of the variable value U basic type is the copy of the value U reference type is the copy of the reference value (Address) 44. static import: (class names of static methods can be omitted when calling static methods) impo Rt static + path name + class name + method name or member variable name; 45. before using local variables, you must declare and assign the initial values. before using the member variables, you must declare them, but do not assign the initial values. The reference type is used on objects. An object can be pointed to by multiple references, but at the same time, each reference can only point to a unique object. If an object is pointed to by multiple references, any reference that modifies the attributes of the object will be reflected in other references. 46 The new Keyword completes three things when an object is generated: a) it opens up memory space for the object. B) Call the constructor of the class. C) return the address of the generated object. E) The constructor of the class cannot be explicitly called. The constructor is typically called by using the new keyword implicitly. 47. constructor U constructor cannot inherit! Ü instantiate the subclass and recursively allocate all space for the parent class. ü The subclass constructor must call the parent class constructor. ü the class must have a constructor (parent class, subclass) 48. polymorphism: the parent type references the child type. during compilation, determine the type (all are Question). during runtime, determine U q and check () the methods are implemented through the "object's dynamic binding" polymorphism. Java references two types of variables: One compile-time type and the other runtime type. The compile-time type is determined by the type used when the variable is declared. The runtime type is determined by the object actually assigned to the variable. If the compile-time type and runtime type are inconsistent, the parent class constructor is called using super (). It must be written in the first line of the subclass constructor. u this () it must be written in the first line of the subclass constructor. If U has super (), it cannot have this (). The two are mutually exclusive. this does not exist during compilation. this occurs only during runtime. If the parent class does not have a parameter constructor, you must specify the parameter constructor that calls the parent class in the subclass! Ü programming suggestion: All classes provide a non-parameter constructor! It is very important to understand the Object Instantiation process in Java to reduce the trouble during inheritance. The main steps of the instantiation process are as follows: Step 1: before creating a class sequence, check whether the class is loaded. class file is loaded into the memory). If this class is not loaded, all parent classes must be loaded before the class is loaded. The policy used during Java runtime: lazy loading (On-Demand Loading): If it is used for the first time, it is loaded only once. Find the class file (. Class) through the path specified by CLASSPATH. After loading, it is an object and the type is class. Step 2: allocate object space in the memory heap. Recursively allocate all attributes of the parent class and subclass. The 33 attributes of Dana IT training group are initialized from the worker by default. The self-initiated token is set to "0. Step 2: assign values to the values of the rows. Step 2: recursively call the parent class constructor. (No parameter constructor is called for the parent class by default !) Step 2: Call the constructor of this class. 49. method override is implemented by dynamically binding methods. the java Virtual Machine runtime determines which method to execute, and the final implementation subclass method of java is allowed in principle in actual project development! Spring, Hibernate, and Struts 2. These frameworks use the "dynamic inheritance proxy" technology. Using final classes will affect the implementation of "dynamic proxy technology. the final modifier attribute indicates "cannot be changed", and the static modifier attribute indicates "only this one copy" of the class. Note the following points: Partial final local variables, only method parameters that can be initialized and changed to writable final can be referenced and referenced. However, the attributes of an object can be changed to compare whether the reference values are equal: "=" u compare whether the object content is equal: xxx. the default hashCode () value of the equals () method is an integer converted from the current heap object address. This integer is not the memory address! 50. notes on inheritance a) constructor cannot inherit B) methods and attributes can be inherited c) constructor of subclass implicitly calls constructor without parameters of parent class d) when the parent class does not have a constructor without parameters, the subclass needs to use super to explicitly call the constructor of the parent class. super refers to references to the parent class e) the super keyword must be the first statement in the constructor.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.