Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly run a static code snippet in Java using an IDE?

I'd like to quickly check the result of a static method I'm analyzing, within the IDE if possible.

public static int foo(int a, int b){
    return (a & 11) ^ b;
}

For example, right-click the expression and run it, something like

> foo(11, 2)
> 9
> foo(28, 3)
> 11

I don't know the result in advance (the actual code is more complicated), so unit testing is out of the question.

How can I do this?

like image 914
Jivay Avatar asked Aug 27 '26 18:08

Jivay


1 Answers

In Eclipse there are the so-called Java Scrapbook Pages: see Eclipse Help - Creating a Java Scrapbook Page

Since Java 9 there is the so-called JShell which can also be used without an IDE: see Oracle Help Center - Introduction to JShell

Java 9 JShell can be used from IntelliJ IDEA: Tools | JSHell Console... action.

like image 72
howlger Avatar answered Aug 29 '26 08:08

howlger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!