Java for loop syntax: for (T obj : objects)
It's called a for-each or enhanced for
statement. See the JLS §14.14.2.
It's syntactic sugar provided by the compiler for iterating over Iterable
s and arrays. The following are equivalent ways to iterate over a list:
List<Foo> foos = ...;
for (Foo foo : foos)
{
foo.bar();
}
// equivalent to:
List<Foo> foos = ...;
for (Iterator<Foo> iter = foos.iterator(); iter.hasNext();)
{
Foo foo = iter.next();
foo.bar();
}
and these are two equivalent ways to iterate over an array:
int[] nums = ...;
for (int num : nums)
{
System.out.println(num);
}
// equivalent to:
int[] nums = ...;
for (int i=0; i<nums.length; i++)
{
int num = nums[i];
System.out.println(num);
}
Further reading
- How is Java's for loop code generated by the compiler
- The For-Each Loop
Java: For-Each loop and references
Java works a little bit different than many other languages. What o
is in the first example is simply a reference to the object.
When you say o = new MyObject()
, it creates a new Object of type MyObject and references o
to that object, whereas before o
referenced objects[index]
.
That is, objects[index] itself is just a reference to another object in memory. So in order to set objects[index] to a new MyObject, you need to change where objects[index] points to, which can only be done by using objects[index].
Image: (my terrible paint skills :D)
Explanation:
This is roughly how Java memory management works. Not exactly, by any means, but roughly. You have objects, which references A1. When you access the objects array, you start from the beginning reference point (A1), and move forward X blocks. For example, referencing index 1 would bring you to B1. B1 then tells you that you're looking for the object at A2. A2 tells you that it has a field located at C2. C2 is an integer, a basic data type. The search is done.
o does not reference A1 or B1, but C1 or C2. When you say new ...
, it will create a new object and put o there (for example, in slot A3). It will not affect A1 or B1.
Let me know if I can clear things up a little.
How does the Java 'for each' loop work?
for (Iterator<String> i = someIterable.iterator(); i.hasNext();) {
String item = i.next();
System.out.println(item);
}
Note that if you need to use i.remove();
in your loop, or access the actual iterator in some way, you cannot use the for ( : )
idiom, since the actual iterator is merely inferred.
As was noted by Denis Bueno, this code works for any object that implements the Iterable
interface.
Also, if the right-hand side of the for (:)
idiom is an array
rather than an Iterable
object, the internal code uses an int index counter and checks against array.length
instead. See the Java Language Specification.
For..In loops in JavaScript - key value pairs
If you can use ES6 natively or with Babel (js compiler) then you could do the following:
const test = {a: 1, b: 2, c: 3};
for (const [key, value] of Object.entries(test)) {
console.log(key, value);
}
Iterate through a C++ Vector using a 'for' loop
Is there any reason I don't see this in C++? Is it bad practice?
No. It is not a bad practice, but the following approach renders your code certain flexibility.
Usually, pre-C++11 the code for iterating over container elements uses iterators, something like:
std::vector<int>::iterator it = vector.begin();
This is because it makes the code more flexible.
All standard library containers support and provide iterators. If at a later point of development you need to switch to another container, then this code does not need to be changed.
Note: Writing code which works with every possible standard library container is not as easy as it might seem to be.
Avoiding NullPointerException in Java
This to me sounds like a reasonably common problem that junior to intermediate developers tend to face at some point: they either don't know or don't trust the contracts they are participating in and defensively overcheck for nulls. Additionally, when writing their own code, they tend to rely on returning nulls to indicate something thus requiring the caller to check for nulls.
To put this another way, there are two instances where null checking comes up:
Where null is a valid response in terms of the contract; and
Where it isn't a valid response.
(2) is easy. As of Java 1.7 you can use Objects.requireNonNull(foo)
. (If you are stuck with a previous version then assert
ions may be a good alternative.)
"Proper" usage of this method would be like below. The method returns the object passed into it and throws a NullPointerException
if the object is null. This means that the returned value is always non-null. The method is primarily intended for validating parameters.
public Foo(Bar bar) {
this.bar = Objects.requireNonNull(bar);
}
It can also be used like an assert
ion though since it throws an exception if the object is null. In both uses, a message can be added which will be shown in the exception. Below is using it like an assertion and providing a message.
Objects.requireNonNull(someobject, "if someobject is null then something is wrong");
someobject.doCalc();
Generally throwing a specific exception like NullPointerException
when a value is null but shouldn't be is favorable to throwing a more general exception like AssertionError
. This is the approach the Java library takes; favoring NullPointerException
over IllegalArgumentException
when an argument is not allowed to be null.
(1) is a little harder. If you have no control over the code you're calling then you're stuck. If null is a valid response, you have to check for it.
If it's code that you do control, however (and this is often the case), then it's a different story. Avoid using nulls as a response. With methods that return collections, it's easy: return empty collections (or arrays) instead of nulls pretty much all the time.
With non-collections it might be harder. Consider this as an example: if you have these interfaces:
public interface Action {
void doSomething();
}
public interface Parser {
Action findAction(String userInput);
}
where Parser takes raw user input and finds something to do, perhaps if you're implementing a command line interface for something. Now you might make the contract that it returns null if there's no appropriate action. That leads the null checking you're talking about.
An alternative solution is to never return null and instead use the Null Object pattern:
public class MyParser implements Parser {
private static Action DO_NOTHING = new Action() {
public void doSomething() { /* do nothing */ }
};
public Action findAction(String userInput) {
// ...
if ( /* we can't find any actions */ ) {
return DO_NOTHING;
}
}
}
Compare:
Parser parser = ParserFactory.getParser();
if (parser == null) {
// now what?
// this would be an example of where null isn't (or shouldn't be) a valid response
}
Action action = parser.findAction(someInput);
if (action == null) {
// do nothing
} else {
action.doSomething();
}
to
ParserFactory.getParser().findAction(someInput).doSomething();
which is a much better design because it leads to more concise code.
That said, perhaps it is entirely appropriate for the findAction() method to throw an Exception with a meaningful error message -- especially in this case where you are relying on user input. It would be much better for the findAction method to throw an Exception than for the calling method to blow up with a simple NullPointerException with no explanation.
try {
ParserFactory.getParser().findAction(someInput).doSomething();
} catch(ActionNotFoundException anfe) {
userConsole.err(anfe.getMessage());
}
Or if you think the try/catch mechanism is too ugly, rather than Do Nothing your default action should provide feedback to the user.
public Action findAction(final String userInput) {
/* Code to return requested Action if found */
return new Action() {
public void doSomething() {
userConsole.err("Action not found: " + userInput);
}
}
}
Related Topics
Why Does Javac Complain About Generics Unrelated to the Class' Type Arguments
Using Itextpdf to Trim a Page's Whitespace
How to Update a Value, Given a Key in a Hashmap
How to Implement Dynamic Gui in Swing
When to Close Connection, Statement, Preparedstatement and Resultset in Jdbc
How to Compile a Java File with a Different Name Than the Class
How to Dockerize Maven Project? and How Many Ways to Accomplish It
How to Read All of Inputstream in Server Socket Java
Why Can't Primitive Data Types Be "Null" in Java
Difference Between Break and Continue Statement
Spring Jdbc Template for Calling Stored Procedures
Java Streams: Replacing Groupingby and Reducing by Tomap
How to Access Parameters in a Restful Post Method
Java 8 Date and Time: Parse Iso 8601 String Without Colon in Offset
How Can a Class Have a Member of Its Own Type, Isn't This Infinite Recursion