0% found this document useful (0 votes)
14 views1 page

part15

Uploaded by

Up ka sher king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

part15

Uploaded by

Up ka sher king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Java Switch Statement

The Java switch statement executes one statement from mul ple condi ons. It is like if-else-if ladder
statement. The switch statement works with byte, short, int, long, enum types, String and some
wrapper types like Byte, Short, Int, and Long. Since Java 7, we can use strings in the switch
statement.

Let's understand about switch statement in Java in detail.

The switch statement can be described as control flow type statement which is u lized for
manipula ng the flow of program execu on and invoking various branches of code using the value of
an expression.

In other words, the switch statement tests the equality of a variable against mul ple values.

Points to Remember

o There can be one or N number of case values for a switch expression.

o The case value must be of switch expression type only. The case value must be literal or
constant. It doesn't allow variables.

o The case values must be unique. In case of duplicate value, it renders compile- me error.

o The Java switch expression must be of byte, short, int, long (with its Wrapper
type), enums and string.

o Each case statement can have a break statement which is op onal. When control reaches to
the break statement, it jumps the control a er the switch expression. If a break statement is
not found, it executes the next case.

o The case value can have a default label which is op onal.

In Java, switch statement mainly provides a more detailed alterna ve that avoids the usage of nested
or several if-else statements when associated with an individual variable.

The syntax of the Java switch statement contains the switch keyword which is followed by the
expression that needs to be evaluated using parentheses. The men oned expression must definitely
evaluate to a definite data type which is primi ve such as int, char, or enum.

You might also like