Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Java syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Conditional statements=== ====<code>if</code> statement==== {{Main|Conditional (computer programming)#C-like languages}} [[Conditional (computer programming)|if statements]] in Java are similar to those in C and use the same syntax: <syntaxhighlight lang="java"> if (i == 3) { doSomething(); } </syntaxhighlight> <code>if</code> statement may include optional <code>else</code> block, in which case it becomes an if-then-else statement: <syntaxhighlight lang="java"> if (i == 3) { doSomething(); } else { doSomethingElse(); } </syntaxhighlight> Like C, else-if construction does not involve any special keywords, it is formed as a sequence of separate if-then-else statements: <syntaxhighlight lang="java"> if (i == 3) { doSomething(); } else if (i == 2) { doSomethingElse(); } else { doSomethingDifferent(); } </syntaxhighlight> Also, a [[?:]] operator can be used in place of simple if statement, for example <syntaxhighlight lang="java"> int a = 1; int b = 2; int minVal = (a < b) ? a : b; </syntaxhighlight> ====<code>switch</code> statement==== [[Switch statement]]s in Java can use <code>byte</code>, <code>short</code>, <code>char</code>, and <code>int</code> (not <code>long</code>) primitive data types or their corresponding wrapper types. Starting with J2SE 5.0, it is possible to use [[Enumerated type|enum types]]. Starting with Java SE 7, it is possible to use Strings.<ref>{{Cite web|title=The switch Statement (The Javaβ’ Tutorials > Learning the Java Language > Language Basics)|url=https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html|access-date=August 15, 2021|website=docs.oracle.com|archive-date=March 15, 2010|archive-url=https://web.archive.org/web/20100315060844/http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html|url-status=live}}</ref> Other [[reference type]]s cannot be used in <code>switch</code> statements. Possible values are listed using <code>case</code> labels. These labels in Java may contain only constants (including enum constants and string constants). Execution will start after the label corresponding to the expression inside the brackets. An optional <code>default</code> label may be present to declare that the code following it will be executed if none of the case labels correspond to the expression. Code for each label ends with the <code>break</code> keyword. It is possible to omit it causing the execution to proceed to the next label, however, a warning will usually be reported during compilation. <syntaxhighlight lang="java"> switch (ch) { case 'A': doSomething(); // Triggered if ch == 'A' break; case 'B': case 'C': doSomethingElse(); // Triggered if ch == 'B' or ch == 'C' break; default: doSomethingDifferent(); // Triggered in any other case break; } </syntaxhighlight> =====<code>switch</code> expressions===== Since Java 14 it has become possible to use switch expressions, which use the new arrow syntax: <syntaxhighlight lang="java"> var result = switch (ch) { case 'A' -> Result.GREAT; case 'B', 'C' -> Result.FINE; default -> throw new ThisIsNoGoodException(); }; </syntaxhighlight> Alternatively, there is a possibility to express the same with the <code>yield</code> statement, although it is recommended to prefer the arrow syntax because it avoids the problem of accidental fall throughs. <syntaxhighlight lang="java"> var result = switch (ch) { case 'A': yield Result.GREAT; case 'B': case 'C': yield Result.FINE; default: throw new ThisIsNoGoodException(); }; </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)