go to previous page   go to home page   go to next page

Answer:

There is nothing wrong with the program. Since color is a primitive data type, an expression such as

color == 'V'

compares the contents of color (a character) to the character literal 'V'. However, the following would be wrong:

color == "V"

This is asking to compare the character value in color with a reference to the String object "V".


switch with Strings

Recall the syntax of the switch statement:

switch ( expression )
{
  case label1:
    statementList1 
    break;

  case label2:
    statementList2 
    break;

  case label3:
    statementList3 
    break;

  . . . other cases like the above

  default:
     defaultStatementList 
}

Starting with Java 7.0 the expression can be a String reference and the case labels can be String literals.

Matching of the expression with the case labels is done as if by String.equals().


QUESTION 13:

Is "BTW".equals( " BTW ") true or false?