Java syntax: Difference between revisions

Content deleted Content added
fixed lede
Line 9:
The syntax is mostly derived from [[C (programming language)|C]] and [[C++]]. Unlike in C++, in Java there are no global functions or variables, but there are data members which are also regarded as global variables. All code belongs to [[class (computer science)|classes]] and all values are [[object (computer science)|objects]]. The only exception is the [[primitive types]], which are not represented by a class instance 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 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 capabilities such as [[generic programming]] and [[function literals]] (called lambda expressions in Java). Since 2017, a new JDK version is released twice a year, with each release bringing incremental improvements to the language.
 
==Basics==
Line 317:
====Static import declaration====
 
This type of declaration has been available since [[J2SE 5.0]]. [[static imports|Static import]] declarations allow access to static members defined in another class, interface, annotation, or enum; without specifying the class name:
 
<syntaxhighlight lang="java">
Line 570:
 
===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.
 
====<code>while</code> loop====
Line 614:
====Enhanced <code>for</code> loop====
 
[[enhanced for loop|Enhanced <code>for</code> loop]]s have been available since [[J2SE 5.0]]. This type of loop uses built-in iterators over arrays and collections to return each item in the given collection. Every element is returned and reachable in the context of the code block. When the block is executed, the next item is returned until there are no items remaining. Unlike [[C Sharp (programming language)|C#]], this kind of loop does not involve a special keyword, but instead uses a different notation style.
 
<syntaxhighlight lang="java">
Line 813:
 
===<code>assert</code> statement===
<code>assert</code> statements have been available since [[J2SE 1.4]]. These types of statements are used to make [[assertion (computing)|assertion]]s in the source code, which can be turned on and off during execution for specific classes or packages. To declare an assertion the <code>assert</code> keyword is used followed by a conditional expression. If it evaluates to <code>false</code> when the statement is executed, an exception is thrown. This statement can include a colon followed by another expression, which will act as the exception's detail message.
 
<syntaxhighlight lang="java">
Line 897:
 
===Boxing and unboxing===
This language feature was introduced in [[J2SE 5.0]]. ''Boxing'' is the operation of converting a value of a primitive type into a value of a corresponding reference type, which serves as a wrapper for this particular primitive type. ''Unboxing'' is the reverse operation of converting a value of a reference type (previously boxed) into a value of a corresponding primitive type. Neither operation requires an explicit conversion.
 
Example:
Line 1,172:
 
=====Varargs=====
This language feature was introduced in [[J2SE 5.0]]. The last argument of the method may be declared as a variable arity parameter, in which case the method becomes a variable arity method (as opposed to fixed arity methods) or simply [[variadic function|varargs]] method. This allows one to pass a variable number of values, of the declared type, to the method as parameters - including no parameters. These values will be available inside the method as an array.
 
<syntaxhighlight lang="java">
Line 1,341:
 
====Enumerations====
This language feature was introduced in [[J2SE 5.0]]. Technically enumerations are a kind of class containing enum constants in its body. Each enum constant defines an instance of the enum type. Enumeration classes cannot be instantiated anywhere except in the enumeration class itself.
 
<syntaxhighlight lang="java">
Line 1,561:
====Annotations====
{{Main|Java annotation}}
Annotations in Java are a way to embed [[metadata]] into code. This language feature was introduced in [[J2SE 5.0]].
 
=====Annotation types=====
Line 1,611:
{{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===