May 10, 2021 Java
A Java program can be thought of as a collection of objects that work together by calling each other's methods. The following is a brief introduction to the concepts of classes, objects, methods, and instance variables.
Here's a look at a simple Java program that will print the string Hello World
public class MyFirstJavaProgram {
/* 第一个Java程序.
* 它将打印字符串 Hello World
*/
public static void main(String []args) {
System.out.println("Hello World"); // 打印 Hello World
}
}
Here's a step-by-step look at how to save, compile, and run the program:
You'll see Hello World in the window
C : > javac MyFirstJavaProgram.java
C : > java MyFirstJavaProgram
Hello World
When writing Java programs, you should be aware of the following:
All components of Java require a name. Class names, variable names, and method names are all referred to as identifiers.
There are a few points to note about Java identifiers:
Like other languages, Java can use modifiers to modify methods and properties in classes. There are two main types of modifiers:
We'll dive into Java modifiers in a later section.
There are several main types of variables in Java
An array is an object stored on a heap and can hold multiple variables of the same type. In later chapters, we'll learn how to declare, construct, and initialize an array.
Java 5.0 introduces enumeration, which limits variables to pre-set values. Use enumeration to reduce bugs in your code.
For example, we design a program for juice shops that will limit juice to small, medium and large cups. This means that it does not allow customers to order juice other than these three sizes.
class FreshJuice {
enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
FreshJuiceSize size;
}
public class FreshJuiceTest {
public static void main(String args[]){
FreshJuice juice = new FreshJuice();
juice.size = FreshJuice. FreshJuiceSize.MEDIUM ;
}
}
Note: Enumerity can be declared separately or inside a class. Methods, variables, and constructors can also be defined in enumerations.
The Java reserved word is listed below. T hese reserved words cannot be used for the names of constants, variables, and any identifiers.
Keywords | Describe |
---|---|
abstract | Abstract methods, modifiers of abstract classes |
Assert | Whether the assertion condition is met |
boolean | Boolean data type |
break | Jump out of a loop or label snipp |
byte | 8-bit signed data type |
case | A condition of the switch statement |
Catch | Match with try to catch abnormal information |
char | 16-bit Unicode character data type |
class | Define the class |
const | Not used |
continue | The rest of the loop body is not executed |
default | The default branch in the switch statement |
do | Loop statement, the loop body executes at least once |
double | 64-bit double floating point |
else | The branch that is executed when the if condition is not established |
Enum | The enumerity type |
extends | Indicates that one class is a sub-class of another class |
final |
Indicates that a value cannot be changed after initialization
The indicates that the method cannot be overrided, or that a class cannot have sub-classes |
finally | The code that is executed is designed primarily for the robustness and integrity of the program, with or without exceptions. |
float | 32-bit single-precision floating point |
for | For loop statement |
Goto | Not used |
if | Conditional statement |
implements | Indicates that a class implements an interface |
import | Import the class |
instanceof | Test whether an object is an instance of a class |
Int | 32-bit integer number |
interface | Interface, an abstract type, defined only by methods and constants |
long | 64-bit integer number |
native | The means method is implemented with non-java code |
new | Assign a new class instance |
package | A series of related classes form a package |
private | Represents a private field, or method, etc., that can only be accessed from inside the class |
protected |
Indicates that a field can only be accessed through a class or its sub-classes
Sub-classes or other classes within the same package |
public | Represents a common property or method |
return | Method returns a value |
short | 16 digits |
static | Represents what is defined at the class level and shared by all instances |
strictfp | Floating points use strict rules |
super | Represents the base class |
switch | Select the statement |
synchronized | Represents a block of code that can only be accessed by one thread at a time |
this |
Represents the call to the current instance
Or call another constructor |
throw | Throw an exception |
throws | Define the exception that the method might throw |
transient | Decorate fields that do not serialize |
try | Indicates that the block of code does exception handling or cooperates with the finally to indicate that the code in the finally is executed whether or not the exception is thrown |
void | The tag method does not return any values |
volatile | Tag fields may be accessed by multiple threads at the same time without synchronization |
while | While loop |
Similar to C/C, Java also supports single-line and multi-line annotations. The characters in the comment are ignored by the Java compiler.
public class MyFirstJavaProgram{
/* 这是第一个Java程序
*它将打印Hello World
* 这是一个多行注释的示例
*/
public static void main(String []args){
// 这是单行注释的示例
/* 这个也是单行注释的示例 */
System.out.println("Hello World");
}
}
Blank lines, or only commented lines, are ignored by the Java compiler.
In Java, one class can be derived from other classes. If you are creating a class and already have a class with the properties or methods you need, you can inherit the class from the newly created class.
Inherited methods allow you to reuse methods and properties of existing classes without overrideing the code. Inherited classes are called superclasses, and derived classes are called subclasses.
In Java, interfaces can be understood as protocols for objects to communicate with each other. Interfaces play an important role in inheritance.
Interfaces define only the methods that are derived, but the implementation of the method depends entirely on the derived class.
The next section describes classes and objects in Java programming. You'll then have a clearer understanding of the classes and objects in Java.