About Java
Java is a widely used computer programming language. It features cross-platform compatibility, object-oriented programming, and generic programming, and is widely used in enterprise-level Web application development and mobile application development.
Installation
See JDK.
Basic syntax
The main function
Like C/C++, Java needs a function (called a method in object-oriented programming) as the entry point for program execution.
The format of Java's main function is fixed, as follows:
class Test {
public static void main(String[] args) {
// Program code
}
}
A packaged Java program (usually named *.jar) may contain many similar functions, but when the program is run, only one of them is executed. This is defined in the Manifest file of the Jar; this knowledge is generally not needed in OI competitions.
Comments
Like C/C++, Java uses // and /* */ for single-line and multi-line comments, respectively.
Basic data types
| Type name | Meaning |
|---|---|
| boolean | Boolean type |
| byte | Byte type |
| char | Character type |
| double | Double-precision floating point |
| float | Single-precision floating point |
| int | Integer type |
| long | Long integer type |
| short | Short integer type |
| null | Null |
Declaring variables
int a = 12; // Set a to integer type and assign 12 to a
String str = "Hello, OI-wiki"; // Declare the string variable str
char ch = 'W';
double PI = 3.1415926;
The final keyword
final means that this is a final, unchangeable result. A variable modified by final can be assigned only once and cannot be changed after assignment.
final double PI = 3.1415926;
Arrays
// An integer array with ten elements
// Its syntax is: data type[] variable name = new data type[array size]
int[] ary = new int[10];
Strings
- A string is a built-in class in Java.
// The simplest way to construct a string variable is as follows
String a = "Hello";
// A string variable can also be constructed from a character array
char[] stringArray = { 'H', 'e', 'l', 'l', 'o' };
String s = new String(stringArray);
Packages and imports
In Java, classes (Class) are placed in packages (package). A package cannot contain two classes with the same name. The first line of a class usually specifies which package it belongs to. For example:
package org.oi-wiki.tutorial;
The usual package naming convention is: 项目所有者的顶级域.项目所有者的二级域.项目名称.
The import keyword is used to import classes that are not in the package to which the current class belongs. For example, the Scanner used below:
import java.util.Scanner;
To import all classes under a package, simply replace the class name before the final semicolon in this statement with *.
Input
The Scanner class can be used to process command-line input.
package org.oiwiki.tutorial;
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); // System.in is the input stream
int a = scan.nextInt();
double b = scan.nextDouble();
String c = scan.nextLine();
}
}
Output
Variables can be printed in formatted form.
| Symbol | Meaning |
|---|---|
%f | Floating-point type |
%s | String type |
%d | Integer type |
%c | Character type |
class Test {
public static void main(String[] args) {
int a = 12;
char b = 'A';
double s = 3.14;
String str = "Hello world";
System.out.printf("%f\n", s);
System.out.printf("%d\n", a);
System.out.printf("%c\n", b);
System.out.printf("%s\n", str);
}
}
Control statements
Java's flow-control statements are basically the same as C++'s.
Selection
- if
class Test {
public static void main(String[] args) {
if ( /* condition */ ){
// Code executed when the condition is true
}
}
}
- if...else
class Test {
public static void main(String[] args) {
if ( /* condition */ ) {
// Code executed when the condition is true
} else {
// Code executed when the condition is false
}
}
}
- if...else if...else
class Test {
public static void main(String[] args) {
if ( /* condition */ ) {
// Code executed when the condition is true
} else if ( /* condition 2 */ ) {
// Code executed when condition 2 is true
} else {
// Code executed when none of the above conditions is true
}
}
}
- switch...case
class Test {
public static void main(String[] args) {
switch ( /* expression */ ){
case /* value 1 */:
// Execute this code when the expression's value matches value 1
break; // Without break, the program continues executing in order until it reaches break
case /* value 2 */:
// Execute this code when the expression's value matches value 2
break;
default:
// Execute this code when the expression does not match any value listed above
}
}
}
Loops
- for
The for keyword has two uses. The first is the ordinary for loop, whose form is as follows:
class Test {
public static void main(String[] args) {
for ( /* initialization */; /* loop condition */; /* step executed after each loop */ ) {
// Code in the loop body executed when the loop condition is true
}
}
}
The second is a use similar to C++'s foreach, for iterating over data in an array or collection. It is equivalent to hiding the loop variable in the previous form:
class Test {
public static void main(String[] args) {
for ( /* element type X */ /* element name Y */ : /* collection Z */ ) {
// On each iteration of this block, element Y is one element of collection Z.
}
}
}
- while
class Test {
public static void main(String[] args) {
while ( /* condition */ ) {
// Code in the loop body executed when the condition is true
}
}
}
- do...while
class Test {
public static void main(String[] args) {
do {
// Code to execute
} while ( /* loop condition */ );
}
}
Notes
Class name and file name must match
When creating a Java source program, the class name and file name must match for compilation to succeed; otherwise, the compiler will report that it cannot find the class. Usually, the file name is specified by the particular OJ.
Example:
Add.java
class Add {
public static void main(String[] args) {
// ...
}
}
The class in this file must be named Add for compilation to succeed.