views:

1100

answers:

9

I would look it up myself, but I don't even know what it's called. Would anyone mind explaining what it does? Thanks!

EDIT: I didn't know there were multiple times the : appeared. What does it do in this case here:

public String toString() {
 String cardString = "";
 for (PlayingCard c : this.list)  // <--
 {
   cardString = cardString + c + "\n";
 }

Edit: How would you write this for-each loop a different way so as to not incorporate the ":"?

A: 

It is used in the new short hand for/loop

final List<String> list = new ArrayList<String>();
for (final String s : list)
{
   System.out.println(s);
}

and the ternary operator

list.isEmpty() ? true : false;
fuzzy lollipop
I didn't realize it was that new... when did it come in?
piggles
@Mechko, in Java 5: http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html#forloop
Jonik
oh... that was 6 years ago... Not new by my frame of reference :D
piggles
I'm with piggles - six years means it's been around since practically the dawn of time.
James Moore
+17  A: 

There is no "colon" operator, but the colon appears in two places:

1: In the ternary operator, e.g.:

int x = bigInt ? 10000 : 50;

In this case, the ternary operator acts as an 'if' for expressions. If bigInt is true, then x will get 10000 assigned to it. If not, 50. The colon here means "else".

2: In a for-each loop:

double[] vals = new double[100];
//fill x with values
for (double x : vals) {
    //do something with x
}

This sets x to each of the values in 'vals' in turn. So if vals contains [10, 20.3, 30, ...], then x will be 10 on the first iteration, 20.3 on the second, etc.

Note: I say it's not an operator because it's just syntax. It can't appear in any given expression by itself, and it's just chance that both the for-each and the ternary operator use a colon.

Claudiu
+1  A: 

It's used in for loops to iterate over a list of objects.

for (Object o: list)
{
    // o is an element of list here
}

Think of it as a for <item> in <list> in Python.

Mike Cialowicz
A: 

The colon actually exists in conjunction with ?

int minVal = (a < b) ? a : b;

is equivalent to:

int minval;
if(a < b){ minval = a;} 
else{ minval = b; }

Also in the for each loop:

for(Node n : List l){ ... }

literally:

for(Node n = l.head; n.next != null; n = n.next)
piggles
+31  A: 

There are several places colon is used in Java code:

1) Jump-out label:

label: for (int i = 0; i < x; i++) {
    for (int j = 0; j < i; j++) {
        if (something(i, j)) break label; // jumps out of the i loop
    }
} 
// i.e. jumps to here

2) Ternary condition:

int a = (b < 4)? 7: 8; // if b < 4, set a to 7, else set a to 8

3) For-each loop:

String[] ss = {"hi", "there"}
for (String s: ss) {
    print(s); // output "hi" , and "there" on the next iteration
}

4) Assertion:

int a = factorial(b);
assert a >= 0: "factorial may not be less than 0"; // quits the program with the message if the condition evaluates to false

5) Case in switch statement:

switch (type) {
    case WHITESPACE:
    case RETURN:
        break;
    case NUMBER:
        print("got number: " + value);
        break;
    default:
        print("syntax error");
}
yuku
nice - I missed a few! and I didn't even know you can name assertions like that, very useful.
Claudiu
+1  A: 

You usually see it in the ternary assignment operator;

Syntax

variable =  `condition ? result 1 : result 2;`

example:

boolean isNegative = number > 0 ? false : true;

which is "equivalent" in nature to the if else

if(number > 0){
    isNegative = false;
}
else{
    isNegative = true;
}

Other than examples given by different posters,

you can also use : to signify a label for a block which you can use in conjunction with continue and break..

for example:

public void someFunction(){
     //an infinite loop
     goBackHere: { //label
          for(int i = 0; i < 10 ;i++){
               if(i == 9 ) continue goBackHere;
          }
     }
}
ultrajohn
I'm sorry, but that is an awful example. Why wouldn't you write boolean isNegative = number > 0;Ternary conditions are good for things such as double sgn = number>0 ? 1 :0;
+1  A: 

In your specific case,

String cardString = "";
for (PlayingCard c : this.list)  // <--
{
    cardString = cardString + c + "\n";
}

this.list is a collection (list, set, or array), and that code assigns c to each element of the collection.

So, if this.list were a collection {"2S", "3H", "4S"} then the cardString on the end would be this string:

2S
3H
4S
yuku
thanks for your answer. How could this code be rewritten to not use the ":" ?
Kevin Duke
+1  A: 

How would you write this for-each loop a different way so as to not incorporate the ":"?

Assuming that list is a Collection instance ...

public String toString() {
   String cardString = "";
   for (Iterator<PlayingCard> it = this.list.iterator(); it.hasNext(); /**/) {
      PlayingCard c = it.next();
      cardString = cardString + c + "\n";
   }
}
Stephen C
My question is: why? Why doing the same thing the long way?
RichN
@RichN - he doesn't want to do it, he just wants to know how.
Stephen C
It's homework..
Chris Nava
+1  A: 

Just to add, when used in a for-each loop, the ":" can basically be read as "in".

So

for (String name : names) {
    // remainder omitted
}

should be read "For each name IN names do ..."

Helper Method