Explain switch statement in java language, JAVA Programming

Assignment Help:

Explain switch statement in java language?

Switch statements are shorthands for a certain type of if statement. It is not common to see a stack of if statements all related to the similar quantity like this:

if (x == 0) doSomething0();
else if (x == 1) doSomething1();
else if (x == 2) doSomething2();
else if (x == 3) doSomething3();
else if (x == 4) doSomething4();
else doSomethingElse();

Java has a shorthand for these kinds of multiple if statements, the switch-case statement. Here's how you'd write the above using a switch-case:

switch (x) {
  case 0: 
    doSomething0();
    break;
  case 1: 
    doSomething1();
    break;
  case 2: 
    doSomething2();
    break;
  case 3: 
    doSomething3();
    break;
  case 4: 
    doSomething4();
    break;
  default: 
    doSomethingElse();
}

In this fragment x must be a variable or expression in which can be cast to an int without loss of precision. This means the variable must be or the expression must return an int, byte, short or char. x is compared along with the value of each the case statements in succession until one matches. This fragment compares x to literals, other than these too could be variables or expressions as long as the variable or result of the expression is an byte, int, short or char. The default action is triggered if no cases are matched.

Once a match is found, all following statements are executed until the end of the switch block is reached or you break out of the block. This could trigger decidedly unexpected behavior. Thus it is general to involve the break statement at the end of each case block. It's excellent programming practice to put a break after each one unless you explicitly want all following statements to be executed.

It's significant to remember that the switch statement doesn't end while one case is matched and its action performs. A program then executes all statements in which follow in which switch block until specifically told to break.


Related Discussions:- Explain switch statement in java language

Swing, can we develop paint user can draw his charcters and images in swing...

can we develop paint user can draw his charcters and images in swing

Determine the meaning of finalize, Determine the meaning of finalize W...

Determine the meaning of finalize When  a  finalize method  is  defined  in  a  class,  Java  run  time  calls  finalize() whenever  it's  about  to  recycle  an object  of  t

Programming help, writing a program to find common friends with the use of ...

writing a program to find common friends with the use of array

Need to develop chat application for j2me, Need to develop Chat application...

Need to develop Chat application for j2me and I want Image Processing code Project Description: We want a chat application for a community website. Image processing and co

Object and Instance, What is the difference between instance and object of ...

What is the difference between instance and object of a class? Few says both are same, then why java kept both the words for same thing?

Explain term literals in java, Explain term Literals in java? Literals ...

Explain term Literals in java? Literals are pieces of Java source code which mean exactly what they say. For example "Hello World!" is a String literal and its meaning is the w

How to integrate your struts application with spring, To integrate your Str...

To integrate your Struts application with Spring, we have two options: ? Configure Spring to manage your Actions as beans, using the ContextLoaderPlugin, and set their dependenc

I want five page website design, I want five page website design Project...

I want five page website design Project Description: Five page website CMS Front end design Skills required are Graphic Design, Java, HTML, PHP, Website Design

Explain the use and purpose of java adapter classes, Question: (a) Exp...

Question: (a) Explain the event handling mechanism using a 'JComboBox' as example. Your answer must clearly indicate the event source, event object and event listener. Also

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd