In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to write HelloWorld programs with Java". In daily operation, I believe many people have doubts about how to write HelloWorld programs with Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to write HelloWorld programs with Java". Next, please follow the editor to study!
For beginners, the first HelloWorld program is too mysterious and difficult, because you need to learn a lot of operations in the first program, and in the process of operation, even a small mistake can make beginners helpless, so learning the first HelloWorld program is not simple at all, but also requires enough care and patience.
In this section, you will learn the following through the first HelloWorld program:
How to edit code
How to save code
How to compile code
L how to run the program
2.1.4.1 how to edit code
Because JDK does not provide a code editing environment, when using JDK for Java program development, you also need a software to edit the code.
The Java source code can be edited in any text, such as notepad that comes with Windows. Here's how to edit the code in notepad.
The steps to open the notepad program are as follows: start > Program > attachments > notepad.
You can then edit the code inside the newly opened notepad.
Here is a HelloWorld program for Java:
Public class HelloWorld {
Public static void main (String [] args) {
System.out.println ("Hello world!" )
}
}
When editing code, pay attention to the following issues:
Letters in source code are case sensitive
L punctuation marks are all half-width characters, that is, punctuation marks in English input mode.
L indentation is just for beauty. During actual compilation, all spaces at the beginning and end of each line in the code are deleted.
2.1.4.2 how to save code
The source file of the Java language, with the suffix name of java, is case-insensitive and is usually lowercase.
For the file name of the source code, if you use public when declaring in the source code, the format is as follows:
Public class HelloWorld
The name of the source code must be the same as the word after class, that is, it must be HelloWorld, which is case-sensitive. That is, the full name of the file must be HelloWorld.java.
If the source code is declared without using public, the format is as follows:
Class HelloWorld
There is no requirement for the name of the source code, but the suffix name must also be java.
Usually, there is only one declaration in a source code, and the declaration is made into public.
In the actual save, there is no specific requirement for the path to save the source code, that is, it can be saved to any path. For the convenience of subsequent instructions, save the code in the d:\ java\ chapter2 folder, and the subsequent instructions are consistent with the path.
After saving, there will be the file under the corresponding path. The actual icon is related to the software installed on the computer, but this icon does not affect the actual use.
The steps to save the code in notepad are as follows:
L Select File > Save in notepad
L Select the d:\ java\ chapter2 path
L save type set to "all files"
The file name is "HelloWorld.java"
Select the Save button to save.
2.1.4.3 how to compile code
Once the source file is saved, you can use the compilation tools in JDK to compile the program.
As mentioned earlier, compilation converts the source code of a program into an executable file for that program. Executable files in the Java language are files with the suffix class, called class files or bytecode files.
Compile time using the javac.exe file in the bin directory in JDK.
The steps to compile the program from the command line are as follows:
L Open a command prompt window
Select start > programs > attachments > Command prompt.
You can also select start > run, then enter the cmd command in the input box to open the window.
L switch to the source code save directory.
On the command line, enter dvv and press enter to switch to disk d.
Then type cd java\ chapter2 to switch to the path where the source code is saved.
The prompt at the command prompt is "D:\ java\ chapter2 >".
L enter the compile command.
The command format is: full name of javac source file
For example: javac HelloWorld.java
Press enter to execute the command, if there is no prompt, it means that the compilation is successful and the HelloWorld.class file is generated in the directory where the source code is located. If there are a series of prompts, it means that there is a syntax error. You can carefully check whether there is any error in the writing of the code, and whether there is any error in the file name or path.
Note: the full name of the source file is case sensitive.
After the compilation is correct, you get the executable file HelloWorld.class file corresponding to HelloWorld.java.
Description: the parameter description of the javac command, you can enter javac directly in the command prompt window and press enter to view.
Tip: you can set the default path to the command prompt window as follows:
Select start > programs > attachments > Command prompt menu, right-click on the Command prompt icon in attachments to open the Properties menu.
L modify the value of Target in the Shortcut property page in the Command prompt Properties window to the path you need to set.
In this way, when you open the command prompt later, you will switch directly to that path.
2.1.4.4 how to run the program
After the compilation is successful, you can use the run command java.exe under bin in JDK to run the generated program. At run time, the class file is executed.
The steps to run are as follows:
1. Change to the directory where the class file is located.
For the mode of operation, see the instructions in 2.1.4.3
2. Enter the run command
Command format: java class file name (without suffix)
For example: java HelloWorld
Press enter to execute the command.
Note: class file names are case-sensitive and cannot have a class suffix.
In this way, you can see the execution result of the HelloWorld program in the command prompt window, that is, output a line of characters in the command prompt window:
Hello world!
At this point, the study on "how to write HelloWorld programs in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.