-3

I was looking through the java source code, ArrayList.java->hugeCapacity, when I found something I'd never seen before:

private static int hugeCapacity(int minCapacity) {
    if (minCapacity < 0) // overflow
       throw new OutOfMemoryError();
   return (minCapacity > MAX_ARRAY_SIZE) ?
       Integer.MAX_VALUE :
       MAX_ARRAY_SIZE;
}

I looked on this site before posting this question and found nothing helpful. I also looked at the docs for return(), but I had no success. Perhaps I'm looking for the wrong thing.

Anyways, what is that return statement doing?

Airistotal
  • 95
  • 8
  • 1
    Have you looked at [java operators](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html)? – Sotirios Delimanolis Apr 26 '14 at 19:11
  • 4
    It's a conditional operator, aka the [ternary operator](http://en.wikipedia.org/wiki/%3F:#Java). – yshavit Apr 26 '14 at 19:11
  • 1
    @SotiriosDelimanolis That phrasing sounds a bit condescending. There's no way someone new to Java would know that's called a ternary/conditional operator, or even that it's an operator at all. In other words, it's hard to google for it without already knowing the answer. This is a reasonable question, asked with as much research and context as could be expected. – yshavit Apr 26 '14 at 19:15
  • @yshavit If `return` didn't bring up the answer, the only logical progression is to look at what is in the rest of the statement. `minCapacity` and `MAX_ARRAY_SIZE` seem like variable names, so does `Integer.MAX_VALUE`. So what is the `? :`? – Sotirios Delimanolis Apr 26 '14 at 19:24
  • @SotiriosDelimanolis You and I know it's an operator, and we even know that they're both part of the same operator. But that's not obvious if you don't already know it (a newbie might not even know enough about Java and language design to differentiate between operators and other symbols, such as the "diamond operator," which of course isn't actually an operator at all). Googling `java ?`, `java :` or even `java ? :` doesn't help at all. – yshavit Apr 26 '14 at 20:25
  • @yshavit What are you trying to defend or argue about? Their learning process or my bluntness? `what is that return statement doing?` Run it and see. Do they know what `>` means? That should be a hint for what kinds of values to test with. As we often say, Stackoverflow is not a replacement for a book or tutorials. The [Java tutorials](http://docs.oracle.com/javase/tutorial/java/TOC.html) explain what `? :` is in the first few chapters, so do most beginner books I've read. I did end up putting them on the path to the answer. – Sotirios Delimanolis Apr 26 '14 at 20:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51513/discussion-between-yshavit-and-sotirios-delimanolis) – yshavit Apr 26 '14 at 21:29

3 Answers3

1
return (minCapacity > MAX_ARRAY_SIZE) ?
       Integer.MAX_VALUE :
       MAX_ARRAY_SIZE;

here return is having ternary operator passed to it. and it works similar to if-else

if(minCapacity > MAX_ARRAY_SIZE){

return Integer.MAX_VALUE;
}

else{
return MAX_ARRAY_SIZE;
}
Karibasappa G C
  • 2,686
  • 1
  • 18
  • 27
1

It's not a special statement. It's ternary statement :

(condtion) ? value_if_true : value_if_flase;

You also can use it to put a value in a variable

string res = ( i < 3 ) ? "i < 3" : "i > 3";

it's a short way to write :

string res;

if(i<3)
    res = "i < 3";
else
    res = "i > 3";
Emrys Myrooin
  • 2,179
  • 14
  • 39
0

The syntax

testStatement ? (return if true) : (return if false) 

is a standard way of doing an if statement in one line. Basically you have a test (usually checking the value of a variable). If it tests true, the first statement after the test is executed. If it is false, the second statement (after the colon ':') is executed. It replaces the following:

if (statement) {
  <return this value if true>;
} else {
  <return this value if false>;
}
DerStrom8
  • 1,311
  • 2
  • 23
  • 45