Java do nothing

JavaPython

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

;

; 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.

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongoodcowView Question on Stackoverflow
Solution 1 - JavaJamesView Answer on Stackoverflow
Solution 2 - JavasabbahillelView Answer on Stackoverflow
Solution 3 - Javauser2357112View Answer on Stackoverflow
Solution 4 - JavaAdam BurleyView Answer on Stackoverflow
Solution 5 - JavaMindaugas BernatavičiusView Answer on Stackoverflow
Solution 6 - JavaGroView Answer on Stackoverflow