Rookie notes-Chapter 3.5 The first Java program in life Hello World

Source: Internet
Author: User
Tags class definition

3.5 First Java program "HelloWorld" 3.5.1 Tools Introduction

Write Java programs can use a variety of tools

L Notepad ( Microsoft OS comes with )

L notepad++

L Eclipse

L MyEclipse

We start with the initial learning phase notepad++ to learn, so that we can more intuitively see the process of running the program.

3.5.2 HelloWorld

Source

class helloworld{  publicstaticvoid  main (string[] args) {System.out.print (" Hello world; "    ); System.out.println ("Hello world;"  ); }}

Store the source code in the K:\WorkSpace\JavaJC\Chapter 1

at this point we open the command line and define the Directory of the JDK,

At this point we compile the program will find

javac.exe Span style= "FONT-FAMILY:CALIBRI;" >bin Not in current bin , We will need to put the compiled file in the current bin directory, RECOMPILE discovery hint encoding error error, This is what we can GBK javac-encoding utf-8 Helloword.java , compiled successfully-meaning to encode helloword.java using the coded character set Utf-8, and then compile to execute

run the class that we compiled successfully

Here we should note that when we change the source file, we run the executable . Class , if not compiled, then the change is not valid, simple example: when we will Helloworld.java Add an input statement System.out.print ("an example that is not compiled "),we do not compile, run the executable directly,

The output is the last time we compiled the rendering, we recompile, re-execute, this change will not be shown, this is because we write the source code and the JVM executes the middle of the middle code to go through a compilation.

3.5.3 Program parsing
classdemo{/*first, define a class class name that will be the future of our compiled bytecode file name after the class definition with a pair of curly braces {}*/ /*Add a main (main) method/function in the middle of the brace public static void Main (String [] args) {} Main is the method defined for the class, when the class is the main class in the package, it must have the main method, the main method is the entry of the program, When the program executes, it is called first. Then the program is completed by invoking another class and method in the Main method. */ Public Static voidMain (string[] args) {/*add two lines of output statement System.out.print ("Hello World") in the middle of the curly brace of the main method. System.out.println ("Hello World"); LN is the difference between a carriage return and an ln representing a carriage return .*/System.out.print ("Hello World"); System.out.println ("Hello World");}}

Our compilation is still available when we do not enter the main method, but we report an error when executing the executable file

Main is the entrance to the program, as if we were going home to open the door, FQ would be beaten.

3.5.4 operation and working principle

3.5.5 Common errors

L File extension shadowing causes compilation to fail

L class write Wrong class Claas ...

L Class name format problem temporarily all use English

The curly braces that follow the L class name are not matched correctly

L main method format error public static void Main (String [] args) {}

L main method Curly brace missing

L Print statement spelling error System.out.println ("");

L use errors in quotation marks, use Chinese full-width quotation marks

if the executable file cannot be built after compilation you can usually follow the steps below to find out where the problem lies:

1. first check that the JDK environment is configured successfully

Practice: DOS Input:java-version, See if there is a jdk version of the information,Java, see if there are relevant commands; Javac to see if there was such an order; 3 a command that will JDK environment configuration has an intuitive understanding, if not the normal display of relevant information, it is necessary to configure JDK environment, the specific steps can refer to Baidu experience.

2. Check if the path is correct

Practice: DOS Input:dir, see if there is really this helloworld.java file under the current path

3. through the command javac-d you want to generate the class file directory Helloworld.java to see if there are class file

Practice: DOS Input:javac-d \ C Helloworld.java , after running, see if There are helloworld.class files under C disk

Rookie notes-Chapter 3.5 The first Java program in life Hello World

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.