SCJP review notes (1)

Source: Internet
Author: User

 

Example 1:

1. final int a = 1;

2. final int B;

3. B = 5;

4. int x = 0;

5. switch (x)

6 .{

7. case:

8. case B: // Exception

9 .}

In a Switch (condition), the condition can only be of the byte, short, int, and enum (version 1.5) types.

Example 2:

1. switch (x)

2 .{

3. case 1:

4 .{

5. System. out. print ("123 ");

6. break;

7 .}

8. case 2:

9 .{

10. System. out. print ("456 ");

11. break;

12 .}

13 .}

Example 3:

For tag example

1. boolean isTrue = true;

2. foo:

3. For (INT I = 0; I <5; I ++)

4 .{

5. While (istrue)

6 .{

7. system. Out. Print ("123 ");

8. Break Foo;

9 .}

10 .}

11. system. Out. Print ("456 ");

Example 4:

1. byte B = 2;

2. Switch (B)

3 .{

4. Case 22:

5. system. Out. Print ("123 ");

6. Break;

7. Case 128: // exception exceeds the byte range

8. system. Out. Print ("456 ");

9. Break;

10 .}

Example 5:

In try ...... Catch ...... In finally statements, statements in finally are not necessarily run. For example, they are not run in system. Exit (1); statements. For details, see the following:

 public class Beat { public static void main(String[] args){ try { System.out.print("1"); System.exit(1); }catch(Exception e) {System.out.print("2"); }finally {System.out.print("3"); }  }}

 

If the first sentence does not exist, it will run in rows 5th and 9.

Example 6:

General exceptions should be placed behind specific exceptions. If this is not the case, exceptions will occur and compilation is not allowed.

Example 7:

If the internal class defined in the method needs to access the variables in the method, the final must be added before the variable, but the final does not indicate that it is a constant, but a simple mark.

Class A {private int a = 1; public void fun (final int log) {// No final will be wrong and cannot be compiled. class B {public void print () {System. out. println ("a =" + a + ", log =" + log) ;}} new B (). print () ;}} public class Text0 {public static void main (String args []) {new (). fun (10 );}}

 

Example 8:

Class Outer {private String info = "wyp --> 397090770"; class Inner {public void print () {System. out. print ("INFO =" + info) ;}} public class Text0 {public static void main (String wyp []) {Outer o = new Outer (); Outer. inner in = o. new Inner (); // pay attention to calling Internal classes in. print ();}}

 

Example 9:

An abstract class can implement a general method, which can have constructor or constructor with parameters. However, when no constructor without parameters exists, you cannot use constructors with parameters.

Example 10:

In if (condition )...... In else, while (condition) {} and other statements, their condition is a boolean type, and there will be Type mismatch during runtime (int type as an example: cannot be converted from int to boolean, for example:

Public class Beat {public void test (String str) {int check = 4; if (check = str. length () {// An error occurred while compiling the System. out. print (str. charAt (check-= 1) + "");} else {System. out. print (str. charAt (0) + "") ;}} public static void main (String [] args) {Beat c = new Beat (); c. test ("four"); c. test ("tee"); c. test ("");}}

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.