As of today (27.08.2025), IntelliJ IDEA has a feature called "Scratch files". You can create a single Java class as a "Java Scratch". This will be stored separately from your project files and can be executed without the need to compile the whole project.
Just press Ctrl+Alt+Shift+Insert at once (this is the most complicated part, I promise). In the dialog, you have to select Java (for a Java Scratch, of course). The scratch will automically have a main method, where you can paste your code and execute it with the green arrow at the left hand side.
Another nice feature: you can create a new scratch file with the contents of the current selection in the editor. Just select some code and press Alt+Enter. In the context dialog select "Create new scratch file from selection".