Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7870183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:32:41+00:00 2026-06-03T01:32:41+00:00

Possible Duplicate: Java: If vs. Switch For all the conditional statements that are observed

  • 0

Possible Duplicate:
Java: If vs. Switch

For all the conditional statements that are observed in programming, which of these blocks is the most preferred:

  1. Ternary operator
  2. else-if block
  3. switch block

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:32:43+00:00Added an answer on June 3, 2026 at 1:32 am

    Of course you may implement the comparison in different ways.

    I did it this way:

    common block:

    int a = 42;
    int k = 17;
    

    if:

    if (a == 42) 
        k+=4;
    else    k+=5;
    

    case:

    switch (a) {
        case 42: k+=4; break;
        default: k+=5; break;
    }
    

    Ternary:

    k += (a == 42) ? 4 : 5; 
    

    They don’t compile to the same bytecode:

    l *Tern*.class
    -rw-r--r-- 1 stefan stefan 704 2012-04-27 14:26 CaseIfTern.class
    -rw-r--r-- 1 stefan stefan 691 2012-04-27 14:26 IfTernCase.class
    -rw-r--r-- 1 stefan stefan 728 2012-04-27 14:26 TernIfCase.class
    

    However, advantages of switch come into play when you have multiple cases – not just 2.

    If and ternary get cascading for more than 2 cases.

    But they differ idiomatically/semantically. The Ternary operator returns something, but not the if or the switch.

    So it isn’t that clear what you have to compare.

    But I made a benchmark with following result:

    0   if      tern    case 
    1   3.103   0.244   0.118   
    2   0.306   0.276   0.309   
    3   0.382   0.329   0.328   
    4   0.464   0.435   0.458   
    5   5.045   1.519   1.248   
    6   4.57    3.088   2.915   
    7   4.036   2.977   3.015   
    8   3.197   3.834   3.893   
    9   4.631   4.523   5.488   
    10  6.445   3.891   3.09    
    

    benchmark plot (Benchmark plot)

    Which shows, that they really don’t make much difference, and that caching effects have still, for 5 M cases, an influence, even after heating up the VM.

    In real circumstances, you rarely have million invocations where nearly nothing happens. But if something happens, the time for if/case/ternary becomes soon irrelevant.

    Here is the code I tested:

    public class CaseTernIf
    {
        public static int aORbIf (int a) {
            if (a == 2) 
                return 4;
            else    return 5;
        }
    
        public static int aORbTern (int a) {
            return  (a == 2) ? 4 : 5;
        }
    
        public static int aORbCase (int a) {
            switch (a) {
                case 2:  return 4;
                default: return 5; 
            }
        }
    }
    

    Here is the Testing code (which is Scala):

    object IfCaseTernBench extends Benchcoat [List[Int], Seq[Int]] {
    
      type I=List[Int]
      type O=Seq[Int]
      val name = "CaseTern"
      /** We return a List of random Ints numbers here. */
      def iGenerator (n: Int) : I = (for (x <- 1 to n) yield math.abs (random.nextInt (3))).toList
      def takePart (li: I, n: Int) : I = li.take (n) 
    
      /* Each algorithm is called by a mapping to a static method.  */
      def ifTest   (i: I) : O = i.map (CaseTernIf.aORbIf) 
      def caseTest (i: I) : O = i.map (CaseTernIf.aORbCase) 
      def ternTest (i: I) : O = i.map (CaseTernIf.aORbTern) 
    
      // Map of Test names -> methods to test
      val list2bench: List [(String, I => O)] = List (
           "if test"    -> ifTest _
         , "case test"  -> caseTest _
         , "tern test"  -> ternTest _
      )
    
      def test = { 
         list2bench.foreach (algo => println (algo._2))
      }
    }
    

    updated:

    And here is the BenchCoat source

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to internationalize a java web application. greetings all i want that
Possible Duplicate: ROT-13 function in java? I have to shift all char from a
Possible Duplicate: Java 7 Date/Time API I've read rumors that Joda Time is slated
Possible Duplicate: Java private field access I just observed little weird(imho) thing in java
Possible Duplicate: Java garbage collector - When does it collect? When people say that
Possible Duplicate: Switch Statement With Strings in Java? Does the switch statement in Java
Possible Duplicate: Switch Statement with Strings in Java I am trying to use switch
Possible Duplicate: Java - Convert String to enum I have a method that uses
Possible Duplicate: Java: adding elements to a collection during iteration My problem is that
Possible Duplicate: Java equivalent to #region in c# Is there something in Java that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.