Java syntax: Difference between revisions

Content deleted Content added
 
(12 intermediate revisions by 2 users not shown)
Line 309:
 
===={{mono|java.lang.String}}====
{{code|java.lang.String}} is Java's basic string type. It is [[immutable object|Immutableimmutable]]. It does not implement {{code|Iterable<Character>}}, so it cannot be iterated over in a for-each loop, but can be converted to <code>char[]</code>. Some methods treat each [[UTF-16]] code unit as a <code>char</code>, but methods to convert to an <code>int[]</code> that is effectively [[UTF-32]] are also available. <code>String</code> implements <code>CharSequence</code>, so <code>char</code>s in the <code>String</code> can be accessed by the method <code>charAt()</code>.
 
===={{mono|java.lang.Throwable}}====
Line 327:
 
====Primitives====
Each primitive type has an associated wrapper class (see [[#Primitive types|primitive types]]).
 
==Program structure==
Line 476:
/* The following line is equivalent to
if (foo == ColorName.RED) foo = ColorName.BLUE; */
if (foo == RED) {
foo = BLUE;
}
}
}
Line 513 ⟶ 515:
 
==Operators==
Operators in Java are similar to those in [[C++]]. However, there is no <code>delete</code> operator due to [[Garbage collection (computer science)|garbage collection]] mechanisms in Java, and there are no operations on [[Pointer (computer programming)|pointers]] since Java does not support them. Another difference is that Java has an unsigned right shift operator (<code>>>></code>), while C's right shift operator's signedness is type-dependent. Operators in Java cannot be [[Operator overloading|overloaded]]. The only overloaded operator is <code>operator+</code> for string concatenation.
 
{| class="wikitable"
Line 722 ⟶ 724:
 
===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. The condition must have type <code>boolean</code> or <code>java.lang.Boolean,</code>. meaningJava C's<syntaxhighlightdoes lang="c">not implicitly convert integers or class types to Boolean values.
 
For example, the following code is valid in C but results in a compilation error in Java.
<syntaxhighlight lang="c">
while (1) {
doSomething();
}
</syntaxhighlight>results in a compilation error.
 
====<code>while</code> loop====
Line 763 ⟶ 768:
</syntaxhighlight>
 
Like C, all three expressions are optional. The following loop isnever infiniteterminates:
<syntaxhighlight lang="java">
for (;;) {
Line 770 ⟶ 775:
</syntaxhighlight>
 
====Enhanced <code>for</code>Foreach loop====
{{Main|Enhanced forForeach loop}}
[[enhanced for loop|Enhanced <code>for</code>Foreach 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. This for loop from Java was later added to [[C++11]]. 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 783 ⟶ 788:
 
====Labels====
Labels are given points in code used by <code>break</code> and <code>continue</code> statements. The JavaWhile <code>goto</code> is a reserved keyword in Java, it cannot be used to jump to specific points in code (in fact it has no use at all).
 
<syntaxhighlight lang="java">
Line 982 ⟶ 987:
 
==Primitive types==
Primitive types in Java include integer types, floating-point numbers, [[UTF-16]] code units and a Boolean type. ThereUnlike C++ and C#, there are no unsigned types in Java except <code>char</code> type, which is used to represent UTF-16 code units. The lack of unsigned types is offset by introducing unsigned right shift operation (<code>>>></code>), which is not present in C++, methods such as <code>.toUnsignedInt()</code>. Nevertheless, criticisms have been leveled about the lack of compatibility with C and C++ this causes.<ref>{{cite web|url=http://darksleep.com/player/JavaAndUnsignedTypes.html|first=Sean|last=Owens|title=Java and unsigned int, unsigned short, unsigned byte, unsigned long, etc. (Or rather, the lack thereof)|access-date=April 21, 2010|archive-date=February 20, 2009|archive-url=https://web.archive.org/web/20090220171410/http://darksleep.com/player/JavaAndUnsignedTypes.html|url-status=live}}</ref> <!-- This is a terrible citation. If you have a better one, use it. -->
 
{| class="wikitable"
Line 1,058 ⟶ 1,063:
| N/A
|}
 
Java does not have support for unsigned integers like C++ and C#. However, Java has methods to treat integer types as unsigned, such as <code>.toUnsignedInt()</code>.
 
<code>null</code> has no type, neither primitive nor class. Any object type may store <code>null</code>.
Line 1,116 ⟶ 1,119:
Due to the nature of the multi-dimensional arrays, sub-arrays can vary in length, so multi-dimensional arrays are not bound to be rectangular unlike C:
<syntaxhighlight lang=Java>
int[][] numbers = new int[2][]; // Initialization of the first dimension only
 
numbers[0] = new int[3];
Line 1,285 ⟶ 1,288:
String str;
 
Foo() { // Constructor with no arguments
Foo() {}
 
Foo(String str) { // Constructor with one argument
// Initialization
Foo(String str) {
}
 
Foo(String str) { // Constructor with one argument
this.str = str;
}
Line 1,347 ⟶ 1,349:
The <code>throws</code> keyword indicates that a method throws an exception. All checked exceptions must be listed in a comma-separated list.
<syntaxhighlight lang="java">
// Indicates thatimport java.io.IOException may be thrown;
import java.util.zip.DataFormatException;
void operateOnFile(File f) throws IOException {
 
// Indicates that IOException and DataFormatException may be thrown
void operateOnFile(File f) throws IOException, DataFormatException {
// ...
}