■ The first Java program
Class hello ...{
}
Class welcome ...{
}
Save it as hello. Java and compile it at the doscommand prompt:
After compilation is successful, two class files are generated in the Directory D:/javae/lesson1: Hello. Class and welcome. Class.
When declaring a class as public type, you must change the name of the Java file to the class name modified by public.
If:
Hello. Java
Class hello ...{
}
Public class welcome ...{
}
Compilation errors:
That is to say, a Java file can only have one class defined as public.
If we only define two classes without defining attributes and methods, the following procedure:
Public class hello ...{
}
Class welcome ...{
}
Save it as hello. Java, compile it at the doscommand prompt, and run:
The prompt is that the main function is not found. From here, we know that the Java program also has an entry address, that is, the main method.
We add a main function to the Hello class:
Public class hello ...{
Public static void main (string [] ARGs )...{
}
}
Class welcome ...{
}
Compile and run without errors:
■ About the Java interpreter
The Java interpreter loads classes instead of files.
If:
Public class hello ...{
Public static void main (string [] ARGs )...{
}
}
Class welcome ...{
}
If Hello. Java is input as hello. Java, no compilation error occurs.
No problem during running.
However, if we mistakenly input hello as hello at runtime, an error will occur:
Because the class name hello is not found, loading fails and an error occurs.
This verifies that the Java interpreter loads classes instead of files.
Below we write the Java program that outputs the string:
Public class hello ...{
Public static void main (string [] ARGs )...{
System. Out. println ("this is sky2098's first line! ");
System. Out. println ("this is sky2098's second line! ");
}
}
Compile and run:
If print is used for the out object method, no line break is returned. The output result is:
Of course, if you use the out object method to print the output and want to wrap it, you can add the line break "/N", that is:
System. Out. Print ("this is sky2098's first line! /N ");
Note: if our Java program has been modified, it needs to be re-compiled before each execution. Otherwise, the class file generated before the modification is loaded.
If we switch to the D:/javae> directory and then compile and execute it, an error will occur:
An error occurs during execution:
How can this problem be solved?
To successfully execute the Java program under the D:/javae> directory, you can set the value of classpath:
Set classpath = D:/javae/lesson1
Then run the Java program:
Run successfully.
If we set Hello under the Directory D:/javae/lesson1. if the class file is deleted and then run in the D:/javae> directory, an error occurs. This is because the class file fails to be loaded:
If you have compiled hello. Java in the Directory D:/javae/lesson1, set the value of classpath:
Set classpath = E :/
Then run the Java program. An error occurs:
This is because the current running Java program is searched in the E:/directory. Although the hello. Class file exists in the Directory D:/javae/lesson1, the search fails if it fails.
In this case, an error occurs when running hello. class. We can set the value of classpath as follows:
Set classpath =.
Switch the directory to the Current Directory D:/Java/lesson1.
In this way, you can run successfully:
We set a classpath in the current Command Prompt window. However, when a command prompt window is restarted, the previously set classpath is invalid.
You can enter:
Start
Then, the classpath in the newly started Command Prompt window is the same as the original one.
However, when all the command prompt windows are closed, you still need to reset them, which is time-consuming. You can set the value of classpath as follows:
"My Computer"-> "properties"-> "advanced"-> "environment variables"-> "system variables"
Select classpath and click Edit. Set the variable value.
Note: it must be a "system variable". If you select "User variable", You need to reset it for different users.
■ About Arrays
◆One-dimensional array
As shown in the following program:
Public class myarray
...{
Public static void main (string [] ARGs )...{
Int num [] = new int [3];
For (INT I = 0; I <3; I ++ )...{
Num [I] = 2 * I;
}
For (INT I = 0; I <3; I ++ )...{
System. Out. println (Num [I]);
}
}
}
Compile and execute:
Arrays can be defined in two ways:
◎ Method 1:
For example:
Int num [] = new int [3];
Or
Int num [];
Num = int [3];
◎ Method 2:
For example:
Int [] num = new int [3];
Or:
Int [] num;
Num = new int [3];
The second definition method is recommended.
Of course, you can assign an initial value to an array when defining an array. There are also two ways:
◎ First initialization method:
For example:
Int [] num = {1, 2, 4, 5, 6, 7, 8 };
However, the following definition and initialization method is incorrect:
Int [] num;
Num = {1, 2, 3, 4, 5, 6, 7, 8 };
◎ Second initialization method:
For example:
Int [] num = new int [] {1, 2, 3, 4, 5, 6 };
We can define arrays, allocate space, assign initial values, and access them, for example:
Int [] num;
Num = new int [3];
Num [0] = 2;
Num [1] = 5;
Num [2] = 9;
System. Out. println (Num [0]);
System. Out. println (Num [1]);
System. Out. println (Num [2]);
◆ Two-dimensional array
Two-dimensional array definition can be defined like a one-dimensional array, for example:
Int [] [] num = new int [3] [4];
In Java, the number of columns in each row of a two-dimensional array can be unequal, which is very convenient to use. For example:
Int [] [] num = new int [3] [];
Num [0] = new int [3];
Num [1] = new int [4];
Num [2] = new int [5];
Initial Values of two-dimensional arrays:
Int [] [] num = new int [] [] {1, 2, 3}, {4, 5, 6 }};
You can also assign an initial value as follows:
Int [] [] num = {1, 2}, {4, 5, 6 }};
However, the following error occurs:
Int [] [] num = new int [2] [] {1, 2, 3}, {4, 5, 6 }};
This is also an error:
Int [] [] num = new int [] [] {1, 2, 3, 4, 5, 6 };
The columns of each row in a two-dimensional array can be different:
Int [] [] num = new int [] [] {1, 2, 3}, {4, 5, 6, 7, 8}, {9 }};
Or:
Int [] [] num = {1, 2}, {4, 5, 6, 7, 8}, {9 }};