|
Programming in Java: We break the process of programming in Java into three steps: - Create the program by typing it into a text editor and saving 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 (and puts the result in a file named MyProgram.class); the third actually runs the program. 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.
One of the very first skills that you will learn is to identify errors; one of the next will be to be sufficiently careful when coding to avoid many of them.Q + A Q. Why Java? A. The programs that we are writing are very similar to their counterparts in several other languages, so our choice of language is not crucial. We use Java because it is widely available, embraces a full set of modern abstractions, and has a variety of automatic checks for mistakes in programs, so it is suitable for learning to program. There is no perfect language, and you certainly will be programming in other languages in the future. Q. What are Java's rules regarding tabs, spaces and newline characters? A. There are not many. Java compilers treat them all to be equivalent. For example, we could also write HelloWorld as follows: public class HelloWorld { public static void main ( String [] args) { System.out.println("Hello World") ; } }
| But we do normally adhere to spacing and indenting conventions when we write Java programs, just as we always indent paragraphs and lines consistently when we write prose or poetry. 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 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. What happens when you omit a brace or misspell one of the words, like public or static or main? A. It depends upon precisely what you do. Such errors are called syntax errors. Try it out and see. 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. Exercises - Write a program TenHelloWorlds.java that prints "Hello, World" ten times.
- Describe what happens if, in HelloWorld.java, you omit
- public
- static
- void
- args
- Describe what happens if, in HelloWorld.java, you misspell (by, say, omitting the second letter)
- public
- static
- void
- args
Reference: Department of Computer Science of Princeton university. |