Java syntax: Difference between revisions

Content deleted Content added
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}}
{{seeSee also|Java (programming language)#Syntax}}
{{moreMore footnotes|date=January 2014}}
[[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 in C++, in Java there arehas no global functions or variables, but there arehas data members which are also regarded as [[global variable]]s. All code belongs to [[classClass (computer scienceprogramming)|classes]] and all values are [[objectObject (computer science)|objects]]. The only exception is the [[primitive data types]], which are not considered to be objects for performance reasons (though can be automatically converted to objects and vice versa via [[#Boxing and unboxing|autoboxing]]). Some features like [[operator overloading]] or [[unsigned integer]] types[[data type]]s are omitted to simplify the language and to avoid possible programming mistakes.
 
The Java syntax has been gradually extended in the course of numerous major [[JDK]] [[Java version history|releases]], and now supports capabilitiesabilities such as [[generic programming]] and [[anonymous function literals]]s (function literals, called lambda expressions in Java). Since 2017, a new JDK version is released twice a year, with each release bringing incremental improvements toimproving the language incrementally.
 
==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, note that thea [[?:]] operator can be used in place of simple if statement, for example
<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> (note: 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-15|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 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. Note that theThe condition must have type boolean or Boolean, meaning C's<syntaxhighlight lang="c">
while (1) {
doSomething();
Line 642:
 
====Labels====
Labels are given points in code used by <code>break</code> and <code>continue</code> statements. Note that theThe Java <code>goto</code> keyword cannot be used to jump to specific points in the code.
 
<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 [[Polymorphism in object-oriented programming#Parametric Polymorphism|parametric polymorphism]], is one of the major features introduced in [[J2SE 5.0]]. Before generics were introduced, it was required to declare all the types explicitly. With generics, it became possible to work in a similar manner with different types without declaring the exact types. The main purpose of generics is to ensure type safety and to detect runtime errors during compilation. Unlike C#, information on the used parameters is not available at runtime due to [[type erasure]].<ref>[https://msdn.microsoft.com/en-us/library/f4a6ta2h.aspx Generics in the Run Time (C# Programming Guide)]</ref>
 
===Generic classes===
Line 1,739:
 
{{Refbegin}}
* [[{{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'', third |edition.=3rd |publisher=The McGraw-Hill Companies, 1999. {{ISBN|isbn=0-07-211976-4}}
* {{cite book | author1 last1= Vermeulen |first1= author2 |last2= Ambler |first2= author3 |last3= Bumgardner |first3= author4 |last4= Metz |first4= author5 |last5= Misfeldt | author6 first5= |last6=Shur |first6= author7|last7=Thompson |first7= Thompson|year=2000 | title = The Elements of Java Style | publisher = Cambridge University Press | year = 2000 | isbn = 0-521-77768-2 | 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 |Joy,last3=Steele Bill]]|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=2008-12-03}}
| author3 = [[Guy Steele|Steele, Guy]] | author4 = [[Gilad Bracha|Bracha, Gillad]]
| title=Java Language Specification |edition=3rd |publisher=Addison-Wesley Professional |year=2005
| url=http://java.sun.com/docs/books/jls/index.html |access-date=2008-12-03}}
{{Refend}}
 
Line 1,752 ⟶ 1,749:
* {{Javadoc:SE}}
 
{{Java (Sunsoftware platform)}}
 
[[Category:Programming language syntax]]
[[Category:Java (programming language)]]
<!-- Hidden categories below -->
[[Category:Articles with example Java code]]