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|
===={{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
For example, the following code is valid in C but results in a compilation error in Java.
<syntaxhighlight lang="c">
while (1) {
doSomething();
}
</syntaxhighlight>
====<code>while</code> loop====
Line 763 ⟶ 768:
</syntaxhighlight>
Like C, all three expressions are optional. The following loop
<syntaxhighlight lang="java">
for (;;) {
Line 770 ⟶ 775:
</syntaxhighlight>
====
{{Main|
[[
<syntaxhighlight lang="java">
Line 783 ⟶ 788:
====Labels====
Labels are given points in code used by <code>break</code> and <code>continue</code> statements.
<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.
{| class="wikitable"
Line 1,058 ⟶ 1,063:
| N/A
|}
<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() {}
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">
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 {
// ...
}
|