Indeed Processing sketches become Java code just before they’re compiled when we hit the “Run” button (CTRL + R).
But notice that the default extension in Processing is “.pde” and not “.java”.
It means that its syntax is slightly diff. than Java’s, and it still needs some small behind-the-scenes changes in order to output a 100% fully compliant Java compilable code.
All the “.pde” files within a Processing’s IDE (PDE) project folder are concatenated as 1 file.
Then the PDE’s preprocessor transpiles that 1 single “.pde” to a “.java” file, wrapping up our whole code as 1 class
that extends
PApplet:
Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html
This is your site link’s Java syntax answer for it:
public class PrintNumbers
{
public static void main(String[] args)
{
for(int i=1; i<=10; i++)
{
System.out.println(i);
}
}
}
In order to convert that Java syntax code to Processing’s we need to pay attention to some details of it.
The code we actually want is just this: for (int i = 1; i <= 10; i++) System.out.println(i);
That code is inside a method called main(), which in turn is wrapped up inside a class
called PrintNumbers.
The method main() is special to Java b/c it acts as the entry-point where the code starts.
However, Processing’s preprocessor creates its own custom main() behind-the-scenes.
Therefore it’s highly unrecommended to attempt creating our own main()!
As I’ve mentioned before, all “.pde” files end up wrapped up as 1 PApplet subclass; so we don’t need PrintNumbers either!
Another cool thing is that Processing’s already got a function called println():
Even though System.out.println() still works in Processing, there’s no point to type in such boilerplate when we can go w/ just println()!
Now, after removing the class
PrintNumbers and its main() method, all that’s left is:
for (int i = 1; i <= 10; ++i) println(i);
Just copy & paste that 1-liner in Processing and it will just magically run!
That’s called immediate mode, when we don’t create any functions in our “.pde” tab file.
However, most of the time we’re gonna need to create our callback setup(), which in some way acts like Java’s main():
void setup() {
for (int i = 1; i <= 10; println(i++));
exit();
}
When we define Processing callbacks, it’s called interactive mode.