Java Switch
Map a value to a clear set of cases.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Switch.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
A switch is useful when one value selects among named alternatives. This example uses traditional case labels and break statements. Modern switch expressions use different syntax; learn one form clearly before mixing the two.
விரிவான விளக்கங்கள்
Java Switch Statements
Many-ஆ இருக்குற conditions-ஐ if-else-ஐ விட சுலபமா handle பண்ண switch use பண்றோம்.
Switch ஒரு expression-ஐ evaluate பண்ணும், value-ஐ cases-உடன் compare பண்ணும்:
break keyword மிகவும் important — இல்லன்னா அடுத்த case-உம் execute ஆகும் (fall-through).default — எந்த case-உம் match ஆகலன்னா இது execute ஆகும்.
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Weekend!");
}
// Wednesday
String type = switch (day) {
case 1, 2, 3, 4, 5 -> "Weekday";
default -> "Weekend";
};
System.out.println(type); // WeekdaySwitch statement-ல் `break` மறந்தால் என்ன நடக்கும்?
- Compile error வரும்
- Program crash ஆகும்
- Fall-through — அடுத்த case-உம் execute ஆகும்
- எந்த case-உம் execute ஆகாது
Reveal answer / விடை
Fall-through — அடுத்த case-உம் execute ஆகும்
Break இல்லாமல் matching case-ல் இருந்து அடுத்த அடுத்த cases எல்லாம் execute ஆகும் — இதை 'fall-through' என்பார்கள். Modern Java switch expressions (`->`) இந்த problem-ஐ தீர்க்கின்றன.
Modern Java Switch (Java 14+): Arrow syntax (case X ->) modern switch expression-ன் feature. இது fall-through இல்லாமல் cleaner code தரும். நீங்கள் இப்போது கற்றுக்கொண்டது cutting-edge Java — industry-ல் இன்று actively use ஆகிறது!
முழுமையான உதாரணம்
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
int choice = 2;
switch (choice) {
case 1: System.out.println("Learn"); break;
case 2: System.out.println("Practice"); break;
default: System.out.println("Choose 1 or 2");
}
}
}Predict first. Then reveal the expected output / வெளியீடு
Practice
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
நீங்களே முயற்சி செய்யுங்கள்
Practice challenge
Remove the break after case 1 and run with choice = 1.
Reveal the answer and reasoning / விடை
Learn and Practice are both printed because execution falls through to the next case. Restore the break afterwards.
தவிர்க்க வேண்டிய தவறு: In traditional switch statements, missing break can cause fall-through. It is not automatically a compiler error.
பாடத்தின் சுருக்கம்
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.