[Java tutorial 03] My first Java program, 03 java
Now, everyone should have installed the jdk environment! Have you been eager to try? Now let's write the first java program in our java learning career.
File Settings
In order to make it easier for everyone to learn? One thing you need to set in advance is the File Settings (if you have already done the relevant settings, skip this section)
First, enter a folder. We will find that all the files in the folder have only the file name, but no file belongs to that type of suffix name. It should be long after it is set, let's see how the settings are successfully set.
If you already have a long time, you don't need to set it. It means you have already set it. If you don't have a long time, you need to look at it again!
First, let's take a look at the toolbar at the top of the folder. Is there a tool option? Click it and select the folder Option
Select "View", select "Show Hidden Files", and remove the extensions of hidden known files. Click "OK!
Write code in a text editor
Many of my friends may use a variety of cool integrated IDE development tools when reading code from other programmers. Is it true that black technology is good? Don't envy them. We will become cool in the future. How can we become cool in the future? Cool, don't worry, now we will introduce coding's startup-text editor to directly write code.
What is a text editor? (⊙ O ⊙ )... Yes. Let's just talk about it!
First, select a folder. It is best to create a new folder for storing the code we have written. Right-click the folder and choose new from the shortcut menu. Then, select the text document.
Rename the file name to HelloJava, and change the txt suffix to java. After the name is completed, the prompt "yes" appears.
After the file is created, we will find that the file type changes to Java type. Select the file, right-click the file, select edit, open the text edit box, and enter the following content.
After editing, close the file and return to the directory (that is, the folder) where the file is located. Then, open the command line DOS window in the current directory and enter the command javac HelloJava. java, and then press enter to find that the DOS window of the command line does not respond, but if you take a closer look, you will find that the current directory contains a new one called HelloJava. class files, such
Well, I will not explain what it is, because we will talk about it later. If this step appears, we will continue to input the following command in the DOS window: java HelloJava, press enter, is there a response in the window? There are two more lines of text
These two lines of content are actually the code we just typed in the text editor. This is how to compile and run the code in the text editor!
Write code in JShell Mode
What is jshell? Let me briefly introduce it to you.
Jshell is a new script tool in Java 9, which means that you can directly run java code in the command line without creating a Java file, then compile it, and finally run it. I think the advantage of jshell is that it can only be written. I usually just want to see how the result of several lines of code is running. With jshell, it is much more convenient and can be directly typed on the command line.
How can we use it?
Open the doscommand window, enter the command: jshell, and press Enter. wait a few seconds and you will see a prompt indicating that JShell is successfully started!
Then, how can we use it? For example, enter 1 + 1:
Result output $1 => 2. $1 indicates the first temporary variable.
What if we output the content just now in the text editor?
Let's use it to create a method. What is the specific method? Let's talk about it later. Now let's look at the effect in a simple way.
What should I do if I want to modify the method? Rewrite? You can enter "/edit sum". The editing page is displayed:
Call the modified method. Here j is the variable we defined, so it does not have the "$" symbol.
If we want to see all the scripts we run earlier, we can use "/list" to view
Then, we can use "/import" to view the default imported package of the script. As for what is a package, it is something that needs to be referenced by the current program running.
Finally, enter "/exit" to exit the jshell environment:
Conclusion
Okay. We have finished the two methods. Please try again!
Reprinted Please note: Dumeng Bell» [Java Review 03] My first Java program