Java syntax: Difference between revisions

Content deleted Content added
No edit summary
Tags: Mobile edit Mobile app edit iOS app edit App section source
m The article has American English spellings. Do not use contractions.
Line 2:
{{See also|Java (programming language)#Syntax}}
{{More footnotes|date=January 2014}}
{{Use mdy dates|date=April 2025}}
{{Use American English|date=April 2025}}
[[File:Java keywords highlighted.svg|thumb|300px|A snippet of Java code with keywords highlighted in bold blue font]]
 
Line 326 ⟶ 328:
/* The following line is equivalent to
* java.util.Random random = new java.util.Random();
* It would've have been incorrect without the import.
*/
Random random = new Random();
Line 421 ⟶ 423:
! 2
| style="border-bottom-style: none" | <code>++</code> <code>--</code>
| style="border-bottom-style: none" | Postfix increment and decrement<ref>{{Cite web|title = Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)|url = http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html|website = docs.oracle.com|access-date = 2015-06-June 16, 2015|publisher = Oracle and/or its affiliates}}</ref>
|-
! rowspan=5| 3
Line 554 ⟶ 556:
 
====<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=2021-08-August 15, 2021|website=docs.oracle.com}}</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.
Line 794 ⟶ 796:
If no <code>catch</code> block matches the type of the thrown exception, the execution of the outer block (or method) containing the <code>try</code> ... <code>catch</code> 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 <code>catch</code> block is found within one of the currently active methods. If the exception propagates all the way up to the top-most <code>main</code> method without a matching <code>catch</code> block being found, a textual description of the exception is written to the standard output stream.
 
The statements within the <code>finally</code> block are always executed after the <code>try</code> and <code>catch</code> blocks, whether or not an exception was thrown and even if a <code>return</code> statement was reached. Such blocks are useful for providing clean-upcleanup code that is guaranteed to always be executed.
 
The <code>catch</code> and <code>finally</code> blocks are optional, but at least one or the other must be present following the <code>try</code> block.
Line 1,498 ⟶ 1,500:
</syntaxhighlight>
 
Lambda's parameters types don'tdo not have to be fully specified and can be inferred from the interface it implements. Lambda's body can be written without a body block and a <code>return</code> statement if it is only an expression. Also, for those interfaces which only have a single parameter in the method, round brackets can be omitted.<ref>{{Cite web|title=Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects)|url=https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html|access-date=August 8, 2021-08-08|website=docs.oracle.com}}</ref>
 
<syntaxhighlight lang="java">
Line 1,553 ⟶ 1,555:
====Default methods====
 
Java SE 8 introduced default methods to interfaces which allows developers to add new methods to existing interfaces without breaking compatibility with the classes already implementing the interface. Unlike regular interface methods, default methods have a body which will get called in the case if the implementing class doesn'tdoes not override it.
 
<syntaxhighlight lang="java">
Line 1,765 ⟶ 1,767:
* {{cite book |last1=Naughton |first1=Patrick |author1-link=Patrick Naughton |last2=Schildt |first2=Herbert |author2-link=Herbert Schildt |year=1999 |title=Java 2: The Complete Reference |edition=3rd |publisher=The McGraw-Hill Companies |isbn=0-07-211976-4}}
* {{cite book |last1=Vermeulen |last2=Ambler |last3=Bumgardner |last4=Metz |last5=Misfeldt |last6=Shur |last7=Thompson |year=2000 |title=The Elements of Java Style |url-access=registration |url=https://archive.org/details/elementsofjavast00verm |publisher=Cambridge University Press |isbn=0-521-77768-2}}
* {{cite book |last1=Gosling |first1=James |author1-link=James Gosling |last2=Joy |first2=Bill |author2-link=Bill Joy |last3=Steele |first3=Guy |author3-link=Guy L. Steele Jr. |last4=Bracha |first4=Gilad |year=2005 |title=Java Language Specification |edition=3rd |url=http://java.sun.com/docs/books/jls/ |publisher=Addison-Wesley Professional |access-date=December 3, 2008-12-03}}
{{Refend}}