70

What is the equivalent to Python's pass in Java? I realize that I could use continue or not complete the body of a statement to achieve that effect, but I like having a pass statement.

3
  • 10
    An empty brace pair. { } Mar 19, 2014 at 0:17
  • What on earth do you use it for? Mar 19, 2014 at 0:27
  • 2
    There is no strict equivalent to it (since you can pass on methods which normally return a value), but there are "do nothing"s. What is it that you want to do exactly? In what context?
    – fge
    Mar 19, 2014 at 0:28

6 Answers 6

106

Just use a semi-colon ;, it has the same effect.

1
  • @TonyCeralva: The question asked for a statement that does nothing, not a statement that prevents compiler optimizations. Jun 9, 2020 at 22:00
40

If you want something noticeable, you can use

assert true;

This will allow you to have something that a reader can recognize or that can be searched for.

4
  • 4
    This seems to be the most clean answer!
    – kgui
    Apr 18, 2018 at 21:35
  • I may be mistaken but assert true is not the same as a no-op Jun 21, 2018 at 22:53
  • 1
    @user3282276 It is not the same, but an assert true is always performed and it is easy to identify as part of a debug statement. When there is no explicit no-op command it is better than an empty statement. That was the point that I was trying to make. Jun 22, 2018 at 11:55
  • @user3282276 the OP wanted something to use instead of an empty statement. Since there is no no-op or pass then this can be used instead Jun 24, 2018 at 17:59
17
;

; is the empty statement. Usually, you don't need it - you can just put nothing in the brackets for an empty loop - but it can be useful.

9

I normally use something like:

"".isEmpty(); // do nothing

It's useful to be able to have a line of code which does nothing when debugging. You can put a breakpoint on that line, which is especially useful if it would usually be an empty block of code (e.g. empty catch block etc), as putting a breakpoint on an empty line can create confusion about where the breakpoint is actually set.

1
  • 1
    I think this is the most genius answer, would use this instead of a bland semicolon
    – Daneolog
    Jan 21, 2022 at 21:53
2

There is no (strict) equivalent since in java you have to specify the return type for the method in its declaration which is then checked against a computed type of the following the return statement. So for methods that have a return type - neither semicollon nor leaving empty braces will work;

I personally use: throw new java.lang.UnsupportedOperationException("Not supported yet."); - this is searchable, alerts you that the method is not implemented and is compatible with the strict return types.

1
  • 1
    don't a method with no operation can't throw any exceptions, because it does nothigng? Dec 11, 2020 at 5:56
2

I feel, there is no construct in Java identical to pass in Python. This is mostly because Java is a statically typed language where as Python is a dynamically typed language. More so when you are defining a method / function. In that context, the provided answers are valid / correct only for a method that returns void.

For example for a Python function

def function_returns_void:
    pass

you can have a Java method

public void function_returns_void(){}

or

public void function_returns_void(){;}

but when a method is supposed to return a value, while pass may still work in Python, one will stuck with compilation problem when not returning a value.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.