Java Crash Course
About Java
Java is a widely used computer programming language known for its cross-platform, object-oriented, and generic programming features. It is extensively applied in enterprise-level web application development and mobile application development.
Environment Installation
Refer to JDK.
Basic Syntax
Main Function
Similar to C/C++, Java requires a function (referred to as a method in object-oriented programming) as the entry point for program execution.
The format of Java's main function is fixed, as follows:
1 2 3 4 5 | |
A packaged Java program (typically named *.jar) can contain many such functions. However, when running the program, only one of these functions will be executed, as defined in the Jar's Manifest file. This knowledge is generally not required in OI competitions.
Comments
Like C/C++, Java uses // and /* */ to comment single lines and multiple lines, 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 |
Variable Declaration
1 2 3 4 | |
final Keyword
final means the result is final and unchangeable. A variable modified by final can only be assigned once and cannot be changed thereafter.
1 | |
Arrays
1 2 3 | |
Strings
- String is a built-in class in Java.
1 2 3 4 5 6 | |
Packages and Importing Packages
Classes (Class) in Java are placed within packages (package). Duplicate class names are not allowed within the same package. The first line of a class typically specifies which package the class belongs to. For example:
1 | |
The naming convention for packages is generally: top-level domain of project owner.second-level domain of project owner.project name.
Use the import keyword to import classes that are not in the same package as the current class. For example, the Scanner class used below:
1 | |
To import all classes under a package, simply replace the class name before the semicolon in the statement with *.
Input
Command-line input can be handled via the Scanner class.
1 2 3 4 5 6 7 8 9 10 11 12 | |
Output
Variables can be formatted for output.
| Symbol | Meaning |
|---|---|
%f |
Floating-point type |
%s |
String type |
%d |
Integer type |
%c |
Character type |
1 2 3 4 5 6 7 8 9 10 11 12 | |
Control Statements
Java's flow control statements are essentially the same as those in C++.
Selection
- if
1 2 3 4 5 6 7 | |
- if...else
1 2 3 4 5 6 7 8 9 | |
- if...else if...else
1 2 3 4 5 6 7 8 9 10 11 | |
- switch...case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Loops
- for
The for keyword has two usage patterns. The first is the ordinary for loop, structured as follows:
1 2 3 4 5 6 7 | |
The second is similar to C++'s foreach, used to iterate over data in arrays or collections. It essentially hides the loop variable from the first pattern. The structure is as follows:
1 2 3 4 5 6 7 | |
- while
1 2 3 4 5 6 7 | |
- do...while
1 2 3 4 5 6 7 | |
Notes
Class Name Must Match File Name
Creating a Java source program requires the class name to match the file name for successful compilation; otherwise, the compiler will report that the class cannot be found. Typically, this file name is specified by the specific OJ.
Example:
Add.java
1 2 3 4 5 | |
In this file, the class name must be Add for compilation to succeed.
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用