Your First Java Program
来源:互联网 发布:淘宝 东京时光隧道 编辑:程序博客网 时间:2024/05/18 02:06
- Create the program by typing it into a text editor andsaving it to a file named, say, MyProgram.java.
- Compile it by typing "javac MyProgram.java"in the terminal window.
- Run (or execute) it by typing "java MyProgram"in the terminal window.
The first step creates the program; the second translates it into a language more suitable for machine execution (andputs the result in a file named MyProgram.class); the third actually runs the program.
- Creating a Java program. A program is nothing more than a sequence of characters, like a sentence, a paragraph, or a poem. To create one, we need only define that sequence characters using a text editor in the same way as we do for e-mail. HelloWorld.java is an example program. Type these character into your text editor and save it into a file named HelloWorld.java.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); }}
- Compiling a Java program. At first, it might seem to you as though the Java programming language is designed to be best understood by the computer. Actually, to the contrary, the language is designed to be best understood by the programmer (that's you). A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer. It takes a text file with the .java extension as input (your program) and produces a file with a .class extension (the computer-language version). To compile HelloWorld.java type the boldfaced text below at the terminal. (We use the % symbol to denote the command prompt, but it may appear different depending on your system.)
If you typed in the program correctly, you should see no error messages. Otherwise, go back and make sure you typed in the program exactly as it appears above.% javac HelloWorld.java
- Executing a Java program. Once you compile your program, you can run it. This is the exciting part, where the computer follows your instructions. To run the HelloWorld program, type the following at the terminal:
% java HelloWorld
If all goes well, you should see the following response
Hello, World
- Understanding a Java program. The key line with System.out.println() send the text "Hello, World". When we begin to write more complicated programs, we will discuss the meaning of public, class, main, String[], args, System.out, and so on.
- Creating your own Java program. For the time being, all of our programs will be just like HelloWorld.java, except with a different sequence of statements in main(). The easiest way to write such a program is to:
- Copy HelloWorld.java into a new file whose name is the program name followed by .java.
- Replace HelloWorld with the program name everywhere.
- Replace the print statement by a sequence of statements.
Errors.
Most errors are easily fixed by carefully examining the program as we create it, in just the same way as we fix spelling and grammatical errors when we type an e-mail message.- Compile-time errors. These errors are caught by the system when we compile the program, because they prevent the compiler from doing the translation (so it issues an error message that tries to explain why).
- Run-time errors. These errors are caught by the system when we execute the program, because the program tries to peform an invalid operation (e.g., division by zero).
- Logical errors. These errors are (hopefully) caught by the programmer when we execute the program and it produces the wrong answer. Bugs are the bane of a programmer's existence. They can be subtle and very hard to find.
Input and output.Typically, we want to provide input to our programs: data that they can process to produce a result. The simplest way to provide input data is illustrated in UseArgument.java. Whenever this program is executed, it reads the command-line argument that you type after the program name and prints it back out to the terminal as part of the message.
% javac UseArgument.java% java UseArgument AliceHi, Alice. How are you?% java UseArgument BobHi, Bob. How are you?
Q + A
Q. What is the meaning of the words public, static and void?
A. These keywords specify certain properties of main() that you will learn about later in the book. For the moment, we just include these keywords in the code (because they are required) but do not refer to them in the text.
Q. Can a program use more than one command-line argument?
A. Yes, you can put several, though we normally use only a few. You refer to the second one as args[1], the third one as args[2], and so forth. Note that we start counting from 0 in Java.
Q. What exactly is a .class file?
A. It's a binary file (sequence of 0s and 1s). If you are using Unix or OS X, you can examine its contents by typing od -x HelloWorld.class at the command prompt. This displays the results in hexadecimal (base 16). In deference to Java's name, the first word of every .class file is cafe.
- Your First Java Program
- Day4-11.Your first Java program
- Writing Your First Program
- Your First OpenGL Program
- Compiling Your First VTK Program
- My First Java Program...
- my first java program
- Java First program
- VBA Tips:Your first VBA program
- 《算法》第四版 Your First Java Program: Hello World——exercise 记录
- YOUR FIRST PYTHON PROGRAM 从第一个PYTHON程序开始
- Why Python to be your first program language?
- Running your first RTEMS program on the Raspberry Pi
- Starting your first golang program. 编写golang程序的"Hello World!"
- My First RMI Program
- The first QT Program !
- first program by c
- MY FIRST C++ PROGRAM
- 使用grep搜索文件内容
- MVC学习1
- 一些集合面试题
- docker常用的命令
- ant使用javac编译时出现的一个问题
- Your First Java Program
- sql语法记录 to_date函数和oracle连接写法
- poj2891
- QT 5 初学1 多窗口切分
- wget命令
- 学习笔记2 Supervised Learning and Optimization 之 Softmax Regression
- grep语法和正则表达式
- 3-1MapReduce原理,原理图要记住
- RESTful API 设计指南