Java Type Casting — தமிழ்

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 08 / 50 · தமிழ்

Java Type Casting

Convert deliberately. Notice what you lose.

Step-by-step explanationsRunnable JavaPractice + answer

முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.

இந்தப் பாடத்தில்…

  • Explain the key idea behind Type Casting.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

அடிப்படையைப் புரிந்துகொள்ளுங்கள்

Widening and narrowing conversions have different consequences. Converting an int to double is automatic here. Converting a double to int requires a cast and truncates the fractional part toward zero; it does not round to the nearest integer.

02 / Step by step

விரிவான விளக்கங்கள்

Type Casting என்னன்னு தெரியுமா?

Type casting என்பது ஒரு data type-ஐ இன்னொரு type-ஆ மாத்துறது.

இதையும் அந்த 'பெட்டி' (Box) உதாரணத்தை வெச்சு ஈஸியா புரிஞ்சுக்கலாம். ஒரு சின்ன பெட்டியில இருக்குற பொருளை, பெரிய பெட்டிக்கு மாத்துறது ரொம்ப சுலபம் (Widening). ஆனா பெரிய பெட்டியில இருக்குறதை, சின்ன பெட்டிக்கு மாத்தணும்னா கொஞ்சம் force பண்ணனும் இல்லையா? அது தான் (Narrowing).

  • Widening Casting (automatic) — சின்ன type-ல இருந்து பெரிய type-க்கு மாத்றது: byte → short → char → int → long → float → double
  • Narrowing Casting (manual) — பெரிய type-ல இருந்து சின்ன type-க்கு மாத்றது: double → float → long → int → char → short → byte

Widening Casting

இது automatically நடக்கும் — manually எதுவும் பண்ணவேண்டியதில்ல:

Narrowing Casting

இதை manually பண்ணணும் — target type-ஐ parentheses-ல போடணும்:

Java reference fragmentJAVA
// Widening (automatic): small → large type
int myInt = 9;
double myDouble = myInt;   // automatic
System.out.println(myDouble);  // 9.0

// Narrowing (manual): large → small type
double pi = 3.14159;
int piInt = (int) pi;      // decimal truncated
System.out.println(piInt); // 3

// Real use: percentage
int score = 700, max = 1000;
double pct = (double) score / max * 100;
System.out.println(pct + "%");  // 70.0%

Widening casting-க்கு manually cast செய்ய வேண்டுமா?

  1. ஆம், எப்போதும்
  2. இல்லை, Java automatically செய்யும்
  3. Only for String types
  4. Only in loops
Reveal answer / விடை

இல்லை, Java automatically செய்யும்

Widening casting (int → double போன்று) Java automatically செய்யும் — data loss இல்லாததால். ஆனால் Narrowing casting (double → int) manually `(int)` என்று செய்ய வேண்டும்.

03 / Read and predict

முழுமையான உதாரணம்

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.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        int count = 9;
        double widened = count;
        double price = 19.95;
        int narrowed = (int) price;
        System.out.println(widened);
        System.out.println(narrowed);
    }
}
Predict first. Then reveal the expected output / வெளியீடு
9.0
19

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

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.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

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.

05 / Your turn

நீங்களே முயற்சி செய்யுங்கள்

Practice challenge

Change price to -19.95. What will narrowed contain?

Reveal the answer and reasoning / விடை

The result is -19 because conversion to int truncates toward zero.

தவிர்க்க வேண்டிய தவறு: A cast can discard information. Do not use a cast as a substitute for checking whether a value is in the expected range.

பாடத்தின் சுருக்கம்

  • 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.

Coding Tamilan · Understand the idea. Try the code. Build the habit.