[SCJP] Test on difficult knowledge points [in eclipse]

Source: Internet
Author: User

1. Visibility Analysis of local varibles: [2006.7.5]

/**
* @ Author Zhu Xiang
* @ Creation date:
* @ Organization: Three Gorges University
* @ Email: zhuxiang_33@163.com
* @ QQ: 75722549.
*/

Package cn. test. certification;

Public class VaribleScope
{
Public static void main (String args [])
{
Char digit = 'a ';
// (1)
For (int I = 0; I <10; I ++)
{
// (2)
Switch (digit)
{
// (3)
Case 'X ':
{
Int j = 0; //
System. out. println (j );
}
// (4)
Default:
{
Int J = 1; // B
System. Out. println (j );
}
Int J = 2; // C
}
Int J = 3; // d
}
Int j = 4; // e
}

This program is compiled successfully. Perform the following tests:

(1) cut a line at e to the position (1. Result: errors are reported at a, B, c, and d (repeated definition by j ).

(2) cut at d to (2. Result: an error is reported at a, B, and c (repeated definition of j ).

(3) Cut the part c to the position (3. Result: an error is returned at a and B (repeated definition by j ).

(4) Cut the part c to the position (4. Result: an error is returned at location B (defined repeatedly by j ).

Conclusion: local varibles with the same name can only see the outside of the block, but cannot see the inside of the block. The direction is always outward (or even horizontally ). If you see it, it is regarded as a duplicate. Therefore, you can usually put local varibles of the same name behind the block instead of the front. These blocks are such as {}, if {}, while {}, try {}and so on.

2. overriding and modiffer

/**
* @ Author Zhu Xiang
* @ Creation date:
* @ Organization: Three Gorges University
* @ Email: zhuxiang_33@163.com
* @ QQ: 75722549.
*/

Package cn. test. certification;

Public class OverridAndModifier {

}
Abstract class Parent {
Static void a1 (){}
Void a2 (){}
Final void b1 (){}
Void b2 (){}
Native void c1 ();
Void c2 (){}
Synchronized void d1 (){}
Void d2 (){}
Abstract void e1 ();
Void e2 (){}
}
Abstract class Clild extends Parent {
Static void a1 () {}// static --> static (OK)
Void a1 () {}// static --> nonstatic (error)
Static void a2 () {}// nonstatic --> static (error)
Void b1 () {}// final --> nonfinal (error)
Final void b1 () {}// final --> final (error)
Final void b2 () {}// nonfinal --> final (OK)
Void c1 () {}// native --> nonnative (OK)
Native void c1 (); // native --> native (OK)
Native void c2 (); // nonnative --> native (OK)
Void d1 () {}// syschronized --> nonsynchronized (OK)
Synchronized void d1 () {}// synchonized --> synchoinzed (OK)
Synchronized void d2 () {}// nonsynchronized --> synchonized (OK)
Void e () {}// OK
Abstract void e2 () {}// error

}

Conclusion:
1. static:
Static --> static (OK), static --> nonstatic (error), nonstatic --> static (error)
2. final
Final, nonfinal --> nonfinal (error), nonfinal --> final, nonfinal (OK)
3. syschronized
Syschronized --> nonsynchronized (OK), nonsynchronized --> synchonized (OK), synchonized --> synchoinzed (OK)
4. native
Native --> nonnative (OK), native --> native (OK), nonnative --> native (OK)
5. abstract
The abstract method does not have overrid, but is only implemented.
Note: All non --> non are OK

3. combination rules of method mofifier:

/**
* @ Author Zhu Xiang
* @ Creation date:
* @ Organization: Three Gorges University
* @ Email: zhuxiang_33@163.com
* @ QQ: 75722549.
*/
Package cn. test. certification;

Abstract public class ModifierConbinationRules {
Final native void a (); // final + native (OK)
Final abstract void B (); // final + abstract (error)
Final static void c () {}// final + static (OK)
Native abstract void e (); // native + abstract (error)
Native static void f (); // native + static (OK)
Native synchronized void g (); // native + synchronized (OK)
Abstract static void h (); // abstract + static (error)
Abstract synchronized void I (); // abstract + synchronized (error)
Static synchronized void j () {}// static + synchronize (OK)
// Conclusion: abstract cannot be combined with static, final, native, and synchronized. Other four types can be freely combined.

Public final void k () {}// OK
Protected final void l () {}// OK
Private final void m () {}// OK

Public native void n (); // OK
Protected native void o (); // OK
Private native void p (); // OK

Public abstract void q (); // OK
Protected abstract void r (); // OK
Private abstract void s (); // error

Public static void t () {}// OK
Protected static void u () {}// OK
Private static void v () {}// OK

Public synchronized void w () {}// OK
Protected synchronized void x () {}// OK
Private synchronized void y () {}// OK
// Conclusion: private abstract is not applicable only in other cases.

}

Conclusion: abstract cannot be combined with static, final, native, and synchronized. The other four types can be freely combined. In addition, in combination with the three accses modifier, there is only one case of private + abstract error, and other cases are acceptable. In short, abstract is a very special guy who likes to be independent, and the other four are very friendly.


4. essence of casting

Program snippets: (assume that Cat extends Dog)

Dog dog;
Cat cat1 = new Cat ();
Cat cat2 = new Cat ();
Dog = cat1;
Cat2 = (Cat) dog; // OK
// Error cast:
Dog dog1 = new Dog ();
Cat cat3;
Cat3 = (Cat) dog1; // ClassCastException

Conclusion: All cast operations are actually performed between referrence, and the object to which it is directed has never changed. /If the object cannot be cast, the two references cannot be cast.

5. Two-dimensional array problems:

Program snippets:

// Two-dimensional array Test
Int [] [] a = new int [4] [];
System. out. println (a); // print out "[[I @ 1a125f0"
System. out. println (a [1]); // print out "null"
System. Out. println (A [1] [1]); // runtimeexception
A [1] = new int [2];
A [3] = new int [5];
System. Out. println (a); // print out "[[I @ 1a125f0"
System. Out. println (A [1]); // print out "[I @ c1cd1f"
System. Out. println (A [1] [1]); // print out "0"
System. Out. println (A [1]. Length );
// System. Out. println (A [2]. Length); // nullpointexception
System. Out. println (A [3]. Length );
System. Out. println (A. Length );

// One-dimensional array Test
Int [] a1 = new int [2];
System. out. println (a1); // print out "[I @ 181afa3"
System. out. println (a1 [1]); // print out "0"
Int [] a2;
System. out. println (a2); // No Initialization

Conclusion:

1. Definition initialization:
Int [] array [] = new int [2] [2]; its initialization can only initialize the left-side size, and the right-side size is used to determine whether to initialize. For example, int [] [] a = new int [10] []. the right side can be initialized separately and the length can be different, for example, array [1] = new int [1], array [0] = new int [2]. all elements are automatically initialized to 0, regardless of the class or method (this is a feature of the array ).
2. print
Println (array): garbled. Prinlt (array [0]): Only the size on the left is initialized to null, and garbled characters are obtained when both sizes are initialized. Println (array [0] [0]): it is defined as 0, and runtimeion is not defined.
(Why is it garbled)

6. Bind Base = new Subclass () and inherit Subclass sub = new Subclass () [2006.7.6]

Package cn. test. certification;
/**
* @ Author Zhu Xiang
* @ Creation date:
* @ Organization: Three Gorges University
* @ Email: zhuxiang_33@163.com
* @ QQ: 75722549.
*/

Public class LastBinding {
Public static void main (String [] args ){
// Bind later
Base B = new Subclass ();
System. out. println (B. x );
System. out. println (B. method ());
B. methodY ();
// Normal parent class
Base b1 = new Base ();
System. out. println (b1.x );
System. out. println (b1.method ());
// Inherit normal subclass
Subclass b2 = new Subclass ();
System. out. println ("sub x is Itself" + b2.x );
System. out. println ("sub z is inherid from super, becase" +
"It doesn't have Z" + b2.methodz (); // you can access the Z of the parent class (via the parent class method methodz ())

// Conclusion: the only difference between the back-help and normal sub-classes is that the post-binding cannot access the sub-class member varibles.
//, While normal sub-classes first access their own member varibles, and then access the parent class member varibles
// Error Casting
// Subclass B3 = (subclass) new base ();
}

}

Class base {
Int x = 1;
Private int y = 2;
Private int z = 5;
Int methodz (){
Return Z;
}
Int method (){
Return x;
}
Void methodY (){
System. out. println ("y is:" + y );
}
}

Class Subclass extends Base {
Int x = 3;
// Private int z = 1;

Int method (){
Return x;
}
Int methodA (){
Return z; // cannot access z of the parent class
}

}

Conclusion:

1. member varibles
After binding, you cannot access the Child class member varibles. You can only access the parent class. Inherit to access the Child class member varibles first, check that the Child class is not accessed and then access the parent class.
2. method
The two are the same:
The method called is a dynamic process: first, check whether the subclass has the overriding method. If so, call the subclass method, just like operating the subclass object, private Members of the parent class cannot be accessed in this method. If no overriding is found, call the parent class method. In this case, like operating the parent class object, you can access the Private Members of the parent class in this method.

7. private visibility

/**
* @ Author Zhu Xiang
* @ Creation date:
* @ Organization: Three Gorges University
* @ Email: zhuxiang_33@163.com
* @ QQ: 75722549.
*/
Package cn. test. certification;

Public class PrivateAcsses {
Public static void main (String args []) {
// New OneClass (). x; // x is not visible here, even for Aclass objects
}

}
Class OneClass {
Private int x;

Void method (){
New OneClass (). x = 10; // x is visible here because it is included inside the OneClass class
This. x = 10; // The second type.
X = 10; // The third type
}
}

Conclusion: It can be accessed in any way within the class (except for the static method, new is used), and direct access (including instances of this class) is not allowed anywhere except the class ), must be accessed through methods.

8. Usage of constructor [2006.7.8]

/**
* @ Author Zhu Xiang
* @ Creation date:
* @ Organization: Three Gorges University
* @ Email: zhuxiang_33@163.com
* @ QQ: 75722549.
*/
Package cn. test. certificationOther;

Public class DefaultConstructor {
Public static void main (String args []) {
Child child = new EveryClass (1 );
}

}

Class Parent {
// This can be seen as automatic addition:
// Public Parent (){
// Super ();
//}
}

Class Child extends Parent {
Public Child (){
// This can be seen as automatic addition:
// Super ();
System. out. println ("Child ");
}

Public Child (int I ){
Return; // return may exist, but it must be null.
}}
Class AnyClass extends Child {
Int x = 0;

// Private AnyClass () {// It can be private, but the subclass is not visible to the subclass.
AnyClass () {// unless no constructor is defined, it must be explicitly defined, or an error will occur.
System. out. println ("after Child execute ");

}

AnyClass (int w ){
X = w;
}
}

Class EveryClass extends AnyClass {
Int x = 0;

EveryClass (int w) // The parent class is constructed as pravate and is not visible.
{
// This can be seen as automatic addition:
// Super ();
X = w + 1;
System. out. println ("after AnyClass execute ");
}
// AnyClass (int w) {// cannot overriding the parent class constructor. The system treats it as a common method.

//}
}

Conclusion:

1. If no constructor is defined in a Someclass, it can be regarded as automatically added: SomeClass () {super ();}
2. If a constructor does not contain super or this, it can be regarded as automatically adding: super () in the first line ();
3. the constructor corresponding to all super called by the subclass must be defined in the parent class (including the above two can be considered as automatically added ).
4. It can be private, but cannot be inherited or accessed by other classes. The modifier can only be public or private.
5. retrun may exist, but the returned result must be null, for example, return;
6. constructor cannot be overriding or inherited in the general sense. It can only use super to call the parent class constructor.
7. Call sequence: subclass --> parent class, execution sequence: parent class --> subclass;

9. catch question: [2006.7.9]

/**
* @ Author Zhu Xiang
* @ Creation date:
* @ Organization: Three Gorges University
* @ Email: zhuxiang_33@163.com
* @ QQ: 75722549.
*/
Package cn. test. certification;
Import java. io. IOException;

Public class ExcepionTest {
Static void method () throws IOException, ClassNotFoundException {
// Throw new Exception (); // throw an issue that fails to be reached by throws. You can skip this line.

}
Public static void main (String args []) {
Try {
Method (); // two catch s after removing this line will be wrong
}
/* Catch (Exception e ){

}
Catch (IOException e) {// if the order is incorrect, an error is returned.

}
*/
Catch (IOException e ){
}
Catch (ClassNotFoundException e) {// if this is removed, the exception of thows cannot be fully caught. An error is returned.

}
}

}

Conclusion:

1. catch the future ion that a try will never throw. An error will be reported during compilation.
2. In any case, the exception must be a subclass --> parent class from top to bottom for multiple catch;
3. If throw fails to reach a replica ion, an error is reported. That is to say, the replica ion of throws must be the parent class or the same class of the replica ion of throw, and cannot be a subclass or irrelevant class.
4. Complete catch is required for all throws; otherwise, an error is returned.
5. A throw exception may not be thrown in the method. However, if throws has a replica ion, it must include this method in try. Otherwise, 1st errors will occur. Since throws replica ion must be caught, conflicts that cannot be solved.

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.