DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Import a Function/Module in Dataweave
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Java String: A Complete Guide With Examples
  • Java: A Time-Tested Programming Language Still Going Strong

Trending

  • Five Tools for Data Scientists to 10X their Productivity
  • Spring Authentication With MetaMask
  • How to Migrate Vector Data from PostgreSQL to MyScale
  • An Introduction to Build Servers and Continuous Integration
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Scope in Switch Expression

Scope in Switch Expression

Understand the Switch expression in Java, and its differences with if-else and for statements.

Ivan Zerin user avatar by
Ivan Zerin
·
Aug. 28, 15 · Tutorial
Like (2)
Save
Tweet
Share
8.53K Views

Join the DZone community and get the full member experience.

Join For Free

The switch expression is not often used by Java developers in everyday practice. So, I suppose, knowledge about this construction isn't as deep as for 'if-else' or 'for.' At least mine... I faced one interesing case and wanted to share it with you. Let's take a look at the code:

    int state = ...
    ...
    List states = new ArrayList<>();
    switch (state) {
        case 0:
            Integer newState = orderState + 1;
            states.add(newState);
            break;
        case 1:
            newState = orderState + 2;
            states.add(newState);
            break;
        case 2:
            newState = orderState + 3;
            states.add(newState);
            break;
        default:
            newState = orderState + 4;
            states.add(newState);
        }

Do you think this code is correct? Or maybe it should look like this:

    int state = ...
    ...
    List states = new ArrayList<>();
    switch (state) {
        case 0:
            Integer newState = orderState + 1;
            states.add(newState);
            break;
        case 1:
            Integer newState = orderState + 2;
            states.add(newState);
            break;
        case 2:
            Integer newState = orderState + 3;
            states.add(newState);
            break;
        default:
            Integer newState = orderState + 4;
            states.add(newState);
        }

Should newState be declared as a new variable in every case or not? My first intention was to answer that the first variant is correct. Seemed obvious that switch is the same as if-else, but with some more if cases inside. So we would declare a new variable in every condition branch.

But it is not correct. You should declare variable once in a switch expression and use it after in all the case blocks. It can be explained very simply: every case ends with break, which prevents processing all subsequent cases, but break is optional. If there is no break operator, then all operators under the first case will be executed, then all operators under the second case and etc. So without break, it is simple part of code with the same scope, where all operators will be executed in usual order.

By the way, one more hint: { and } indicate the border of scope. Switch have one pair of {}, so there is one scope, but if-else could have one pair of {} per if and else. That is why it is possible to declare the same variable in if part and in else part.

Operator (extension) Java (programming language) dev Blocks Processing Branch (computer science)

Published at DZone with permission of Ivan Zerin, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Import a Function/Module in Dataweave
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Java String: A Complete Guide With Examples
  • Java: A Time-Tested Programming Language Still Going Strong

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: