Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
These tips and cautions will help you write better programs and save you from agonizing over why the compiler produces error messages.
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 after java.exe on a command line.
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 (<) character followed by a filename after java.exe on a command line.
System.err instead of System.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.
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.
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.
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!
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 like java PrintArgs First Second Third.
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 when java runs -- instead of an asterisk symbol. As such, the command line arguments passed to calculator are not 4, *, and 3. Instead, the command line arguments are 4, a list of file names for all files in the directory from where java runs, and 3. To fix that problem, surround the asterisk with double-quote characters (e.g., java calculator 4 "*" 3).
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.
False: multiline comments cannot be nested.
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.
true, false, and goto considered keywords?
true, false, and goto are reserved words because they cannot be used to name variables, methods, and so on. However, only goto is considered a keyword because true and false represent literals; and keywords are not also literals.
A data type describes the internal binary representation of a data item along with any operations that can be legally applied to that representation.
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, and short keywords.
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).
The default data type of a floating-point literal is double-precision floating-point.
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.
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.
A variable is a storage location that contains a data item.
int i, j = 1;
You obtain the length of an array variable by specifying .length. For example, assuming you have an array called animals, animals.length returns animal's length (also known as number of array elements).
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.
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.
final double PI = 3.14159;
An operator is a combination of characters that identifies a data operation, such as addition.
The name given to the entity or entities that are operated on by an operator is operand.
A unary operator operates on only one entity.
A binary operator operates on a pair of entities.
Java only has one ternary operator known as the conditional 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.
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.
Unlike C++, Java doesn't allow developers to overload operators.
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).
Java uses & to represent bitwise AND, ? : to represent conditional, and instanceof to represent reference data type-checking.
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.)
When 6.3 is divided by 0.0 (as in 6.3 / 0.0), a special value representing +Infinity returns.
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.
The fastest operator for multiplying by powers of 2 is the bitwise shift left operator. For example, x = x << 1; is the same as x = x * 2;.
The cast operator converts its operand's data type to another data type.
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 and b 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, specify b = (byte) (b | 0x31);.
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.
Precedence is a predefined evaluation order. Parentheses are used to change precedence. For example, 6 + 3 * 2 first evaluates 3 * 2 and then adds that result to 6. The final result is 12. 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 is 18.
'A' + 6 and the expression "A" + 6?
The expression 'A' + 6 evaluates to the integer value 71. The expression "A" + 6 evaluates to the string A6.
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.
Block is a synonym for compound statement -- zero or more statements surrounded by brace characters.
The dangling-else problem refers to improperly matching elses to the appropriate ifs in decision statements.
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!
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.
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.
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 of i. However, the presence of the infinite loop, which cannot be terminated (apart from pressing Ctrl+C), still constitutes a problem.
for (; ;) System.out.println ("Looping forever");
The answer is true, which the following code illustrates:
for (int i = 2, j = 3; i < 10; i++, j++)
System.out.println (i + ", " + j);
False: the for loop's initialization clause executes once for the entire loop.
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.
A labeled continue statement terminates the current iteration of either a single loop or several loops (in a nested situation).
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.