
Can a static method take another static method as an argument in Java?Ī. You probably forgot to put StdIn.java in your working directory. What does this error message mean? Exception in thread "main" : StdInĪ. StdIn.isEmpty() allows you to avoid such an error by check- ing whether there is more input available. What happens if my program attempts to read after standard input is exhausted?Ī. You only get one shot at it, in the same way that you cannot undo println(). Can my program reread data from standard input?Ī.
#Princeton mergex api code#
To get an idea of what they are like, look at the code in StdIn.java and StdDraw.java. The Java libraries behind StdIn and StdDraw are built for production programming, and the libraries and their APIs are a bit unwieldy. We are using them, but we prefer to work with simpler abstract models. Why are we not using the standard Java libraries for input and graphics?Ī.

It is printing out the memory address of the array, which, unfortu- nately, is rarely what you want. If a is an array, why does StdOut.println(a) print out a hexadecimal integer, such as, instead of the elements of the array?Ī. Starting indices at 1 would entail either a waste of space at theīeginning of the array or a waste of time to subtract the 1. This convention originated with machine-language programming, where the ad- dress of an array element would be computed by adding the index to the address of the beginning of an array. Why do array indices start at 0 instead of 1 ?Ī. The latter is the preferred style in Java since the type of the variable int more clearly indicates that it is an array of integers. The former is how arrays are declared in C.

Some Java programmers use int a instead of int a to declare arrays. This distinction is often a rea- son to use a while instead of a for loop. In a typical for loop, the incrementing variable is not available for use in later statements in the corresponding while loop, it is. The code in the for loop header is considered to be in the same block as the for loop body. What is the difference between a for loop and its while formulation?Ī. Using explicit braces is a good way to avoid this dangling else pitfall. For example, if x is an int with the value 3, then the expression (x < 3.1) is true -Java converts x to double (because 3.1 is a double literal) before performing the comparison. Not without doing a type conversion, but remember that Java usually does the req- uisite type conversion automatically. Java has built-in constants available for this purpose: Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY.

How can I initialize a double variable to infinity?Ī. This strange (but true) result is a typical example of the effects of integer overflow. We use the int type for small numbers (less than ten decimal digits), and the long type when values run into the bil- lions or more. A little knowledge can go a long way in avoiding such problems. The short answer is that the lack of such checking is one reason such types are called primitive data types. This issue is a contentious one among programmers. Shouldn’t Java automatically check for overflow?Ī.

It seems wrong that Java should just let int s overflow and give bad values. This level of abstraction makes it easier for the developers of Java to ensure that our programs run on a broad variety of devices. A low-level version of your program that runs on the Java virtual machine.
