One, final
1. A class that is final modified cannot be inherited
2. The final modified method cannot be overridden
3. The final modified variable cannot be changed
The point is the third sentence. The final modified variable can not be changed, what can not be changed, is a reference to the variable? or the contents of a variable? Or are they both not to be changed?
Public classUser {PrivateString name; PublicUser (String name) { This. Name =name; } PublicString GetName () {returnname;} Public voidSetName (String name) { This. Name =name;} Public Static voidMain (string[] args) {FinalUser user=NewUser ("Zhang San"); User.setname ("John Doe"); System.out.println (User.getname ());//run no problem, output: John Doe }}
Public classUser {PrivateString name; PublicUser (String name) { This. Name =name; } PublicString GetName () {returnname;} Public voidSetName (String name) { This. Name =name;} Public Static voidMain (string[] args) {FinalUser user=NewUser ("Zhang San"); FinalUser user2=NewUser ("John Doe"); User=user2;//compilation does not pass, Error: (9) Java:cannot assign a value to final variable user}} is visible, the final modifier is immutable as a reference to a variable, not a reference to the content that the reference points to can be changed
Summarize:
The final modified variable, regardless of the variable is the variable, remember that immutable is a reference to the variable instead of referring to the content of the object.
Final modified constants, which are stored in the constant pool of the calling class during the compilation phase
Two, static
1. Static and static methods the
Static keyword is the most basic use of:
(1). A variable that is modified by static is a class variable, and can be referenced directly by the name of the class, without the need for a new class
(2). A method that is modified by static is a class method, which can be referenced directly by the class name. Method name, no new class
The static modified variables and methods are uniformly classified as classes, and are shared among class instances, changing everywhere.
2. Question: Why does the JDK put different static resources in different classes instead of putting all the static resources in one class?
(1). Different classes have their own static resources, can implement static resource classification. See also: Tool Class
(2) under the Utils package. Avoid duplicate names. In different classes, you can have static variables with the same name and static methods
(3). Avoid infinite growth of static resource classes
said so much to classify
3. Reference problems between static and non-static resources
Static resources are loaded when the class is initialized, Non-static resources are loaded at the time of class new. Class is initialized earlier than the class's new
so:
Static methods cannot refer to non-static resources
Static methods can reference static resources
Non-static methods can reference static resources
4. Static block
Static block code executes only once, and only when the class is initialized
Conclusion:
(1). The load order of static resources is
(2) loaded strictly in the order in which static resources are defined. Static variables defined after static code blocks can be assigned, but cannot be accessed
static{
C = 3;//can be assigned without a definition
System.out.println (c);//error, cannot access
}
private static int C;
(3). Static code blocks are loaded strictly in order of the parent class static code blocks---> subclass static blocks of code, and load only once
Three, Java object Representation 1: Serialize serizaliable and transient
Normally in Java memory objects, is unable to do IO operation or network communication, because of the IO operation or network communication, They have no idea what the object in memory is, so the object must be represented in some way, that is, the state in the 4 storage object. The representation of a Java object has many styles, and Java itself provides a way for the user to represent the object, which is serialization. That is, serialization is just one way of representing an object
serialization: Converting an object into a string of binary representations of byte arrays, by saving or transferring those byte data for persistence
deserialization: Re-structuring the byte array to an object
Four, Java object Representation 2:xstream implementing XML for an object
1. There are a variety of ways in which objects are represented, and serialization is just one of them. Data can also be JSON, XML. (Google's protobuf is used in Redis serialization)
2. Using XStream to represent an object
XStream is a tool for translating between Java objects and XML. The core class in XStream is the XStream class. (Need to import jar package)
Java code:
XStream xs = new XStream (),//xstream object
Xmlobject xo = new Xmlobject (Ten, 10.5, "Max");//java object
String str = xs.toxml (XO);
Java objects are represented in XML:
<com.xrq.test.xmlobject>
<count>10</count>
<price>10.5</price>
<phone>110</phone>
</com.xrq.test.xmlobject>
3.XML Convert to Java object
XStream xs = new XStream ();
String xmlstr = "<com.xrq.test.XmlObject><count>10</count><price>10.5</price>< Phone>110</phone></com.xrq.test.xmlobject> ";
Xmlobject xo = (xmlobject) xs.fromxml (XMLSTR);
Five, interfaces and abstract classes
1. The role of Interface interface
(1). An interface is an abstraction of behavior. From a design standpoint, the presence of an interface can help clarify the business, leverage interfaces to tell developers what to do, and limit naming conventions
(2). To compensate for the lack of Java class single inheritance, a class can implement multiple interfaces. Simultaneous interfaces can also inherit multiple
(3). Reduces the coupling of the code. Because of the Java polymorphic Nature, the interface reference can accept the object of the subclass, and after instantiating the declared interface with the implemented subclass, the method that the subclass overrides can be called through the interface. That is, the place where the interface is invoked is independent of the local law implementing the interface. (think of the usual interface call)
2. Abstract class and interface differences
(1). The interface is an abstraction of the action, indicating what the object can do, such as people can eat, and cats can eat, as long as there is the same behavior.
Abstract class is an abstraction of the source, which represents what this object is, such as a cat is an animal, a dog is an animal.
(2). You can implement multiple interfaces and inherit only one abstract class
(3). An abstract method can only be defined in an interface public abstract void Add (); Note methods cannot be decorated with private, usually public abstatic omitted
There can be common methods in abstract classes
(4). There can be only static data members that cannot be changed in the interface
Abstract classes can be normal data members
Java face question two: keywords in Java