| | The simplest control-flow construct Java offers is the if statement. But in bytecodes, the if is not so simple. When a Java program is compiled, the if statement may be translated to a variety of opcodes. Each opcode pops one or two values from the top of the stack and does a comparison. The opcodes that pop only one value off the top of the stack compare that value with zero. The opcodes that pop two values off the stack compare one of the popped values to the other popped value. If the comparison succeeds (success is defined differently by each individual opcode), the Java virtual machine (JVM) branches -- or jumps -- to the offset given as an operand to the comparison opcode. In this manner, the if statement provides many ways for you to make the Java virtual machine decide between two alternative paths of program flow. | Editor's note: Sometimes the most interesting discussions begin when someone says, "This may be a stupid question, but ...". If the person asking the question has taken the time to think about the problem before asking, the question is often not stupid at all. The uncertainty points out an ambiguity in the specs, holes in the docs, or a search for how more experienced programmers might address a particular problem. From time to time, we will print one of the "(Not So) Stupid Questions" we receive and invite our readers to answer the question in the feedback section. | Summing a list of integers is one basic use for variable arguments. Computing the average of a list of floating-point numbers, concatenating a list of strings into a single string, and finding the maximum/minimum values in lists of floating-point numbers are other basic uses. The following BasicUses.java source code demonstrates these basic uses, to give you more exposure to variable arguments. | This article is an in-depth examination of one of the simplest but most pleasing features in Java 5.0--the for-each loop. I present eleven short items discussing various nuances of usage, pitfalls to be aware of, and possible optimizations surrounding the use of the for-each loop. In the first section, I discuss what kind of iterations are possible with the for-each. The next section illustrates common programming errors in using the for-each loop. The final section shows how to write new classes that can be used as targets of a for-each loop. I also talk about advanced implementations that allow multiple iterable views; lazily construct objects just in time for iteration; and enable possible generic algorithm and compiler optimizations of the for-each loop. | Editor's note: Sometimes the most interesting discussions begin when someone says, "This may be a stupid question, but ...." If the person asking the question has taken the time to think about the problem before asking, the question is often not stupid at all. The uncertainty points out an ambiguity in the specs, holes in the docs, or a search for how more experienced programmers might address a particular problem. From time to time, we will print one of the "(Not So) Stupid Questions" we receive and invite our readers to answer the question in the feedback section. | It is an accepted fact that Java does not support return-type-based method overloading. This means that a class cannot have two methods that differ only by return type -- you can't have int doXyz(int x) and double doXyz(int x) in the same class. And indeed, the Java compiler duly rejects any such attempt. But recently I discovered a way to do this, which I wish to share with all. Along the way, we will also explore some rudiments of Java bytecode programming. | Decisions, decisions: keep it simple The simplest control-flow construct Java offers is the if statement. But in bytecodes, the if is not so simple. When a Java program is compiled, the if statement may be translated to a variety of opcodes. Each opcode pops one or two values from the top of the stack and does a comparison. The opcodes that pop only one value off the top of the stack compare that value with zero. The opcodes that pop two values off the stack compare one of the popped values to the other popped value. If the comparison succeeds (success is defined differently by each individual opcode), the Java virtual machine (JVM) branches -- or jumps -- to the offset given as an operand to the comparison opcode. In this manner, the if statement provides many ways for you to make the Java virtual machine decide between two alternative paths of program flow. | | That happy medium, at least as far as I'm concerned, is rooted in finding convenient ways to do things, that aren't short for the the sake of being short. As a great example of just such a solution, Java 5.0 introduces a new version of the for loop, which I refer to as for/in. It's also called foreach, and sometimes enhanced for -- but these all refer to the same construct. Whatever you choose to call it, for/in makes for simpler code, as you'll see in the examples throughout this article. |
|