Monday, September 6, 2010

Java, lesser-known small ideas


1. Nonsense perhaps you have already heard disputes goto statement, goto know that in Java, is still a reserved keyword, but are not allowed. Let's look at the classic use goto:

1: statement1; 
2: statement_label_for_section2: 
3: statement2; 
4: goto statement_label_for_section2;

You can see 2: statement_label_for_section2: a code fragment (Section) of the opening tag.

Java language does not allow use of goto, but you can use as 2: statement_label_for_section2: the same code fragment labeled as break / continue the jump target.


See documents related standards: <> 14.7 Labeled Statements

2. Application of nested loops, I sometimes need to jump from outside the inner loop in the loop. Labeled Statements can be used to facilitate the achievement.

1: for (int I1 = 0; I1 <10; I1 + +) ( 
2: loop1: 
3: for (int I2 = 0; I2 <20; I1 + +) ( 
4: break loop1; 
5:) 
6:)

continue to use and the use of break is the same.

Java language is not widely used in many small ideas. See: <>

3.Blocks 
<> 14.2 Blocks

Perhaps you are a slacker like me. Among the special code in the Test Case, I often need to define a lot of the same Method object, these objects are similar in major operations, you should know what I was thinking: Copy & Past. And for each object from a different name can be a pain to do. At the same time these objects are often only short-lived Ghost.

1: import java.util.ArrayList; 
2: import java.util.Collection; 
3: 
4: public class T2 ( 
5: 
6: public static void main (String [] args) ( 
7: ( 
8: Collection c = new ArrayList (); 
9: c.add ("1"); 
10: c.clear (); 
11:) 
12: ( 
13: Collection c = new ArrayList (); 
14: c.add ("2"); 
15: c.clear (); 
16:) 
17:) 
18: 
19:)

No comments:

Post a Comment