Summary of small knowledge points in Java

Source: Internet
Author: User

I recently reviewed the basics of Java and encountered some relatively biased questions. I would like to summarize some important knowledge points! However, when using IDE programming, these problems will be immediately identified by IDE, and compilation is not acceptable. I propose it here as a contribution to beginners.

I. declaring some features and rules of local variables
(1) declared local variables are not initialized by default, and member variables are initialized by default. For example:

Copy codeThe Code is as follows:
Class Demo {
Public static void main (String [] args ){
String s;
System. out. println (s );
}
}


In the above sample code, the direct output of s is incorrect, and the compilation is different because s is declared in the main method. In this case, s is a local variable and will not be initialized by default, therefore, there is an error in direct output below. Rewrite the Code as follows:

Copy codeThe Code is as follows:
Class Demo {
String s;
Public static void main (String [] args ){
System. out. println (s );
}
}


In this case, the output is null. Because s is declared as a member variable, the default value is null.
(2) You cannot have any access modifiers (public, protected, and private) before declaring local variables. They can only be friendly by default, but can use final to modify local variables.

Ii. Differences between modifying local variables of the basic type and referencing local variables using final
(1) When final modifies a local variable of the basic type, the data value of the basic type cannot be modified. Because final modified variables are final and cannot be changed. The following code:

Copy codeThe Code is as follows:
Public class Something {
Public int addOne (final int x ){
Return ++ x;
}
}


Here, the addOne method uses final int x as the parameter, and it is wrong to perform auto-increment next to it.

(2) When final modifies a local variable of the reference type, it is also an object. In this case, you can modify the attribute information of an object, but cannot modify the object reference. For example, the following code:

Copy codeThe Code is as follows:
Public class Something {
Public static void main (String [] args ){
Other o = new Other ();
New Something (). addOne (o );
}
Public void addOne (final Other o ){
O. I ++;
}
}
Class Other {
Public int I;
}


Here, the parameters received by the addOne method are an object, and the property I value of the object is added in the method body. The reference address of the object is not modified, so no error occurs. If you enter o = new Other () in the method body, an error is returned.

(3) The member variables declared by final modification in the class will not be initialized by default. Therefore, a specific value must be given before the constructor. For example, the following code:

Copy codeThe Code is as follows:
Class Something {
Final int I; // This line has already reported an error. The compilation fails and there is no initialization value.
Public void doSomething (){
System. out. println ("I =" + I );
}
}


Final int I is a final instant variable (instance variable, or member variable ). The instant variable of final has no default value and must be assigned a clear value before the constructor ends. It can be changed to "final int I = 0 ;".

 

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.