"Loop:" in Java Code. What Is This, and Why Does It Compile

loop: in Java code. What is this, and why does it compile?

It is not a keyword it is a label.

Usage:

    label1:
for (; ; ) {
label2:
for (; ; ) {
if (condition1) {
// break outer loop
break label1;
}
if (condition2) {
// break inner loop
break label2;
}
if (condition3) {
// break inner loop
break;
}
}
}

Documentation.

Compiling loops in Java

It is not for better JIT optimizations - for JIT, these code snippets are equivalent. It is because there is no sense to make optimizations in javac, since JIT optimizations are more powerful anyway.

What does ' : ' do in a java loop?

The colon does nothing, aside from allowing your code to compile!

: is just part of the syntax of the enhanced for statement, also less-formally known as the "for each loop".

As it says in the "for each loop" link above:

void cancelAll(Collection<TimerTask> c) {
for (TimerTask t : c)
t.cancel();
}

When you see the colon (:) read it as "in." The loop above reads as "for each TimerTask t in c."

So, applying the same interpretation to your code, read it as "for each String s in words".

Java: how to make bufferedreader loop through input file, and run code blocks once

Only loop through file once. Read and collect data as you go. Don't build output until you have all the data. When you see a new Rover file entry, write output (unless first entry) and clear values. When you reach the end, write output (if any).

Isolating code in a class might make it easier to reuse printing logic.

Example:

public final class LogEntry {
private final Pattern pattern = Pattern.compile("Rover file: (.*)" +
"|(\\d+)% total coverage" +
"|(\\d+)% coverage by (.*)");
private String roverFile;
private Integer totalCoverage;
private Map<String, Integer> fileCoverage = new LinkedHashMap<>();

public void process(BufferedReader in) throws IOException {
for (String line; (line = in.readLine()) != null; ) {
Matcher m = this.pattern.matcher(line);
if (! m.matches())
continue;
if (m.start(1) != -1) {
print();
clear();
this.roverFile = m.group(1);
} else if (m.start(2) != -1) {
this.totalCoverage = Integer.valueOf(m.group(2));
} else if (m.start(3) != -1) {
this.fileCoverage.put(m.group(4), Integer.valueOf(m.group(3)));
}
}
print();
}

private void clear() {
this.roverFile = null;
this.totalCoverage = null;
this.fileCoverage.clear();
}

private void print() {
if (this.roverFile == null)
return;
if (this.fileCoverage.isEmpty()) {
System.out.println(this.roverFile + "," + this.totalCoverage);
} else {
for (Entry<String, Integer> entry : this.fileCoverage.entrySet()) {
System.out.println(this.roverFile + "," + this.totalCoverage + "," + entry.getValue() + "," + entry.getKey());
}
}
}
}

Test

String input = "Rover file: AA-123-12345-SP1.SSF\n" +
"Local time: 2/11/2014 8:06:30 PM to 2/11/2014 8:37:15 PM\n" +
"100% total coverage\n" +
"100% coverage by guug04314054.zip\n" +
"Rover file: AA-321-54321-SP1.SSF\n" +
"Local time: 2/3/2015 4:06:14 PM to 2/3/2015 4:06:44 PM\n" +
"0% total coverage. No matching base data found.\n" +
"Rover file: AA-132-12354-SP2.SSF\n" +
"Local time: 2/17/2014 5:51:01 PM to 2/17/2014 6:18:57 PM\n" +
"100% total coverage\n" +
"4% coverage by guug04914003.zip\n" +
"100% coverage by guug04914022.zip\n";
try (BufferedReader in = new BufferedReader(new StringReader(input))) {
new LogEntry().process(in);
}

Output

AA-123-12345-SP1.SSF,100,100,guug04314054.zip
AA-321-54321-SP1.SSF,0
AA-132-12354-SP2.SSF,100,4,guug04914003.zip
AA-132-12354-SP2.SSF,100,100,guug04914022.zip

What does this mark before for loop do?

retry is not a keyword, but an identifier. It's a label. See the Java Language Specification for the break statement.

A break statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label; this statement, which is called the break target, then immediately completes normally. In this case, the break target need not be a switch, while, do, or for statement.

Instead of breaking out of the innermost enclosing loop as it does by default, adding the identifier causes control to transer to the part of the code identified by this label.

In this case, when execution arrives at the break retry statement, it will go to a label retry: and continue from there.

Does Java recognize infinite loops?

I'll just quote the Java Language Specification, as it's rather clear on this:

This section is devoted to a precise explanation of the word "reachable." The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements. Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.

...

A while statement can complete normally iff at least one of the following is true:

  • The while statement is reachable and the condition expression is not a constant expression with value true.
  • There is a reachable break statement that exits the while statement.

...

Every other statement S in a nonempty block that is not a switch block is reachable iff the statement preceding S can complete normally.

And then apply the above definitions to this:

If a method is declared to have a return type, then every return statement (§14.17) in its body must have an Expression. A compile-time error occurs if the body of the method can complete normally (§14.1).

In other words, a method with a return type must return only by using a return statement that provides a value return; it is not allowed to "drop off the end of its body."

Note that it is possible for a method to have a declared return type and yet contain no return statements. Here is one example:

class DizzyDean {
int pitch() { throw new RuntimeException("90 mph?!"); }
}


Related Topics



Leave a reply



Submit