2

This might be a trivial question, but I came across this syntax for an Array Declaration:

   void someFunction(int n){
      int[] myArray = new int[ n == 0 ? 1 : n ];
      ...
   }

I tried looking up online for some tutorials to understand what is happening with no luck.
Can anyone explain the expression in the right bracket, and when is something like that typically used?

1
  • 1
    n == 0 ? 1 : n is basically a compressed if statement, if the condition is true, it's the first element, if it's the not then it's the second element. In this case the variable seems to be the array size, and the code makes sure the array is never of size zero. Commented Sep 4, 2012 at 4:51

6 Answers 6

5

The right expression is a 'shortcut' to the 'if/(then)/else'

The first part of the expression is the 'if', the condition and can (but doesn't have to be) included in brackets, for clarification purposes.

Then comes the ?, stating 'Condition over, what's the result?' After that comes the 'true' statement, and after the colon the 'else' statement.

In short that means: If n == 0, allocate an array of size 1, otherwise allocate n elements.

It's a rather common c syntax and a nice way to shorten variable assignments, but doesn't really have anything to do with arrays per definition.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh ok. So basically its doing nothing but declaring the array of a size which is based on the value of n. Got it. Thanks!
2

It's basically a syntactic sugar for if/else. Here's a link

Comments

1

It's a ternary operator. The boolean statement before the ? is evaluated and, if it's true, the expression is evaluated to the value before the :, else it evaluates to the second value.

Comments

1

It is java ternary operator (?) is used to check if n is 0. if n is zero, create array of size 1.

if n is >1 , use that for creating array. Zero length arrays are legal in java, so not sure what author meant here.

If it to safeguard from bad values for in is should have been checking for n > 0 ? n :1 , so even negative values get a array of size 1

Comments

1

As @ATaylor and @SperanskyDanil told,
Syntax will create array with size 1, when n=0 and it'll create Array with size n, when n != 0.
as Shown in below diagram.
enter image description here

Comments

1

Here is a simple declaration of array with length 5:

int[] myArray = new int[5];

n == 0 ? 1 : n provides a number (1 if n == 0 and n if not), it is an example of ternary operator.

So

int[] myArray = new int[ n == 0 ? 1 : n ];

is shorthand to

int[] myArray;

if (n == 0) {
  myArray = new int[1];
} else {
  myArray = new int[n];
}

1 Comment

Your explanation is correct, however your 'shorthand' code would cause a problem, since int[] myArray would go out of scope right after it is created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.