Contents - Cultural View
Contents - Cultural View Contents - Cultural View
Java syntax 184 int calculateSum(int a, int b) { } int result = a + b; return result; return statement ends execution immediately, except for one case: if the statement is encountered within a try block and it is complemented by a finally, control is passed to the finally block. void doSomething(boolean streamClosed) { } try { if (streamClosed) { } return; readFromStream(); } finally { } /* Will be called last even if readFromStream() was not called */ freeResources(); Exception handling statements try ... catch ... finally statements Exceptions are managed within try ... catch blocks. try { // Statements which may throw exceptions methodThrowingExceptions(); } catch (Exception ex) { // Exception caught and handled here reportException(ex); } finally { } // Statements always executed after the try/catch blocks freeResources(); The statements within the try block are executed, and if any of them throws an exception, execution of the block is discontinued and the exception is handled by the catch block. There may be multiple catch blocks, in which case the first block with an exception variable whose type matches the type of the thrown exception is executed. If no catch block matches the type of the thrown exception, the execution of the outer block (or method) containing the try ... catch statement is discontinued, and the exception is passed up and outside the containing block (or method). The exception is propagated upwards through the call stack until a matching catch block is found within one of the currently active methods. If the exception propagates all the way up to the top-most main method without a matching catch block being found, a textual description of the exception is written to the standard output stream. The statements within the finally block are always executed after the try and catch blocks, whether or not an exception was thrown and even if a return statement was reached. Such blocks are useful for providing clean-up code that is guaranteed to always be executed.
Java syntax 185 The catch and finally blocks are optional, but at least one or the other must be present following the try block. throw statement throw statement is used to throw an exception and end method execution. Thrown exception instance is written after the throw statement. void methodThrowingExceptions(Object obj) { } if (obj == null) { } // Throws exception of NullPointerException type throw new NullPointerException(); // Will not be called if obj was null doSomethingWithObject(obj); Thread concurrency control Unlike C++, Java has built-in tools for multi-thread programming. For the purposes of thread synchronization the synchronized statement is included in Java language. To make a code block synchronized, it is preceded by the synchronized keyword followed by the lock object inside the brackets. When the executing thread reaches the synchronized block, it acquires a mutual exclusion lock, executes the block, then releases the lock. No threads may enter this block until the lock is released. Any non-null reference type may be used as the lock. /* Acquires lock on someObject. It must be of a reference type and must be non-null */ synchronized(someObject) { } // Synchronized statements assert statement assert statements have been available since J2SE 1.4. This type of statements is used to make assertions in the source code, which can be turned on and off during execution for specific classes or packages. To declare an assertion the assert keyword is used followed by a conditional expression. If it evaluates to false during when the statement is executed, an exception is thrown. This statement may include a colon followed by another expression, which will act as the exception's detail message. // If n equals 0, AssertionError is thrown assert n != 0; /* If n equals 0, AssertionError will be thrown with the message after the colon */ assert n != 0 : "n was equal to zero";
- 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 and 186: Java syntax 181 switch statement Sw
- Page 187: Java syntax 183 outer: for (int i =
- 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
- Page 237 and 238: New I/O 233 FileChannel can be used
Java syntax 184<br />
int calculateSum(int a, int b) {<br />
}<br />
int result = a + b;<br />
return result;<br />
return statement ends execution immediately, except for one case: if the statement is encountered within a try block<br />
and it is complemented by a finally, control is passed to the finally block.<br />
void doSomething(boolean streamClosed) {<br />
}<br />
try {<br />
if (streamClosed) {<br />
}<br />
return;<br />
readFromStream();<br />
} finally {<br />
}<br />
/* Will be called last even if<br />
readFromStream() was not called */<br />
freeResources();<br />
Exception handling statements<br />
try ... catch ... finally statements<br />
Exceptions are managed within try ... catch blocks.<br />
try {<br />
// Statements which may throw exceptions<br />
methodThrowingExceptions();<br />
} catch (Exception ex) {<br />
// Exception caught and handled here<br />
reportException(ex);<br />
} finally {<br />
}<br />
// Statements always executed after the try/catch blocks<br />
freeResources();<br />
The statements within the try block are executed, and if any of them throws an exception, execution of the block is<br />
discontinued and the exception is handled by the catch block. There may be multiple catch blocks, in which case the<br />
first block with an exception variable whose type matches the type of the thrown exception is executed.<br />
If no catch block matches the type of the thrown exception, the execution of the outer block (or method) containing<br />
the try ... catch statement is discontinued, and the exception is passed up and outside the containing block (or<br />
method). The exception is propagated upwards through the call stack until a matching catch block is found within<br />
one of the currently active methods. If the exception propagates all the way up to the top-most main method without<br />
a matching catch block being found, a textual description of the exception is written to the standard output stream.<br />
The statements within the finally block are always executed after the try and catch blocks, whether or not an<br />
exception was thrown and even if a return statement was reached. Such blocks are useful for providing clean-up code<br />
that is guaranteed to always be executed.