Home > Articles

  • Print
  • + Share This
This chapter is from the book

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. Do I really have to type in the programs in the book to try them out? I believe that you ran them and that they produce the indicated output.

A. Everyone should type in and run HelloWorld. Your understanding will be greatly magnified if you also run UseArgument, try it on various inputs, and modify it to test different ideas of your own. To save some typing, you can find all of the code in this book (and much more) on the booksite. This site also has information about installing and running Java on your computer, answers to selected exercises, web links, and other extra information that you may find useful while programming.

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. What is the meaning of the //, /*, and */ character sequences in the code?

A. They denote comments, which are ignored by the compiler. A comment is either text in between /* and */ or at the end of a line after //. Comments are indispensable because they help other programmers to understand your code and even can help you to understand your own code in retrospect. The constraints of the book format demand that we use comments sparingly in our programs; instead we describe each program thoroughly in the accompanying text and figures. The programs on the booksite are commented to a more realistic degree.

Q. What are Java’s rules regarding tabs, spaces, and newline characters?

A. Such characters are known as whitespace characters. Java compilers consider all whitespace in program text to be equivalent. For example, we could 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 indent paragraphs and lines consistently when we write prose or poetry.

Q. What are the rules regarding quotation marks?

A. Material inside double quotation marks is an exception to the rule defined in the previous question: typically, characters within quotes are taken literally so that you can precisely specify what gets printed. If you put any number of successive spaces within the quotes, you get that number of spaces in the output. If you accidentally omit a quotation mark, the compiler may get very confused, because it needs that mark to distinguish between characters in the string and other parts of the program.

Q. What happens when you omit a curly brace or misspell one of the words, such as public or static or void or main?

A. It depends upon precisely what you do. Such errors are called syntax errors and are usually caught by the compiler. For example, if you make a program Bad that is exactly the same as HelloWorld except that you omit the line containing the first left curly brace (and change the program name from HelloWorld to Bad), you get the following helpful message:

% javac Bad.java
Bad.java:1: error: '{' expected
public class Bad
                ^
1 error

From this message, you might correctly surmise that you need to insert a left curly brace. But the compiler may not be able to tell you exactly which mistake you made, so the error message may be hard to understand. For example, if you omit the second left curly brace instead of the first one, you get the following message:

% javac Bad.java
Bad.java:3: error: ';' expected
   public static void main(String[] args)
                                         ^
Bad.java:7: error: class, interface, or enum expected
}
^
2 errors

One way to get used to such messages is to intentionally introduce mistakes into a simple program and then see what happens. Whatever the error message says, you should treat the compiler as a friend, because it is just trying to tell you that something is wrong with your program.

Q. Which Java methods are available for me to use?

A. There are thousands of them. We introduce them to you in a deliberate fashion (starting in the next section) to avoid overwhelming you with choices.

Q. When I ran UseArgument, I got a strange error message. What’s the problem?

A. Most likely, you forgot to include a command-line argument:

% java UseArgument
Hi, Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 0
        at UseArgument.main(UseArgument.java:6)

Java is complaining that you ran the program but did not type a command-line argument as promised. You will learn more details about array indices in SECTION 1.4. Remember this error message—you are likely to see it again. Even experienced programmers forget to type command-line arguments on occasion.

  • + Share This
  • 🔖 Save To Your Account