Java Math
Let the standard library do the calculation.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Math.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
Math groups common numeric operations. Read the return type as well as the method name: sqrt returns a double, while max has overloads for several numeric types. Floating-point calculations can be approximate, so test against appropriate tolerances when needed.
விரிவான விளக்கங்கள்
Java Math
Java-ல Math class use பண்ணி mathematical operations பண்ணலாம்.
Useful Math methods:
Math.max(x, y)— இரண்டுல பெரியதை return பண்றதுMath.min(x, y)— இரண்டுல சிறியதை return பண்றதுMath.sqrt(x)— square root கொடுக்கும்Math.abs(x)— absolute value (positive) கொடுக்கும்Math.random()— 0.0 முதல் 1.0 வரை random number கொடுக்கும்Math.pow(x, y)— x-ஐ y-ஆல power பண்றது
System.out.println(Math.max(15, 42)); // 42
System.out.println(Math.min(15, 42)); // 15
System.out.println(Math.abs(-64)); // 64
System.out.println(Math.sqrt(144)); // 12.0
System.out.println(Math.pow(2, 10)); // 1024.0
System.out.println(Math.round(4.6)); // 5
System.out.println(Math.ceil(4.2)); // 5.0
System.out.println(Math.floor(4.9)); // 4.0
int rand = (int)(Math.random() * 100);
System.out.println(rand); // e.g. 73`Math.random()` என்ன return செய்யும்?
- 0 முதல் 100 வரை random integer
- 0.0 முதல் 1.0 வரை random double
- 1 முதல் 10 வரை random number
- Negative number
Reveal answer / விடை
0.0 முதல் 1.0 வரை random double
`Math.random()` 0.0 (inclusive) முதல் 1.0 (exclusive) வரை random double return செய்யும். 1-10 range வேண்டுமென்றால்: `(int)(Math.random() * 10) + 1`
முழுமையான உதாரணம்
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 {
System.out.println(Math.max(12, 27));
System.out.println(Math.abs(-8));
System.out.println(Math.sqrt(81));
System.out.println(Math.round(4.6));
}
}Predict first. Then reveal the expected output / வெளியீடு
27 8 9.0 5
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
Change the square-root argument to 2 and compare the type and value of the output.
Reveal the answer and reasoning / விடை
Math.sqrt(2) produces an approximate double value, about 1.4142135623730951.
தவிர்க்க வேண்டிய தவறு: Math.pow returns a double. The ^ operator is bitwise XOR for integer operands, not exponentiation.
பாடத்தின் சுருக்கம்
- 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.