0

I just saw a post about Python's loop: Trying to understand Python loop using underscore and input

There's a way to loop in python without "variable name":

for _ in range(10):
    print("Hello world")

A Java way to do it:

for (int _ = 0; _ < 10; _++) {
    System.out.println("Hello world");
}

And eclipse warns me:

'_' should not be used as an identifier, since it is a reserved keyword from source level 1.8 on.

Maybe I should just stick to i and write

for (int i = 0; i < 10; i++) {
    System.out.println("Hello world");
}

which use a (maybe) not necessary integer variable.

Is there another way to write simple java loop like python loop, maybe more elegantly?

Community
  • 1
  • 1
Sean L
  • 151
  • 2
  • 12
  • 4
    isn't `_` just another variable name as well? It's even *explicitly mentioned* in the comments and answers to that question. – Jongware Feb 06 '17 at 01:57
  • When I use `_` I got warning from eclipse "'_' should not be used as an identifier, since it is a reserved keyword from source level 1.8 on." but I should check out this first for `_` anyway.. http://stackoverflow.com/questions/23523946/underscore-is-a-reserved-keyword – Sean L Feb 06 '17 at 01:59
  • 3
    The `_` is a variable name, but it's conventional to use it when the loop index isn't used in the loop body. Also note that `_` has a special meaning in the interactive interpreter - it holds the result of the last expression that wasn't assigned to a variable. – PM 2Ring Feb 06 '17 at 02:00
  • I think I should close this as `_` is not necessarily a good way of writing code according to http://stackoverflow.com/questions/5893163/ – Sean L Feb 06 '17 at 02:01
  • 1
    FWIW, Nick Coghlan is a major core Python developer, so he knows what he's talking about. ;) But if you aren't doing i18n stuff then using `_` is perfectly fine and it makes it clear to readers that the loop index isn't used in the loop body. – PM 2Ring Feb 06 '17 at 02:14

4 Answers4

4

There is no more efficient or more elegant (IMO) way to express the loop in Java.

However:

  • The supposed efficiency of _ in Python could well be an illusion.
  • The supposed inefficiency of an unused variable in Java is probably an illusion. The JIT compiler should optimize away any assignments to the variable that do not contribute to the computation.
  • We are talking about a tiny overhead compared to the println call, and all of the stuff that that causes.

One more point. The identifier _ is legal in Java 8, but it is a blatant style violation. In Java 9, (I think) they are going to make it a syntax error!!

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Elegance is always a matter of opinion!! – Stephen C Feb 06 '17 at 02:03
  • 2
    In Python, _ has zero effect on the efficiency of the code execution, but it improves efficiency of humans reading the code, assuming they're familiar with the convention. – PM 2Ring Feb 06 '17 at 02:27
1

In Java 8+, you could use an IntStream and something like

IntStream.range(0, 10).forEach(i -> System.out.println("Hello, World"));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

According to the post and its answer, the '_' in a python loop is a "throwaway" variable, which means the value is not important / used in the loop.

I think your java loop is the same with your python loop, '_' or 'i' doesn't bring any diff.

licc
  • 1
  • 1
0

Maybe a helper function will do

IntConsumer c(Runnable f) {
    return ($) -> f.run();
}

and run the code with

IntStream.range(0, 10).forEach(c(() -> System.out.println("hi")));

Downside is, for each type of consumer you need to have a helper function since this won't work as IntConsumer is not a Function:

private <T1, T2> Function<T1, T2> c(Runnable f) {
    return ($) -> { f.run(); return null; }
}
meh
  • 26
  • 2