#Switch() vs if-else

97 messages ยท Page 1 of 1 (latest)

lapis viper
#

how is Switch() is more optimized than if-else?

pale blazeBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
How is Switch() is more optimized than if-else?

In Java, switch statements can be more optimized than if-else chains primarily because of how the bytecode and JVM handle them.

  1. Jump Table Optimization:
    For switch statements with dense and contiguous case values (like integers or enums), the JVM may use a jump table. This allows constant-time branching (O(1)), as it directly jumps to the matched case rather than evaluating each condition sequentially.

  2. Lookup Switch:
    For sparse or non-contiguous values, the JVM can use a lookup table, which is still more efficient than a chain of if-else because it uses a binary search internally, resulting in O(log n) time complexity vs. O(n) for linear if-else checks.

  3. Predictability & Readability:
    switch expresses a multi-way branch clearly, which helps both JVM optimizations and developer readability, while if-else requires evaluating each condition sequentially until a match is found.

In contrast, an if-else chain always checks conditions one by one until a match occurs, potentially causing more CPU instructions and slower performance for many cases.


Useful links:

livid stag
#

else-if is a mess in that thing though it still works

#

also

#
var = switch(val) {
  case 1 -> 3;
  .
   .
    .
}
pale blazeBOT
livid stag
#

this can be done by switch statements too

#

this cant be done by else-if

lapis viper
lapis viper
#

i meant this

livid stag
#

its more efficient in specific use cases

restive hazel
#

I think OP is asking you what your periods (.) mean in your code example

livid stag
#

you can add several more lines to iit

livid stag
lapis viper
#

thanks for the help

livid stag
lapis viper
lapis viper
livid stag
#
// this is the efficient way though
int var = switch(val) {
  case 1->3;
  case 2->5;
  default -> 2;
}
pale blazeBOT
livid stag
#

sry for late response

#

i had forgotten how to use default in it

lapis viper
livid stag
twilit pulsar
#

Strictly speaking that's a 'switch expression' and there are two forms of 'switch statements' (the old-style and enhanced-switch statement)

lapis viper
livid stag
#

i forgot that

lapis viper
twilit pulsar
#

Switch statements can (but are not always) more efficient that a tree of nested if-else statements.

lapis viper
#

as i can see ,it enhances the readibilty

#

thanks i got to know something new though, i should really not use AI for quesn now ๐Ÿ˜…

livid stag
#

we have a nice java learning material if u want

pale blazeBOT
#

@lapis viper

For learning Java, we recommend the ebook Modern Java:
https://javabook.mccue.dev/

It is completely free, meant for beginners and covers all content relevant for the first year. It is a great way to learn Java from the ground up.
If you run into any questions during your journey, you may ask us in #1051826284008853505 and we will help cat_thumbs_up

If you prefer a more traditional learning experience, we also recommend MOOC:
https://java-programming.mooc.fi/
This is the course used by the University of Helsinki to teach their students programming in their first year, also completely free.

livid stag
#

its free

lapis viper
livid stag
lapis viper
#

yeah , i have been reading it alongside, downloaded bash etc,

#

and i got a doubt too

livid stag
lapis viper
#

can u see this, i tried what it said

#

but idk why it didnt work, so i moved on

livid stag
#

cd .. moves us to parent dir

#

so your outside of your user directory

#

while desktop folder stays in your user directory

#

in your context, the user dir is "/home/kartik"

lapis viper
#

but i tried cd Desktop there too

livid stag
lapis viper
#

and ls didnt list anything

livid stag
#

wait

lapis viper
#

not really its old, but yeah i downloaded ubuntu just yesterday

livid stag
#

it seems like u ls out of the user dir

livid stag
#

and then cd to Desktop

lapis viper
#

i tried , i am in home/kartik

#

then i did ls

#

i see bash directory , which i created

livid stag
lapis viper
livid stag
#

is it a virtual enviornment or truely windows?

lapis viper
#

what .. do u mean, i installed ubuntu , and actiated WSL

livid stag
lapis viper
#

oh.

#

but it said to install WSL, did i do something wrong

livid stag
#

WSL always runs seperate from windows

lapis viper
#

oh ok, so i cant go to desktop from here

#

okokkkk

livid stag
#

wait

#

try this command

#
cd ../../
cd /mnt/c
#

the first line takes you to root directory

#

the 2nd one takes you to C partition that is already mounted in WSL

#

@lapis viper

lapis viper
#

yes

#

kartik@DESKTOP-THJF8U9:/mnt/c$

#

came here

livid stag
#

you will get to see your typical windows root directory folders

lapis viper
#

oh it worked

#

okkk , i got to learn so many things from u ,

livid stag
#

yw

lapis viper
#

once again thank you