Java does not take detours tutorial (2.Hello, Java!)

2. Hello, Java!
Welcome to the world of Java. In the last chapter, we have completed the basic operation learning of DOS and the construction of the Java environment. In this chapter, we will complete a simple DOS program in Java.

2.1 Hello, Java!
First, we create a file named MyNotepad.java and save it to c:\work\001.
The contents of the file are as follows: (Pay attention to the case of the file name and file content, and will not be prompted later)

Then start the DOS environment and enter the following command:
cd c:\work\001
javac MyNotepad.java
java MyNotepad

You can see the output: Hello, Java!
Execute java MyNotepad again, there is no need to compile again.

OK, we have completed a simple DOS program, please understand this process carefully, because the next content of this tutorial will be the continuous expansion of this process until the completion of Java learning.

2.2 Viewing the content of the file
Now there is a simple requirement:
enter the following command to view the content of the file a.txt:
java MyNotepad a.txt

Let's briefly introduce the MyNotepad.java just written

In the above program, the semicolon represents the end of a line, and the curly brackets represent the end of a block.
MyNotepad is the class name, main is the class entry, String[] args is used to receive the parameters of the program, because the program will have multiple parameter names such as
java MyNotepad a.txt b.txt c.txt
, so the type of variable args is defined as a can An array that receives multiple strings (String) is used to receive multiple parameters, as shown in the following figure:

We use [] to indicate that the parameter is an array.
You can use the following methods to get the value of each element in the array:
args[0], args[1], args[2]...

Note: The first element in Java is subscripted 0,

we try to come first Receive input parameters and output to DOS interface:

Note: The plus sign is used to concatenate two strings.

Compile and run: java MyNotepad a.txt
outputs Hello, a.txt

/////////////////////// /////////////
Take a break, let's take a look at how this process is done:
1. Create the file MyNotepad.java and write the program.
2. Enter the command javac MyNotepad.java to compile the program into a class file (MyNotepad.class).
We call MyNotepad.java as the source file and MyNotepad.class as the target file (execution file).
The compilation process is shown in the figure below

At this time, you can delete MyNotepad.java, because program execution is done by parsing the target file (.class file).
We use Notepad to open the .class file to see the contents, but we can't understand it. . . , yes, because this is machine readable. Compared to humans, it
is easier for machines to read class files than java files.
Therefore, we need a compilation process to translate java files into class files. The same process of translating class files into java files is called decompilation. This tutorial does not discuss the content of decompilation.

3. We run the Java program and pass in the parameter MyNotepad a.txt
. In this process, the Java program first searches for the class definition named MyNotepad in the environment variable CLASSPATH according to the incoming parameter MyNotepad, that is, the definition of class MyNotepad,
because we set the current Directory. In the environment variable CLASSPATH, so the Java program finds the class definition of MyNotepad in the current directory, then calls the fixed entry main method of the class,
and passes the subsequent parameter a.txt to the args variable of main.
Note: In the new version of JDK, there is no need to set the environment variable CLASSPATH, the compiler will automatically find the relevant library files, which will not be explained in the future.

4. In main, the first incoming parameter of args, args[0](a.txt), is spliced ​​together with the previous string Hello to generate a new string Hello a.txt, which is then
passed to Syste.out as a parameter .println method, which outputs the passed parameter "Hello a.txt" to the DOS interface.

Summary:
So far we have been exposed to the following concepts:
class, function/method, parameter
Note: function and method are the same concept
It can be represented by the following figure:
public class class name {
- function 1
- function 2
- function 3
...
- main function main
}

where the function is defined as follows
public static return value function name (parameter type parameter name) {}

where {} represents The beginning and end of a block, such as class definitions, function definitions.
When the following command is used to run a Java program, the system will call the function named main by default and pass subsequent parameters to the main function. (We call main the main function/main method)
java class name

The return value of the function will be introduced later.

///////////////////////// ///////////////////

Next we continue to expand this program.

For System.out.println, we will explain it in subsequent chapters, because we cannot understand this sentence at this stage The specific meaning of the words, so we will temporarily hide him.
To hide what we don't want to see, the first thought is to turn it into a method call, so our program becomes like this:

For calling a static (static) function of a certain class, the format of classname.functionname() is generally used directly. If the current class is the current class, the class name can be omitted, such as the above program.
We renamed the formal format as follows:
Note: Static function means adding the static keyword to the function definition, which we will explain in subsequent chapters.

Still not clear enough, I want to put this method in another class.
Create a new file MyUtil.java as follows:

Modify MyNotepad.java as follows:

Compile MyUtil and MyNotepad respectively, and test run.
Note: It is also possible to only compile MyNotepad, because the associated MyUtil will be automatically compiled.

OK, the program is basically clear, and the generated files are still in the same directory. I want to put this tool class or helper function in a separate folder and use it as a class library.
We continue to expand:
the current directory is c:\work\001
We create new directories util and main in the current directory and
then put MyUtil.java into the util directory and MyNotepad.java into the main directory

We call the directory c:\work\001 the project root directory, and the program should run in the project's root directory, because CLASSPATH is set to search for class files from the current path (.).
When compiling and executing, we need to add the directory name to the class name, as follows:
javac main\MyNotepad.java
java main.MyNotepad

don't run it yet, it's not finished yet.

You can see that javac is followed by the path of the file, so the Windows directory format is used to locate the file.
java is followed by the class name, because the Java program automatically loads the .class class file.
The directory name before the class name is called the package name, which prevents the problem that the same files in different directories cannot be located.

So the official class name is the package name. The class name

So we want to declare the package (directory) where we are in the program, and modify MyNotepad.java as follows:


Note: At the same time, the call to MyUtil should also use the full name

to modify MyUtil as follows:

Compile, run:
javac main\MyNotepad.java
java main.MyNotepad a.txt
Output: Hello a.txt

Observe the call to util.MyUtil in MyNotepad.java. If there are many calls to util.MyUtil, we need to write the full name of util.MyUtil every time.
Considering the simplicity of the program, I want to write only MyUtil, so I need to add a declaration at the head of the file:
import util.*

means to import all the class files under the util package (directory),

so the program becomes like this:

OK, now we don't need to care about the implementation of the output of System.out.println at all, just make function/method calls.

Continue to expand our program:
For the following statement
MyUtil.println("Hello "+args[0]);
we can modify it to:
String message = "Hello "+args[0];
MyUtil.println(message);

in the above In the statement, we complete the declaration and assignment of variables, that is, declare the variable message as String type, and assign the result of "Hello "+args[0] to message.

The format of variable declaration is as follows:
variable type variable name;

the format of variable assignment is as follows:
variable name = value;

where variable declaration and assignment can be written together:
variable type variable name = value;

Java has three types, basic types, Custom type, String
Note: Because String is special and commonly used, we temporarily propose it as a type.
The basic types include: char, boolean, byte, short, int, long, float, double
Among them, we only need to pay attention to: char, boolean, int (all lowercase)
The custom type is the class name declared by the class, which defaults to uppercase letters At the beginning, it includes many system classes, which are not listed here.

So the types we focus on temporarily are: String, char, boolean, int, custom classes (including many system classes)
Our program becomes like this:

I have added a method to read files in MyUtil, which is defined as follows:
public static String getFileContent(String fileName)
He can read the file in the current directory and return the file content according to the incoming file name.
We don't need to care about the specific implementation, we just need to call it. The class file can be downloaded from here.

So we complete the goal of this chapter and modify the program as follows:

Put a.txt under c:\work\001, compile and run the program as follows:
javac main\MyNotepad.java
java main.MyNotepad a.txt
output: abc

simple? Yes, because operations such as reading files and outputting content are encapsulated into tool classes, so here, you only need to master the environment construction, operation, variable declaration, assignment and function invocation.
Don't worry about what I encapsulated in MyUtil for the time being, how to write it, whether it is correct or not.
You just need to know how to call, what parameters to pass in, and what results to return, and that's it.
Because program manufacturing is a huge project that requires teamwork, it is impossible for a programmer to understand everything, so you need to care more about the correctness of your program, input and output, and how to call the interface provided by others, not care about all.
It is important to ensure that you fully understand the programs within your scope, and that you write programs with elegance and simplicity.

Note: This tutorial only talks about the main content and learning methods of Java learning. If you are struggling with the knowledge involved in this tutorial, please supplement the relevant knowledge points by yourself during the reading process.

Copyright statement: The copyright of this tutorial belongs to java123.vip, and any form of reprinting and citation is prohibited.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325163614&siteId=291194637