Content deleted Content added
Jerryobject (talk | contribs) Small WP:COPYEDITs WP:EoS: WP:TERSE, clarify, MOS:NOTETHATs cut. WP:LINKs: update-standardizes, adds, needless WP:PIPE > WP:NOPIPE. WP:REFerences: plain text > WP:CITations; parameters: adds, fills, author > last + first, update-standardize-conform, reorders. WP:NAVBOX update. Adds: MOS:COMMENT, WP:CATEGORY. |
|||
Line 1:
{{Short description|Set of rules defining correctly structured program}}
{{
{{
[[File:Java keywords highlighted.svg|thumb|300px|A snippet of Java code with keywords highlighted in bold blue font]]
The '''syntax of Java''' is [[syntax|the set of rules]] defining how a [[Java (programming language)|Java]] program is written and interpreted.
The [[Syntax (programming languages)|syntax]] is mostly derived from [[C (programming language)|C]] and [[C++]]. Unlike
The Java syntax has been gradually extended in the course of numerous major [[JDK]] [[Java version history|releases]], and now supports
==Basics==
Line 22:
An identifier cannot:
* Start with a digit.
* Be equal to a reserved keyword, null literal or [[Boolean data type|boolean]] literal.
===Keywords===
Line 209:
===Code blocks===
The separators {{mono|{{(}}}} and {{mono|{{)}}}} signify a code block and a new scope. Class members and the body of a [[Method (computer programming)|method]] are examples of what can live inside these braces in various contexts.
Inside of method bodies, braces may be used to create new scopes, as follows:
Line 525:
</syntaxhighlight>
Also,
<syntaxhighlight lang="java">
int a = 1;
Line 533:
====<code>switch</code> statement====
[[Switch statement]]s in Java can use <code>byte</code>, <code>short</code>, <code>char</code>, and <code>int</code> (
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 581:
===Iteration statements===
Iteration statements are statements that are repeatedly executed when a given condition is evaluated as true. Since [[J2SE 5.0]], Java has four forms of such statements.
while (1) {
doSomething();
Line 642:
====Labels====
Labels are given points in code used by <code>break</code> and <code>continue</code> statements.
<syntaxhighlight lang="java">
Line 1,155:
====Methods====
All the statements in Java must reside within [[Method (computer programming)|methods]]. Methods are similar to functions except they belong to classes. A method has a return value, a name and usually some parameters initialized when it is called with some arguments. Similar to C++, methods returning nothing have return type declared as <code>void</code>. Unlike in C++, methods in Java are not allowed to have [[default argument]] values and methods are usually overloaded instead.
<syntaxhighlight lang="java">
Line 1,640:
{{Main|Generics in Java}}
[[Generic programming|Generics]], or parameterized types, or [[
===Generic classes===
Line 1,739:
{{Refbegin}}
*
* {{cite book |
* {{cite book |last1=Gosling |first1=James |author1-link=James Gosling |last2=Joy |first2=Bill |author2-link=
{{Refend}}
Line 1,752 ⟶ 1,749:
* {{Javadoc:SE}}
{{Java (
[[Category:Programming language syntax]]
[[Category:Java (programming language)]]
<!-- Hidden categories below -->
[[Category:Articles with example Java code]]
|