Contents - Cultural View
Contents - Cultural View Contents - Cultural View
Java syntax 182 for loop for loops in Java include an initializer, a condition and a counter expression. It is possible to include several expressions of the same kind using comma as delimiter. However, unlike C, comma is just a delimiter and not an operator. for (int i = 0; i < 10; i++) { } doSomething(); // A more complex loop using two variables for (int i = 0, j = 9; i < 10; i++, j -= 3) { } doSomething(); Like C, all three expressions are optional, the following loop will be infinite: for (;;) { } doSomething(); Enhanced for loop Enhanced for loops have been available since J2SE 5.0. This type of loop uses built-in iterators over arrays and collections to return each item in the given collection. Every element will be returned and reachable in the context of the code block. When the block has been executed the next item will be returned until there are no items remaining. Unlike C# this kind of loop does not involve a special keyword but instead uses a different notation style. for (int i : intArray) { } doSomething(i); Jump statements Labels Labels are given points in code used by break and continue statements. Despite the presence of the goto keyword, it cannot be used to jump to specific points in the code. start: someMethod(); break statement The break statement breaks out of the closest loop or switch statement. Execution continues in the statement after the terminated statement, if any. for (int i = 0; i < 10; i++) { } while (true) { } break; // Will break to this point It is possible to break out of the outer loop using labels:
Java syntax 183 outer: for (int i = 0; i < 10; i++) { } while (true) { } break outer; // Will break to this point continue statement The continue statement discontinues the current iteration of the current control statement and begins the next iteration. The following while loop in the code above reads characters by calling getChar(), skipping the statements in the body of the loop if the characters are spaces: int ch; while (ch = getChar()) { } if (ch == ' ') continue; // Skips the rest of the while-loop // Rest of the while-loop, will not be reached if ch == ' ' doSomething(); Labels can be specified in continue statements, as they can in break statements: outer: for (String str : stringsArr) { } char[] strChars = str.toCharArray(); for (char ch : strChars) { } if (ch == ' ') { } return statement /* Continues the outer cycle and the next string is retrieved from stringsArr */ continue outer; doSomething(ch); The return statement is used to end method execution and to return a value. A value returned by the method is written after the return keyword. If the method returns anything but void, it must use the return statement to return some value. void doSomething(boolean streamClosed) { } // If streamClosed is true, execution is stopped if (streamClosed) { } return; readFromStream();
- Page 135 and 136: GlassFish Metro 131 Features Metro
- Page 137 and 138: James Gosling 133 Contributions He
- Page 139 and 140: Todd Greanier 135 Todd Greanier Tod
- Page 141 and 142: Head First (book series) 137 Head F
- Page 143 and 144: Inner class 139 • Local class - T
- Page 145 and 146: Interface (Java) 141 The body of th
- Page 147 and 148: Internet Foundation Classes 143 Int
- Page 149 and 150: Internet Foundation Classes 145 •
- Page 151 and 152: JAD (file format) 147 JAD (file for
- Page 153 and 154: JAMWiki 149 JAMWiki Developer(s) Ry
- Page 155 and 156: JGroups 151 JGroups Stable release
- Page 157 and 158: Java Class Library 153 Java Class L
- Page 159 and 160: Java Class Library 155 Alternative
- Page 161 and 162: Java Classloader 157 User-defined c
- Page 163 and 164: Java compiler 159 Java compiler A J
- Page 165 and 166: Java Development Kit 161 • jstack
- Page 167 and 168: Java Interface Definition Language
- Page 169 and 170: Java TV 165 See also • Xlet Exter
- Page 171 and 172: Java Work Framework 167 Database db
- Page 173 and 174: Java annotation 169 programmaticall
- Page 175 and 176: Java annotation 171 component.getAt
- Page 177 and 178: Java collections framework 173 Refe
- Page 179 and 180: Java syntax 175 Java syntax The syn
- Page 181 and 182: Java syntax 177 Double quote \" Tab
- Page 183 and 184: Java syntax 179 import statement Th
- Page 185: Java syntax 181 switch statement Sw
- Page 189 and 190: Java syntax 185 The catch and final
- Page 191 and 192: Java syntax 187 Reference types Ref
- Page 193 and 194: Java syntax 189 Foo.doSomething();
- Page 195 and 196: Java syntax 191 Modifiers • abstr
- Page 197 and 198: Java syntax 193 } public void walk(
- Page 199 and 200: Java syntax 195 Inheritance Interfa
- Page 201 and 202: Java syntax 197 /* Any Mapper insta
- Page 203 and 204: Java: View Technologies and Framewo
- Page 205 and 206: JavaBeans Activation Framework 201
- Page 207 and 208: javac 203 References [1] http:/ / j
- Page 209 and 210: JavaFX 205 profile includes Swing a
- Page 211 and 212: JavaFX 207 External links • Offic
- Page 213 and 214: JExamples 209 JExamples Industry Co
- Page 215 and 216: Jikes 211 Jikes Developer(s) IBM St
- Page 217 and 218: Michael Kölling 213 Microsoft Pate
- Page 219 and 220: List of Java keywords 215 case catc
- Page 221 and 222: List of Java keywords 217 long nati
- Page 223 and 224: List of Java keywords 219 Reserved
- Page 225 and 226: Name mangling 221 Name mangling In
- Page 227 and 228: Name mangling 223 } }; }; /* = _ZN9
- Page 229 and 230: Name mangling 225 if (__1cGstrcmp6F
- Page 231 and 232: Name mangling 227 ['_Test__private_
- Page 233 and 234: Name mangling 229 External links
- Page 235 and 236: Patrick Naughton 231 See also • H
Java syntax 182<br />
for loop<br />
for loops in Java include an initializer, a condition and a counter expression. It is possible to include several<br />
expressions of the same kind using comma as delimiter. However, unlike C, comma is just a delimiter and not an<br />
operator.<br />
for (int i = 0; i < 10; i++) {<br />
}<br />
doSomething();<br />
// A more complex loop using two variables<br />
for (int i = 0, j = 9; i < 10; i++, j -= 3) {<br />
}<br />
doSomething();<br />
Like C, all three expressions are optional, the following loop will be infinite:<br />
for (;;) {<br />
}<br />
doSomething();<br />
Enhanced for loop<br />
Enhanced for loops have been available since J2SE 5.0. This type of loop uses built-in iterators over arrays and<br />
collections to return each item in the given collection. Every element will be returned and reachable in the context of<br />
the code block. When the block has been executed the next item will be returned until there are no items remaining.<br />
Unlike C# this kind of loop does not involve a special keyword but instead uses a different notation style.<br />
for (int i : intArray) {<br />
}<br />
doSomething(i);<br />
Jump statements<br />
Labels<br />
Labels are given points in code used by break and continue statements. Despite the presence of the goto keyword, it<br />
cannot be used to jump to specific points in the code.<br />
start:<br />
someMethod();<br />
break statement<br />
The break statement breaks out of the closest loop or switch statement. Execution continues in the statement after the<br />
terminated statement, if any.<br />
for (int i = 0; i < 10; i++) {<br />
}<br />
while (true) {<br />
}<br />
break;<br />
// Will break to this point<br />
It is possible to break out of the outer loop using labels: