Java basic textbook series (two) - the first to write a java program using the command line

HelloWorld textbook series (two) - the first to write a java program using the command line

Click here to get the most complete java 2019 study Raiders,, sign up for a new eruption from white to Daniel
java so just learn it! ! ! , Sign up for

Truly develop java applications are at work to use eclipse, myeclipse, IntelliJ , etc.

But still worth spending 10 minutes learning how to use the most primitive form of the command line to compile and execute java program

Using the most primitive command line to execute Hello World
look at the operating results: Step 1
Step 2: Prepare your project directory
Step 3: Create the first java source files
Step 4: Display Extension
Step 5: Compile
Step 6: Run
Step 7 : About class
step 8: method main
step 9: console output

Step 1: Look at the operating results

In the first console to run a Java program, you can see the output string
hello world
Here Insert Picture Description

Step 2: Prepare your project directory

Usually in e: Create a project directory
in this example, we use e: / project / j2se project directory as a directory
of all the java code will be placed in the project directory
and create a src directory for storing source code java .java file
Here Insert Picture Description

Step 3: Create the first java source files

Create the first java source files
in the blank space of the screen, click on the right mouse button -> New -> Text file to get a file " New Text Document .txt "
right of the file -> rename HelloWorld.java
in the file type in the following Code

public class HelloWorld{
  public static void main(String[] args){
    System.out.println("hello world");
  }
}

Because java is object-oriented programming language, so we are actually creating one of the class
class HelloWorld indicates that the name of the class is the HelloWorld
public static void main (String [] args) This is the main method , all entrance code
if you c learned language, corresponding to the program entry _main
System.out.println ( "hello world"); represents the console (black screen) outputs a character string "hello world"

public class HelloWorld{
  public static void main(String[] args){
    System.out.println("hello world");
  }
}

Step 4: Display extension

Sometimes the system will default to hide extensions, extensions can show the following manner, so as to modify the .txt .java to
open My Computer -> F10 displays the menu bar -> Tools -> Folder Options -> View - > remove the name Hide extensions for known file types check on
Here Insert Picture Description

Step 5: Compile

.java file is java source files, but can not run directly and must first be compiled to be able to perform file .class
java javac command to compile using the
syntax:
javac filename.java

NOTE: keep .java extension must be
performed as the following command:
Run win + r, input into the console interface cmd
e: e is the drive letter switching disc
cd e: \ project \ j2se \ src handover source directory to the directory
javac HelloWorld.java compilation command javac file name must be the same case
If you get a blank line , it means success, and get a class file: HelloWorld.class
Note: E switching the first line of the letter: Do not forget
Here Insert Picture Description

Step 6: Run

The command is run java
java classname

FIG Run the following commands:
run win + r, cmd input into the console interface
e: e is the drive letter switching disc
cd e: \ project \ j2se \ src handover source directory to the directory
java HelloWorld
attention to the case needs to be consistent, and there is no .class extension
will see the string "hello world" after successful operation
Here Insert Picture Description

Step 7: About class

All java class code is run inside

public class HelloWorld

public that this is a publicly accessible class
class indicates that this is a class
HelloWorld name represents the class, the first letter of each word capitalized

public class HelloWorld{
 
}

Step 8: Method main

public static void main(String[] args)

You write a lot of code, there is always the first line of code is executed, which is the main method
args represents operating parameters, not used to in this case.

public static void main(String[] args){
  System.out.println("hello world");
}

Step 9: console output

System.out.println("hello world");

Console output string will
println represents print data to the console, and the line feed

System.out.println("hello world");

More details, click here

Published 32 original articles · won praise 182 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44092440/article/details/100942687