[J2SE]) Ten blind spots in Java basic knowledge

Source: Internet
Author: User
1. The function of the numeric type suffix (for example, the double type uses d and the long type uses l). For this, let's first look at the definition of a variable:
Int I = 2200000000; of course, this will not be compiled because the number has exceeded the int type range. Naturally, I changed int to long. Do you think the modification can be compiled? In fact, it still does not work. Not only can I of the int type be assigned a number that exceeds the int range, but 2200000000 cannot exist in this way, because it must tell the compiler: I am a long number. The variable definition is valid only when l is added after 2200000000;
2. What are the functions of the modifier of the class? This is relatively simple. However, if private can be used as the modifier of the class, what should I do? Will you hesitate? The modifier that can be used as a class has only a few: final, abstract, public, and default modifier;
3. is the default modifier (that is, public, protected, or private is not used) in the accessible modifier of method and field higher or lower than protected? I have never been very clear about this issue. The correct answer is high. The default accessible modifier makes the method and field have package-level accessibility. classes other than package cannot access these methods and fields, but protected is not, if it is a subclass of this class, you can access the method and field modified by protected, but cannot access the method and field modified by default modifier;
4. Can constructors use final or abstract modifiers? The answer is no. The constructor can only use accessible modifier;
5. the constructor cannot return values, that is, it cannot add a modifier of the return value before its function name. If a function with a returned value with the same name as the class is defined, will it generate a compilation error? For this question, see the following code:

Class Test {
Public void Test (){
}
}

Obviously, I won't write it like this, but does this code produce compilation errors? The result is no. This is only a method in the Test class that returns void. There will be no prompts when compiling with javac. However, using jikes will give a warning to tell programmer: class Test contains a function with the same name as the constructor. It can be seen from this that jikes as a Java compiler has a more professional side than javac;
6. Can the synchronized keyword be used before the field of the class? The answer is no. Synchronized can only be used in methods and define synchronization blocks;
7. When a parent class explicitly defines a non-default constructor (that is, a constructor with parameters), do subclasses need to define their own constructor? The answer is yes. Let's take a look at the following code:

Class SuperClass {
SuperClass (int I ){}
}
Class SubClass extends SuperClass {
}

In SubClass, We must explicitly define a constructor, and then call the constructor of the parent class in the constructor. Otherwise, the constructor cannot be compiled, the compilation error is that the parent class does not define the default constructor. From the compilation error prompt, we can find another solution, that is, define another default constructor in SuperClass, sub-classes do not need to define a constructor to call the constructor of the parent class, and then do nothing. Therefore, I think we should develop a habit of defining non-default constructor. Don't forget to define the default constructor again, this facilitates the extension subclass;
8. Is the field defined in the interface static? Is it final? The answer is that the field in the interface is static and final. Fields defined in the interface must be initialized and can be accessed through intefaceName. fieldName;
9. This question is about abstract. Let's take a look at the following code:

Abstract class Foo {
Void test ();
}

Is there a problem with this writing? At first, I thought there was no problem, but in fact there was a compilation error. The reason is very simple. Because the test method is not abstract, you need to add the method body sign -- {}; or if a method does not have a method body, you must declare that it is abstract.
10. The last question is about instanceof. Let's take a look at the following code:

Class {}
Class B extends {}
Class C extends B {}
Class Test {
Public static void main (String [] args ){
C c = new C ();
System. out. println (c instanceof );
}
}

Will print be true or false? The answer is simple. It is true. However, I always thought it was false, because c is only a class C instance, but I forgot that every time I use the new keyword to create an instance, the constructor of the parent class will be called one by one, so the instanceof returns true here.

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.