Six big questions that Java beginners must understand <3>

Source: Internet
Author: User

Six big questions that Java beginners must understand

For the problems in this series, everyone who learns Java should understand. Of course, it doesn't matter if you just learn to play in Java. If you think you have surpassed the beginner, but do not understand these problems, please return yourself to the ranks of beginners.

Question four: What exactly does the final keyword modify?

Shenyang 463 Plastic Surgery Hospital Program final makes the modified variable "unchanged", but because the nature of the object type variable is "reference", so that "invariant" also has two meanings: the reference itself is unchanged, and the reference point to the object is unchanged.

The reference itself does not change:

Final StringBuffer a=new StringBuffer ("immutable");

Final StringBuffer b=new StringBuffer ("not immutable");

a=b;//Compile-time error

The object that the reference points to is the same:

Final StringBuffer a=new StringBuffer ("immutable");

A.append ("broken!" );//Compile Through

As you can see, final is valid only for the referenced "value" (the memory address of the object it points to), forcing the reference to point only to the object that was initially pointed at, and changing its point to cause a compile-time error. Final is not responsible for the change in the object it points to. This is similar to the = = Operator: the = = operator is only responsible for the reference "value" equality, as to whether the address points to the object content is equal, the = = operator is no matter.

Understanding the final question has a very important meaning. Many program vulnerabilities are based on this----final can only guarantee that a reference will always point to a fixed object, and that the state of that object is not guaranteed to be constant. In multi-threaded operations, an object is shared or modified by multiple threads, and a thread's unconscious modification of an object can cause another thread that uses this object to crash. One of the wrong solutions is to declare this object as final when it is newly created, with the intention of making it "never change". In fact, it was futile.

Question five: Exactly how to initialize!

This question discusses the initialization of variables, so first look at what kinds of variables are in Java.

1. Properties of the class, or domain name

2. Local variables in the method

3. Parameters of the method

For the first variable, the Java virtual opportunity is automatically initialized. If the initial value is given, it is initialized to that initial value. If not given, it is initialized to the default initial value of the type variable.

int type variable default initial value is 0

Float type variable default initial value is 0.0f

The default initial value for a double type variable is 0.0

Boolean type variable default initial value is False

Char type variable default initial value is 0 (ASCII code)

The default initial value of the long type variable is 0

The default initial value of all object reference type variables is null, that is, no object is pointed to. Note that the array itself is also an object, so an array reference that is not initialized is also null after auto-initialization.

For two different class properties, the static property is different from the instance property when it is initialized. The instance property is initialized when the instance is created, and the static property is initialized when the class is loaded, that is, when the class is first used, and is not initialized again for subsequent instance creation. This issue will be discussed in detail in a later series. Shenyang 463 Plastic Surgery Hospital http://www.hengnaya.com/

For the second variable, it must be explicitly initialized. If you try to use it before it is initialized, the compiler will protest. If the initialized statement is in a try block or an if block, it must be assigned before the first use. That is, the compiler will also protest if the initialization statement is placed in a conditional judgment statement with only the If block, because execution may not conform to the judgment condition after the IF, so that the initialization statement is not executed, which violates the requirement that the local variable must be initialized before it is used. However, if there are initialization statements in the else block, it can be compiled, because at least one initialization statement will be executed in any case, and nothing that has not been initialized before use will occur. The same is true for Try-catch, if the initialization statement is only in the try block, the compilation department passes. If it is also in catch or finally, it can be compiled. In conclusion, it is important to ensure that local variables are initialized before they are used. So, a good practice is to initialize them when declaring them, and if you don't know what the value is, use the default value above!

In fact, the third variable is essentially the same as the second, and it is a local variable in the method. Just as a parameter, it must have been initialized, the value passed in is the initial value, so it does not need to be initialized.

Question six: What is instanceof?

Instanceof is a Java two-dollar operator, and ==,>, < is the same kind of stuff. Because it is made up of letters, it is also a reserved keyword for java. Its purpose is to test whether the object on its left is an instance of the class to the right of it, returning a Boolean type of data. As an example:

String s = "I AM an object!";

Boolean IsObject = s instanceof Object;

We declare a String object reference, point to a String object, and then use INSTANCOF to test whether the object it points to is an instance of the objects class, which, obviously, is true, so return true, that is, the value of IsObject is true.

Instanceof has some use. For example, we wrote a system that handles bills, and there are three of these:

public class Bill {//omit details}

public class PhoneBill extends Bill {//omit details}

public class Gasbill extends Bill {//omit details}

There is a method in the handler that accepts an object of type bill and calculates the amount. Suppose the two billing methods are different, and the incoming Bill object may be any of the two, so use instanceof to determine:

Public double Calculate (Bill Bill) {

if (Bill instanceof PhoneBill) {

Calculate phone bills

}

if (Bill instanceof Gasbill) {

Calculate gas bills

}

...

}

This allows the two seed class to be processed in one way. However, this practice is often thought of as not taking good advantage of the polymorphism in object-oriented. In fact, the above function requires a method overload can be fully implemented, this is an object-oriented approach should be done to avoid back to the structured programming mode. As long as the two names and return values are the same, a different method of accepting the parameter types is available:

Public double Calculate (PhoneBill bill) {

Calculate phone bills

}

Public double Calculate (Gasbill bill) {

Calculate gas bills

}

Six big questions that Java beginners must understand <3>

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.