Head First Java

Source: Internet
Author: User

Conditional statements

& | Can be used as a conditional statement, but it is a long connection, and both left and right expressions must be executed! This and && | | Different,&& and | | is a short connection, as long as the expression on the left has been able to calculate the result of the entire expression, the right expression will not be executed!

Inherited, overloaded, polymorphic

Overloading and polymorphism are not a concept:

    • Overloading: in-class, horizontal relations; Two method names are the same, but different parameters, the return type can be different (but not only the return type), access permissions can be different (because there is no more relationship between overloaded two methods, inheritance and polymorphism independent).

    • Polymorphic: A longitudinal relationship between classes (parent and subclass) , inherited from a method of a parent class, overriding overrides, having the same method name, having the same argument, returning the same type, or being a subclass of that return type (and the subclass guarantees that all the behavior of the parent class can be performed), access permissions are the same or more open.

    • With regard to the difference in the return type and access permissions, it can be seen from the perspective that the declaration of a subclass cannot affect how the declaration in the parent class is used, because the purpose of polymorphism is to use the parent class as a reference without caring about the implementation of the subclass, so the declaration of the subclass cannot be used in a way that the declaration in the parent class Thus, the return type subclass cannot exceed the declaration of the parent class, and the access permission subclass cannot restrict the declaration of the parent class.

Call Method:

    • The compiler depends on the reference type to determine which method can be called, not the actual type of the object. However, when you call the version, the most recent version is found based on the object's true type.

Why is it allowed to create object objects?

    • Because sometimes it is necessary to create a generic object, a lightweight object, the most common is the synchronization of threads.

Super, this

    • Each constructor can choose to call Super () or this (), but it cannot be called at the same time, and they must be the first statement of the constructor (the parent class constructor is the first step of the subclass constructor, and the parent class must be constructed before the child class).

Output formatting
Normal formatting

Formatting instructions:


Example of formatting:%1$,06.1f

    • 1$: Using the 1th parameter (the count is 1 base, that is, the first parameter after the formatted string) is formatted as a floating-point number;

    • Use "," to separate the integer part;

    • 06: The integer part is at least 6 characters, the insufficient part is padded with 0;

    • .1: A decimal number is retained after the decimal point;

    • F: floating point number;

date Formatting (String.Format (...) )

Formatting "" "means reusing previously used parameters

Date class

Java.util.Calendar:set (...) The month in is zero-based, so 0 represents January, and set is delayed.

Abnormal

Try/catch is used to deal with real exceptions, which programmers cannot predict or prevent, rather than logic errors of the program, while RuntimeException is mostly a logic problem for the program and is a non-check exception (unchecked, the compiler does not check).

Input/OutputStreaming (Inputstream/outputstream)

Design principles:

    • Object-oriented design: Each class can do just one thing.

    • So Java's input/output API has a connection-type stream that represents the link between the source and the destination, and the connection stream connects the stream to the other streams.

    • In general, it makes sense for a stream to be 22 connected-one representing the connection (the source of the input or the destination of the output) and the other representing the method to be called (the high-level API, which reads out the true high-level data type or outputs the true high-level data).

    • For example, FileOutputStream is used to write bytes to a file, but usually we do not write directly, but we write at the object level, so high-level connection streams are required to handle high-level object data to the byte stream.

    • This makes it possible to achieve maximum adaptability through different combinations without having to consider all possible situations in a stream class (which is not possible).

Serialization of
  • Tag interface:

      • The serializable interface is also used as the tag interface for the marker or tag class, and all classes that need to be serialized need to declare the implementation of the interface, and there is actually no method that needs to be implemented, just markup to tell the JVM that the class can be serialized.

      • When a class is serialized, all of its member variables are also serialized, so its member variables must also be serializable, that is, the serializable interface is implemented.

  • Filter:

      • If a member variable does not need to be serialized, it is tagged with the keyword transient, and serialization skips the variable.

  • Point invariance:

      • Serialization distinguishes between two variables pointing to the same object, and if two variables point to the same object when serializing, the two variables still point to the same object when deserializing.

  • Parent class and child class:

      • A non-Serializable parent class (not final) can have a serializable subclass, but when the object is deserialized, the constructor of the parent class executes as if the new object was created (the constructor of the serializable class is not executed), and the member variable belonging to the non-Serializable parent class and its ancestor class is initialized by the constructor. However, member variables that are overloaded by serializable subclasses are serialized and deserialized according to the definition (whether transient) in the subclass.

  • Static

      • Static variables are not serialized, and static variables that are deserialized will maintain the original appearance of the class, rather than the stored value.

  • Serialized version Serialversionuid:

      • By explicitly saving this ID in a class, you can guarantee that the serializable class remains the same ID after evolution, so that the class will still be able to deserialize the previously serialized data after the evolution of the non-corrupt serialization (the new variable uses the default value).

Generic type
    • Use universal characters in method parameters? , the compiler blocks any behavior that could break the collection referred to by the reference parameter, which is read-only and not writable:

Class ChildClass extends Superclass {...} public void f (list<? extends superclass> List) {list.get (0);//OK List.add (new ChildClass ());//Wrong} This is To: public <t extends superclass> void f (list<t> List) {list.get (0);//OK List.add ((T) New ChildClass ()) ;}


Head First Java

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: info-contact@alibabacloud.com 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.