Home > Software design >  Wonder about syntax and language for two code examples
Wonder about syntax and language for two code examples

Time:12-30

I watched a video about composition and inheritance. In this video I saw two different examples of code syntax that I have not seen before.

Generally the code looks like java but I don't recognize these two code snippets.

Would appriciate if someone would explain what the code does and if the code is java or another language. Thanks!


First code:

public void replacePixel(Pixel[,] pixels] {....}

Here it is the syntax [,] that is new to me. What does it do?


Second code:

void saveClicked(){
    file?.load(image);
}

Here is is the syntax ?. What does it do?

Tried to use a online java compiler and the syntax did not seem to work.

CodePudding user response:

public void replacePixel(Pixel[,] pixels) {....}

Im not sure if this is valid for Java, but in C#:

[,] here means that as an argument you want to get two-dimensional array. If you want three-dimensional array you can use [,,] and so on.

void saveClicked(){
    file?.load(image);
}

Here ? is a conditional operator that says: if file is not null then do file.load(image). Otherwise, if file is null do nothing. This can be converted to:

void saveClicked(){
    if(file != null){
        file.load(image);
    }
}
  • Related