Processing.org Java Syntax

I‘ve been intending to do this for a while now, but never got very far…

Anyway, what this will hopefully become is a compact description of Processings Java syntax.

There are many ways to find the correct syntax for specific situations, but i never found anything that describes the possible syntax alternatives for different situations, or makes it clear in less than ~100 words.

So this is my attempt at describing the syntax for beginners (who probably won‘t need to read 3 pages of text full of complex information for the time being, since that can easily take away the fun in programming for many).

This is just an attempt for now, since i don‘t know how often i can edit this post, and i hope for your help and suggestions :wink:.

This will probably take a while to complete, so don‘t expect too much to be covered for now :sweat_smile:

Hope this will help some newcomers to understand the structure of coding easier :blush:

Anyway, let‘s start :

The first thing to cover should be the syntax for declaring and defining variables :


//Declaring variables (creating variables)

ClassName variableName;

ClassName[] variableName;

ClassName variableName = new ClassName(variable...); // or new ClassName();

ClassName[] variableName = new ClassName[arrayLength];

//Defining variables (setting variables)

ClassName variableName = variable;

ClassName[] variableName = variableArray;

•ClassName is the class of the variable.

•variableName is the name of the variable you want to declare.

•variable stands for another variable of the same class that has already been declared.

•variable… means, that it is possible for multiple variable s to be used, or not to include one (leave empty, like ()). They have to be seperated by a comma and can be of different classes (although the class depends on the class required in the class initialization method and they have to be in the correct order)

•arrayLength has to be an Integer value (0,1,2).

•aariableArray stands for an already declared array of the same Class.

Example :


Ball ball;

Ball[] balls;

Ball ball = new Ball(100,30); // Where 100,30 could be the coordinates of the ball.

Ball[] balls = new Ball(); // This will create 20 variables of the class Ball.

// They have been declared like in the first line of the code above and therefore have no value attached. 
// Therefore you‘ll have to define them afterwards. 
// They don‘t have to be declared again.
//To define an already declared variable, you can use the same code as f0r declaring variables without including the first ClassName before the variableName

Ball ball = redBall;

Ball[] balls = redBalls; // redBalls is an array of class Ball

Next, we have to talk about declaring and calling methods :


//Declaring methods (creating methods)

Type methodName (variable...) { // if nothing should be returned, use ‚void‘

//MethodBody

return (Type) variableName; //not used if Type is ‚void‘

}

//Calling methods

ClassName.methodName(); // no return variable, since ‚void‘ has been used in the declaration

ClassName variableName = ClassName.methodName(); // returns a variable of class Type, since it used ‚ClassName‘ in it‘s declaration

•Type is the class of the variable the method will return (‚void‘ if no variable should be returned).

•methodName is the name with which the method will be called.

•MethodBody is the code that is executed when this method is called.


void printHello () {

println(„Hello“);

}

Ball getRedBall () {

return redBall;

}

void printMyText (String myText) {

println(myText);

}

Ball getRedBall (Int index) {

return redBalls[index]; //redBalls is an array of the class Ball

}

Methods are always part of a class and have to be called referencing an object or a class :


Ball.setPosition(0,0); // ‚void‘ is used in the declaration to not return a variable

Float thirdRedBallPosX = (Float) ( (Ball) ( (ManyBalls) manyBalls).getRedBall(3)).getPositionX(); // the Type of the method is Float in this case
//sorry for the mess with different classes, but i just couldn‘t come up with a better way to use the above declared method...

println(„Hello“); 
// This method is also referencing a class/variable. 
//To be precise, it is referencing ‚this‘, which is just implied, so you don‘t need to write it. 
//In this case, this would be the PApplet (basically the main class)

I‘ll just post this for now and wait for some reactions :wink:

1 Like

Nice approach!

Here

ClassName VariableName;

ClassName[] VariableName;

ClassName VariableName = new ClassName(Variable...); // or new ClassName();

ClassName[] VariableName = new ClassName[ArrayLength];

by convention that’s

ClassName variableName;  // small letter 

ClassName[] variableNames; // small letter and plural (many items in list)

ClassName variableName = new ClassName(Variable...); // or new ClassName(); // small letter

ClassName[] variableNames = new ClassName[arrayLength];   // small letter and plural 

I wrote the ClassName etc. with capitals to make sure people don‘t confuse them with actual variable names :sweat_smile:

Hm… I’d call that the return type. Either it’s returning nothing (void) or a variable type (int) or a class (Date) or an Array like int or int

I‘ve though about that, too, but there is still the difference with the return var; at the end of the Body of the method. It is there in a return-method, but not in a void-method, therefore i thought i‘d go with them being different types of methods.

see also https://github.com/Kango/Processing-snippets/wiki/programming-and-functions

and other articles there

Did you see the tutorials section on the website?

see also Some guidance for beginners

1 Like

No.

When there is no explicit return on the end of the method, it’s returning void.

Yeah, you‘re right. I‘ll change that :sweat_smile: As for the link to the Guidance for beginners, i‘ve actually posted it there after reading it, since it‘s technically meant as a Guide for beginners, but goToLoop replaced it to Gallery.

1 Like

Since it’s not a question, and this forum doesn’t have a Tutorials category, the closer I think for it is the Gallery category. :woozy_face:

But feel free to choose another 1 if you find it’s more appropriate. Maybe Community category? :thinking:

I placed it in Beginners, since it‘s a Guide meant mainly for beginners and could technically be seen as a discussion, which would pretty much fit it‘s description and since there was already the Guide for Beginners pinned to the top, i thought it would be the best place.

Gallery didn’t really fit, since it‘s description goes more in the direction of Software or similar.

And Community also doesn‘t really fit the goal, since it‘s different from it‘s description.

In Short, i‘d say Beginners would be best, but i‘ll think about it a bit more and then we‘ll see :sweat_smile:

1 Like

Something in the direction of Guidelines for writing code might be a nice addition to https://processing.org/tutorials/ :slight_smile:

1 Like

Problem is that Processing Beginners category is already used for basic syntax questions/doubts posts; but yours is a guide. :cowboy_hat_face:

Another possible category is Teaching. :man_teacher:

Well, it‘s meant as an extension of Chrisirs pinned post in Beginners to specifically target those Syntax questions and give an answer before you have to specifically ask the question (or at least that‘s what i‘d hope for :sweat_smile:).

2 Likes