java8--Object-oriented (crazy Java handout 3) Review notes

Source: Internet
Author: User
Tags instance method modifier

1, if a wrapper class and a basic type comparison size, or two basic types to compare size, directly with = = good;  
If two wrapper classes are compared, use Equals () and the return value is True,false. Or, using Xxx.compare (), the return value is 1,0,-1. (The Equals () method here, which has been overridden by the wrapper class, is not the Equals ()) of object   the difference between
2, = = and the Equals method  
When you use = = To determine whether two variables want to wait, the value in the stack memory is compared. If two variables are basic type variables and are numeric types, then the value of the underlying type variable is stored in the stack memory, so it returns true as long as the values of the two primitive type variables want to wait. But for two reference-type variables, because the stack memory is a reference to a variable, the = = will only return TRUE if the two reference-type variables point to the same object. = = is not available for two objects that do not have a parent-child relationship on the comparison type.   the
Equals () method is an instance method of the object class, but this method does not differ from = =. However, the wrapper class of the string class and the base type overrides the Equals () method, no longer compares the reference address of the reference object, but compares the referenced value.  
3, class member  
Static keyword-decorated member is a class member, there are class variables, class methods, static initialization block three parts, the static keyword cannot decorate the constructor. The static decorated class member belongs to the entire class. In addition to these three, a class can also include constructors and inner classes (including interfaces, enumerations), and inner classes can be decorated with static.   a
Null object can access the class member of the class to which it belongs.  
for the static keyword: class members (including methods, initialization blocks, inner classes, and enumeration classes) cannot access instance members (including member variables, methods, initialization blocks, inner classes, and enumeration classes). Because the scope of a class member belongs to a class, the scope of the class member is larger than the scope of the instance member, it is possible that the class member has been initialized, but the instance member has not been initialized, and if the class member is allowed access to the instance member, a large number of errors will be caused.

4. Final modifier
Final modified variables cannot be re-assigned after they get their initial values, so there is a difference between the final decorated member variable and the decorated local variable.
Final member Variable:
Unlike normal member variables, the final decorated member variable must be initialized by the programmer, and the final member is not implicitly initialized.
The final decorated class variable specifies the initial value in the declaration or in the static initialization block.
The instance variable of the final decoration, specifying the initial value in the declaration or in a non-static initialization block or in the constructor ;

Final Local variables:
The system does not initialize local variables, and local variables must be initialized by the programmer's display. So when you use final to decorate a local variable, you can either specify a default value when you define it, or you can specify no default value. If the final decorated local variable does not specify a default value when it is defined, you can assign an initial value to the FIANL variable in the following code, but only once, and cannot be assigned again.

The difference between the final decorated base type variable and the decorated reference type variable:
When you use the final decorated base type variable, you cannot reassign the underlying type variable, so the underlying type variable cannot be changed. But for a reference-type variable, it only holds a reference, and final only guarantees that the address referenced by the reference-type variable is constant, that is, referencing the same object all the time (the reference-type variable with the final decoration cannot be re-assigned). However, the full amount of this object can be changed (that is, you can change the contents of the object referenced by the reference type variable).

Final method
The final decorated method cannot be overridden. If you do not want subclasses to override a method, you can use final to decorate the method.

Final class
The final class may not have subclasses (not inheritable).

Immutable classes
The member variables of the class are decorated with the private and final modifier classes.
Available with parametric constructors
Do not provide setter methods
If necessary, override the Hashcode () and Equals () methods of the Objcet class.

Caching instances of immutable classes
If your program often needs to use the same immutable class instance, you should consider the instance of the cached person in the immutable class. After all, it doesn't make much sense to create the same objects repeatedly, and it increases the overhead of the system.
Caching is a very useful pattern in software design, there are many ways to implement caching, and different implementations may have large performance differences. However, blind use of the cache can also lead to degraded system performance, the cached object consumes system memory, so caching an instance with a small repetition probability will do more harm than benefit.

5. Abstract class
The method of the abstract modification must be overridden to make sense, so abstract cannot be used with private or final.
6. Interface
The interface does not protect the ordinary method, all the methods in the interface are abstract methods, JAVA8 interface has been improved to allow the definition of the default method in the interface, the default method can provide a method implementation.

An interface is a specification abstracted from multiple similar classes, and the interface does not provide any implementations. The interface embodies the philosophy of specification and realization separation.
Interfaces are typically defined by a set of common methods.
interfaces fully support multiple inheritance, an interface can directly inherit multiple parent interfaces, but interfaces only inherit interfaces and cannot inherit classes.
A class can implement multiple interfaces, if a class implements multiple interfaces, then the object of this class can be directly assigned to more than one parent class variable, which is Java provides the simulation of multiple inheritance.

because the interface defines a specification, the interface cannot contain constructors and initialization block definitions. The interface can contain member variables (only static constants), methods (only abstract instance methods, class methods (1.8), or Default methods (1.8)), and internal classes (including internal interfaces, enumerations) are defined.
The system automatically adds the public static final modifier to the member variables in the interface.
So the definition of int max = 50 in the interface, and the public static final int max = 50, is the same meaning.

7. Inner class

java8--Object-oriented (crazy Java handout 3) Review notes

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.