Hello World
According to international practice, your first program must say hello to the world, so you can write an e-mail, an article, a poem, and use your computer's editor to enter the following English letters. Then save as Heloword.java
Public class HelloWorld { publicstaticvoid main (string[] args) { System.out.println ("Hello, World");} }
Compile
Maybe at first you might think that this piece of code is for a computer, but it's all written to you, right, right. is written for people to read, so please write the more simple and clear no ambiguity the better OH. In fact, to make the computer understand must execute the following command, it will translate Helloword.java content into the computer's language and save as Helloword.class. This process is called compiling, that is, the Java compiler will translate the source code to look at the computer to see.
% Javac Helloworld.java
Run
If you didn't hit any of the wrong words, you didn't find anything. Then go on.
% Java HelloWorld
you will see a Java program say hello to the world.
Hello, World
The key to this simple Java program is SYSTEM.OUT.PRINTLN ("Hello, World"), this sentence. It sends the message "Hello,world". Of course there are a lot of things we don't understand, including public , class , main , string[] , args ,
Type of error
Perhaps not so well, you may encounter some mistakes, do not worry, generally you carefully examine your code and the code in the example of the difference, you should be able to find the problem.
The difference between what kind of error, can let us in the back to find the wrong time (is the legendary debug), become so happy to lose.
Compile-time error compile-time errors, which occurs when the compiler compiles a program that prevents the compiler from translating the program.
Run-time error run-time errors, which occurs when executing a program, is a program that attempts to perform an illegal operation, such as except 0.
Logic error logical errors, this error is the result of the program after the execution and you want the result is not the same AH. The nightmare of the yard farm Oh.
Input and output
Now let's add a little bit of interesting stuff. Use parameters to interact with the program a little bit.
Public class useargument { publicstaticvoid main (string[] args) { System.out.print ("Hi,"); System.out.print (args[0]); System.out.println (". How is it? " ); }}
% javac Useargument.java% java useargument Davidhi, David. How is you?% java useargument Peterhi, Peter. How is it?
Space, tab. Line break
The Java compiler treats these symbols as a sentence, ignoring them all. You can omit all line breaks, ta keys. But as we say, it's something to show. Not to the machine. Let's put the program in a good format.
Double quotes
Double quotes are used to tell the Java compiler that between this is what I want, they are a whole, a string. No matter how many spaces you add to the double quotation marks, the compiler will take a single full charge, rather than ignoring it like the rules above
Start the Java Tour