Study guide: The ins and outs of standard input/output
Brush up on Java terms, learn tips and cautions, review homework assignments, and read Jeff’s answers to student questions
Glossary of terms
- standard error device
- That part of the operating system that controls where a program sends error messages.
- standard I/O
- A standardized input/output mechanism that originates from the Unix operating system. It uses devices for inputting and outputting data.
- standard input device
- That part of the operating system that controls from where a program receives its input.
- standard output device
- That part of the operating system that controls where a program sends its output.
Tips and cautions
These tips and cautions will help you write better programs and save you from agonizing over why the compiler produces error messages.
Tips
- When using
java.exe
to run an application, you can redirect standard output, so that output arrives at a file instead of the screen. You accomplish that task by specifying the greater-than-sign (>) character followed by a filename afterjava.exe
on a command line. - When using
java.exe
to run an application, you can redirect standard input, so that input originates from a file instead of the keyboard. You accomplish that task by specifying the less-than-sign (java.exe on a command line. - Use
System.err
instead ofSystem.out
for outputting error messages. After all, a user might become confused if a problem occurs in a program and no error message appears on the screen because the message is sent to the standard output device — which has been redirected from the screen to a file.
Cautions
System.in.read()
does not return user-entered integer numbers. Instead, it returns either a key’s 7-bit ASCII code or a file’s 8-bit byte.System.in.read()
can throw exceptions.
Homework
Because the last several articles have covered a lot of detailed material, this shorter article provides a brief respite. As an added bonus, there is no homework.
Answers to last month’s homework
Last month, I presented the first of several Java 101 quizzes. Each quiz is designed to get you thinking about concepts introduced in the Java 101 course and to help prepare you for certification. This month, those questions are answered. Check out the following answers to see how well you’re doing!
- Write an application that prints out its command line arguments.
-
To answer that question, Listing 6 presents source code to an application called
PrintArgs
.Listing 6. PrintArgs.java
// PrintArgs.java class PrintArgs { public static void main (String [] args) { for (int i = 0; i < args.length; i++) System.out.println (args [i]); } }
To run
PrintArgs
, issue a command line likejava PrintArgs First Second Third
. - What is wrong with the
java calculator 4 * 3
command line, and why? -
The complier interprets the asterisk character,
*
, as a wildcard character — a character that represents all files in the current directory whenjava
runs — instead of an asterisk symbol. As such, the command line arguments passed tocalculator
are not4
,*
, and3
. Instead, the command line arguments are4
, a list of file names for all files in the directory from wherejava
runs, and3
. To fix that problem, surround the asterisk with double-quote characters (e.g.,java calculator 4 "*" 3
). - What is a comment, and what kinds of comments does Java support?
-
A comment is nothing more than source code documentation set apart from program source code by the use of certain symbols. Java supports multiline, single-line, and doc comments.
- True or false: multiline comments can be nested.
-
False: multiline comments cannot be nested.
- What is an identifier, and what criteria does Java use to determine which identifiers are legal?
-
An identifier is a sequence of characters that names a variable, method, class, interface, or package. Java classifies a legal identifier as a sequence of characters beginning with a letter, a currency character, or a connecting character, and continuing with letters, digits, currency characters, and connecting characters.
- Are
true
,false
, andgoto
considered keywords? -
true
,false
, andgoto
are reserved words because they cannot be used to name variables, methods, and so on. However, onlygoto
is considered a keyword becausetrue
andfalse
represent literals; and keywords are not also literals. - What is the purpose of a data type?
-
A data type describes the internal binary representation of a data item along with any operations that can be legally applied to that representation.
- List the names of Java’s primitive data types.
-
The names of Java’s primitive data types are Boolean, byte integer, character, double-precision floating-point, floating-point, integer, long integer, and short integer. These data types are specified in source code by using Java’s
boolean
,byte
,char
,double
,float
,int
,long
, andshort
keywords. - True or false: Java’s various integer data types can be used to represent both signed and unsigned numbers.
-
True: Java’s various integer data types represent both signed and unsigned numbers. For example, a byte integer data type can represent an integer ranging from -128 to 127 (inclusive).
- What is the default data type of a floating-point literal?
-
The default data type of a floating-point literal is double-precision floating-point.
- Is a value of a primitive data type considered to be an object?
-
A value of a primitive data type is not considered an object. Note: A literal string is not a value of a primitive data type. Instead, it represents an object.
- What is the difference between a primitive data type and a reference data type?
-
A primitive data type is a predefined data type named by a reserved word. Values of primitive data types are not objects. In contrast, a reference data type is a developer-defined data type — a class. Objects can be created from reference data types.
- What is a variable?
-
A variable is a storage location that contains a data item.
- Declare a pair of integer nonarray variables (in one declaration) and initialize one variable to the literal 1.
-
int i, j = 1;
- How do you obtain the length of an array variable?
-
You obtain the length of an array variable by specifying
.length
. For example, assuming you have an array calledanimals
,animals.length
returnsanimal
‘s length (also known as number of array elements). - What is the difference between an array variable and a nonarray variable?
-
An array variable contains multiple storage locations, where an integer number represents each storage location. A single data item can be stored in each storage location. In contrast, a nonarray variable consists of a single storage location, and only one data item can be stored there.
- What is a constant, and how does it differ from a variable?
-
Unlike variables, which can be read from and written to, a constant is a read-only variable. Specify it by prefixing the keyword
final
to the variable’s data type. - Declare a constant for the mathematical concept of PI.
-
final double PI = 3.14159;
- What is an operator?
-
An operator is a combination of characters that identifies a data operation, such as addition.
- What is the name given to the entity or entities that are operated on by an operator?
-
The name given to the entity or entities that are operated on by an operator is operand.
- If an operator operates on only one entity, that operator is known as what?
-
A unary operator operates on only one entity.
- If an operator operates on a pair of entities, that operator is known as what?
-
A binary operator operates on a pair of entities.
- How many ternary operators does Java have? Please identify those operators.
-
Java only has one ternary operator known as the conditional operator.
- What is an overloaded operator?
-
An overloaded operator is an operator that performs different operations based on its operands’ data types. For example, Java’s
+
operator is used to perform addition when its operands are numeric or string concatenation when one or both operands are strings. - Does Java provide any overloaded operators?
-
Java’s addition/string concatenation operator,
+
, is an example of an overloaded operator. Furthermore, the bitwise AND/Boolean AND operator,&
; bitwise inclusive OR/Boolean inclusive OR operator,|
; and bitwise exclusive OR/Boolean exclusive OR operator,^
, are overloaded. There are a few other overloaded operators, but I’ll leave it as an exercise for you to find them. - Can a developer overload an operator?
-
Unlike C++, Java doesn’t allow developers to overload operators.
- What kind of operator is the minus sign?
-
The minus sign is a unary operator when used for negation (e.g.,
-6
) and a binary operator when used for subtraction (e.g.,5 - 3
). - What symbols does Java use to represent the bitwise AND, conditional, and reference data type-checking operators?
-
Java uses
&
to represent bitwise AND,? :
to represent conditional, andinstanceof
to represent reference data type-checking. - What is the difference between the predecrement and postdecrement operators?
-
The predecrement operator decrements its operand’s value by one and then returns the new value. In contrast, the postdecrement operator returns its operand’s value before incrementing the operand value. (Java accomplishes that task by storing the operand’s value in a temporary memory location, incrementing the operand’s value, and returning the original value from the temporary memory location.)
- What happens when 6.3 is divided by 0.0?
-
When 6.3 is divided by 0.0 (as in
6.3 / 0.0
), a special value representing+Infinity
returns. - What is the difference between the logical AND and Boolean AND operators?
-
The logical AND operator,
&&
, evaluates its left operand. If the value of that operand is false,&&
doesn’t evaluate the right operand. That behavior is known as short circuiting. In contrast, the Boolean AND operator,&
, evaluates both operands. - What is the fastest operator for multiplying by powers of 2?
-
The fastest operator for multiplying by powers of 2 is the bitwise shift left operator. For example,
x = x << 1;
is the same asx = x * 2;
. - What is the purpose of a cast operator?
-
The cast operator converts its operand’s data type to another data type.
- Is
byte b = 40; b = b | 0x31;
legal Java code? Why or why not? -
The above code is not legal because
0x31
is an integer literal andb
is a byte integer.b | 0x31
returns a value of type integer. Because an integer value is 32 bits and a byte integer variable can only hold 8 bits, some bits will be lost when converting from integer to byte integer. Whenever bits will be lost, a cast operator is required. To fix the above code, specifyb = (byte) (b | 0x31);
. - What is an expression?
-
An expression is a sequence of operators and operands — constructed according to the Java language’s syntax — that evaluates to a specific data type’s single value.
- What is precedence, and how can precedence be changed?
-
Precedence is a predefined evaluation order. Parentheses are used to change precedence. For example,
6 + 3 * 2
first evaluates3 * 2
and then adds that result to6
. The final result is12
. You can change the precedence so that multiplication follows addition by using parentheses as follows:(6 + 3) * 2
. Now,6 + 3
evaluates first. The final result is18
. - What is the difference between the expression
'A' + 6
and the expression"A" + 6
? -
The expression
'A' + 6
evaluates to the integer value71
. The expression"A" + 6
evaluates to the stringA6
. - What is a statement?
-
A statement is a source code construct that declares one or more variables, assigns an expression’s result to a variable, repeatedly executes other statements in a loop, controls a loop, makes a decision, and so on.
- What is a block?
-
Block is a synonym for compound statement — zero or more statements surrounded by brace characters.
- Explain what is meant by the dangling-else problem.
-
The dangling-else problem refers to improperly matching
else
s to the appropriateif
s in decision statements. - True or false: a switch statement can be specified with a numeric expression that evaluates to long integer.
-
False: It is illegal to specify a long integer numeric expression in a switch statement. That is probably due to the Java Language Specification treating long integers as two variables of 32 bits each. Because of that fact, a JVM doesn’t have to read or write a long integer variable in a single 64-bit atomic operation. Instead, it can use two 32-bit operations. If long integer numeric expressions could be used with switch statements, imagine what would happen if two threads of execution are running and a second thread writes a new value to the long integer between the first read and the second read. Chaos would result!
- What is the difference between an entry condition and an exit condition loop?
-
Loops evaluate Boolean expressions to determine whether they keep iterating. An entry condition loop (such as a while loop) evaluates the Boolean expression before executing the body of the loop. In contrast, an exit condition loop (such as a do-while loop) evaluates the Boolean expression after executing the body of the loop.
- True or false: a do-while loop is known as an entry condition loop.
-
False: a do-while loop is known as an exit condition loop because the Boolean expression is evaluated after the loop’s body has executed.
- What is wrong with
int i = 0; while (true); System.out.println (i++);
-
The semicolon in
while (true);
forms the problem in the code above. If that semicolon is left as is, the loop is infinite and does absolutely nothing. Even after you remove the semicolon, the loop is still infinite — though that loop can print out values ofi
. However, the presence of the infinite loop, which cannot be terminated (apart from pressingCtrl+C
), still constitutes a problem. - How can you specify a for loop that never stops iterating — an infinite loop?
-
for (; ;) System.out.println ("Looping forever");
- True or false: the first and third clauses in a for loop can consist of more than a single expression.
-
The answer is true, which the following code illustrates:
for (int i = 2, j = 3; i < 10; i++, j++) System.out.println (i + ", " + j);
- True or false: the first (initialization) clause in a for loop executes once for each iteration.
-
False: the for loop’s initialization clause executes once for the entire loop.
- What is the purpose of a labeled break statement?
-
A labeled break statement causes execution to break out of nested loops. It is also used to break out of nested switch statements or combinations of nested loops/nested switch statements.
- What is the purpose of a labeled continue statement?
-
A labeled continue statement terminates the current iteration of either a single loop or several loops (in a nested situation).
- Write a Java application that reads a floating-point number from standard input, assumes that number represents a temperature in degrees Celsius, converts that temperature to Fahrenheit, and outputs the result.
-
Listing 7 provides the answer by presenting source code to an application called
C2F
.Listing 7. C2F.java
// C2F.java class C2F { public static void main (String [] args) throws java.io.IOException { System.out.print ("Please enter a temperature in degrees Celsius: "); double celsius = 0.0; boolean inDecimal = false; int numDecimalDigits = 0; int ch; while ((ch = System.in.read ()) != 'n') if (ch >= '0' && ch <= '9') { celsius *= 10; celsius += ch - '0'; if (inDecimal) numDecimalDigits++; } else if (ch == '.') { if (inDecimal) { System.err.println ("Too many decimals."); return; } inDecimal = true; } else break; if (numDecimalDigits != 0) celsius /= Math.pow (10, numDecimalDigits); // Note: Math.pow calculates 10 to the power of numDecimalDigits. // I'll have more to say about Math.pow in an upcoming article. System.out.println ("Degrees Fahrenheit = " + (9.0 / 5.0 * celsius + 32.0)); } }
C2F
handles a number’s fractional part by counting the number of digits following the decimal point. After the last digit has been read, and if the count is not zero, the number is divided by 10 times that count.C2F
isn’t perfect! What happens if you enter a single decimal point? I’ll leave it as an exercise for you to figure out what to do about that situation.