Content deleted Content added
(23 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 420 ⟶ 338:
<syntaxhighlight lang=Java>
public static void main(String[] args) {
// ...
}
</syntaxhighlight>
Line 558 ⟶ 476:
/* The following line is equivalent to
if (foo == ColorName.RED) foo = ColorName.BLUE; */
if (foo == RED) {
foo = BLUE; }
}
}
Line 595 ⟶ 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 776 ⟶ 696:
<syntaxhighlight lang="java">
enum Result {
GREAT,
FINE,
// more enum values
}
Result result = switch (ch) {
case 'A' -> Result.GREAT;
case 'B', 'C' -> Result.FINE;
default -> throw new
};
</syntaxhighlight>
Line 786 ⟶ 712:
<syntaxhighlight lang="java">
case 'A':
yield Result.GREAT;
Line 793 ⟶ 719:
yield Result.FINE;
default:
throw new
};
</syntaxhighlight>
===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 839 ⟶ 768:
</syntaxhighlight>
Like C, all three expressions are optional. The following loop
<syntaxhighlight lang="java">
for (;;) {
Line 846 ⟶ 775:
</syntaxhighlight>
====
{{Main|
[[
<syntaxhighlight lang="java">
Line 859 ⟶ 788:
====Labels====
Labels are given points in code used by <code>break</code> and <code>continue</code> statements.
<syntaxhighlight lang="java">
Line 1,058 ⟶ 987:
==Primitive types==
Primitive types in Java include integer types, floating-point numbers, [[UTF-16]] code units and a Boolean type.
{| class="wikitable"
|-
!colspan="6"|Primitive
|-
! Type
! [[Java Class Library|Class Library]] equivalent
! Value
! Range
! Size
! Default
|-
| {{java|short}}
|
| integer
| −32,768 through +32,767
Line 1,085 ⟶ 1,007:
| <code>0</code>
|-
|
|
| integer
| −2,147,483,648 through +2,147,483,647
Line 1,092 ⟶ 1,014:
| <code>0</code>
|-
|
|
| integer
| −9,223,372,036,854,775,808 through<br/> +9,223,372,036,854,775,807
Line 1,099 ⟶ 1,021:
| <code>0</code>
|-
| {{java|byte}}
|
| integer
| -128 through 127
| 8-bit (1-byte)
| <code>0</code>
|-
| {{java|float}}
| {{java|java.lang.Float}}
| floating point number
| ±1.401298E−45 through ±3.402823E+38
| 32-bit (4-byte)
| <code>0.0</code>
|-
|
|
| floating point number
| ±4.94065645841246E−324 through<br/> ±1.79769313486232E+308
Line 1,113 ⟶ 1,042:
| <code>0.0</code>
|-
|
|
| Boolean
|
|
|
|-
|
|
| single Unicode character
|
| 16-bit (2-byte)
|
|-
| {{java|void}}
| {{java|java.lang.Void}}
| N/A
| N/A
| N/A
| N/A
|}
<code>null</code> has no type, neither primitive nor class. Any object type may store <code>null</code>.
The class <code>Void</code> is used to hold a reference to the <code>Class</code> object representing the keyword <code>void</code>. It cannot be instantiated as <code>void</code> cannot be the type of any object. For example, <code>CompletableFuture<Void></code> signifies that a <code>CompletableFuture</code> performs a task that does not return a value.
The class <code>java.lang.Number</code> represents all numeric values which can be converted to <code>byte</code>, <code>double</code>, <code>float</code>, <code>int</code>, <code>long</code>, and <code>short</code>.
<code>char</code> does not necessarily correspond to a single character. It may represent a part of a [[UTF-16#Encoding of characters outside the BMP|surrogate pair]], in which case Unicode code point is represented by a sequence of two <code>char</code> values.
Line 1,177 ⟶ 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,198 ⟶ 1,140:
!Inner class
|<syntaxhighlight lang="java">
// Inner class
class Bar {
// ...
}
}
Line 1,206 ⟶ 1,151:
!Nested class
|<syntaxhighlight lang="java">
// Nested class
static class Bar {
// ...
}
}
Line 1,216 ⟶ 1,164:
class Foo {
void bar() {
class Foobar {
// ...
}
}
Line 1,226 ⟶ 1,176:
class Foo {
void bar() {
new Object() {};
}
}
Line 1,257 ⟶ 1,207:
public class Foo {
public static void doSomething() {
// ...
}
}
Line 1,283 ⟶ 1,234:
<syntaxhighlight lang="java">
public class Foo {
int
return 0;
}
private class Bar {
}
}
Line 1,336 ⟶ 1,288:
String str;
Foo() {}
// Constructor with one argument
Foo(String str) {
this.str = str;
}
Line 1,377 ⟶ 1,328:
class Foo {
int bar(int a, int b) {
return (a * 2) + b;
}
/* Overloaded method with the same name but different set of arguments */
int bar(int a) {
return a * 2;
}
}
Line 1,398 ⟶ 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.io.IOException;
import java.util.zip.DataFormatException;
// Indicates that IOException and DataFormatException may be thrown
void operateOnFile(File f) throws IOException, DataFormatException {
// ...
}
</syntaxhighlight>
Line 1,407 ⟶ 1,363:
* '''<code>final</code>''' - Declares that the method cannot be overridden in a subclass.
* '''<code>native</code>''' - Indicates that this method is implemented through [[JNI]] in platform-dependent code. Actual implementation happens outside Java code, and such methods have no body.
* '''<code>strictfp</code>''' - Declares strict conformance to [[IEEE 754]] in carrying out floating-point operations. Now obsolete.
* '''<code>synchronized</code>''' - Declares that a thread executing this method must acquire monitor. For <code>synchronized</code> methods the monitor is the class instance or <code>java.lang.Class</code> if the method is static.
* Access modifiers - Identical to those used with classes.
Line 1,420 ⟶ 1,376:
<syntaxhighlight lang="java">
void printReport(String header, int... numbers) {
System.out.println(header);
for (int num : numbers) {
Line 1,520 ⟶ 1,477:
<syntaxhighlight lang="java">
package org.
public class AbstractClass {
private static final String hello;
static {
System.out.
hello = String.format("hello from %s"
}
{
System.out.
}
public AbstractClass() {
System.out.
}
Line 1,547 ⟶ 1,501:
</syntaxhighlight>
<syntaxhighlight lang="java">
package org.
public class CustomClass extends AbstractClass {
static {
System.out.
}
{
System.out.
}
public CustomClass() {
System.out.
}
Line 1,569 ⟶ 1,520:
CustomClass nc = new CustomClass();
hello();
}
}
Line 1,576 ⟶ 1,527:
Output:
<syntaxhighlight lang="text">
org.
org.
org.
org.
org.
org.
hello from org.
</syntaxhighlight>
Line 1,653 ⟶ 1,604:
class ActionHandler implements ActionListener, RequestListener {
public void actionSelected(int action) {
// ...
}
public int requestReceived() {
// ...
}
}
// Calling method defined by interface
// ActionHandler can be represented as RequestListener...
RequestListener listener = new ActionHandler();
</syntaxhighlight>
Line 1,765 ⟶ 1,718:
@Override
public String extendString(String input) {
return
}
}
|