← Back to Core Java Course | Chapter 13: Advanced Topics & Reference | Lesson 1 of 10

Java की Math Class

Math class Java का calculator है, rounding, powers, square roots और pi जैसी चीज़ों के साथ। यह वह math करता है जो simple operators नहीं कर सकते।
Syntax
java
Math.function(number);  // e.g. Math.sqrt(x), Math.pow(x, y), Math.max(a, b)

Basic Math Operations

Math class basic operations के लिए static methods प्रदान करती है, जैसे absolute value के लिए Math.abs() और comparisons के लिए Math.max()/Math.min(), कुछ भी instantiate किए बिना।

उदाहरण: Basic Math Operations

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(Math.abs(-5));
		// Print a line to the console
		System.out.println(Math.max(3, 7));
		System.out.println(Math.min(3, 7));
	}
}

Rounding Operations

Math.round(), Math.floor(), और Math.ceil() हर एक अलग तरीके से round करते हैं — nearest, down, और up respectively — और गलत एक चुनना numeric code में off-by-one bugs का एक आम source है।

उदाहरण: Rounding Operations

java
public class Main {
	public static void main(String[] args) {
		System.out.println(Math.round(4.5)); // nearest
		System.out.println(Math.floor(4.9)); // down
		System.out.println(Math.ceil(4.1)); // up
	}
}

Exponents and Roots

Math.pow(base, exponent) exponents compute करता है और Math.sqrt() square roots compute करता है, दोनों एक double return करते हैं भले ही inputs integers हों।

ध्यान दें कि छोटे fixed integer powers के लिए Math.pow आमतौर पर direct multiplication से धीमा है, इसलिए 'x * x' अक्सर Math.pow(x, 2) से बेहतर है।

उदाहरण: Exponents and Roots

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(Math.pow(2, 3));
		// Print a line to the console
		System.out.println(Math.sqrt(16));
	}
}

Trigonometry and Constants

Math.sin(), Math.cos(), और Math.PI जैसे constants trigonometric और geometric calculations support करते हैं, सभी default रूप से degrees के बजाय radians में काम करते हुए।

Math.toRadians() और Math.toDegrees() से degrees और radians के बीच convert करना असली-world angle values के साथ काम करते समय एक आम companion step है।

उदाहरण: Trigonometry and Constants

java
public class Main {
	public static void main(String[] args) {
		System.out.println(Math.PI);
		System.out.println(Math.sin(Math.PI / 2)); // radians, not degrees
	}
}

Exact Arithmetic

Math.addExact() जैसे methods चुपचाप wrap होने के बजाय overflow पर एक ArithmeticException throw करते हैं, जो तब valuable है जब exact correctness raw speed से ज़्यादा मायने रखती है।

उदाहरण: Exact Arithmetic

java
public class Main {
	public static void main(String[] args) {
		try {
			Math.addExact(Integer.MAX_VALUE, 1); // throws instead of silently wrapping
		} catch (ArithmeticException e) {
			System.out.println("Overflow detected: " + e.getMessage());
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि Math.round(2.5) 2 देगा, जब यह half up to 3 round होता है।
  2. Math.pow उपयोग करना और एक int की उम्मीद करना, जब यह एक double return करता है।
  3. Math.sqrt(a / b) के अंदर integer division उपयोग करना और decimals खोना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.