← Back to Core Java Course | Chapter 3: Operators | Lesson 2 of 9

Java के Arithmetic Operators

Arithmetic operators एक calculator के math buttons हैं: plus, minus, times, divide, और remainder। Java इन्हें numbers के साथ calculations करने के लिए उपयोग करती है।
Syntax
java
result = a + b;  // also -, *, /, %

Addition and Subtraction

Addition और subtraction Java के सभी numeric types (int, double, long, वगैरह) में बिल्कुल उम्मीद के अनुसार काम करते हैं, और calculation के दौरान ज़रूरत पड़ने पर Java अपने आप छोटे types widen करता है।

उदाहरण: Addition and Subtraction

java
public class Main {
	public static void main(String[] args) {
		int a = 5;
		double b = 2.5;
		System.out.println(a + b); // int widened to double automatically
		System.out.println(a - b);
	}
}

Multiplication and Division

Integer division चुपचाप zero की ओर truncate करता है और किसी भी remainder को drop करता है -- 7 / 2 3 evaluate होता है, 3.5 नहीं -- इसलिए एक fractional result चाहते हुए दो ints divide करने के लिए पहले कम से कम एक operand को double में cast करना चाहिए।

उदाहरण: Multiplication and Division

java
public class Main {
	public static void main(String[] args) {
		System.out.println(7 / 2);         // 3 - integer division truncates
		System.out.println(7 / (double) 2); // 3.5 - cast forces a fractional result
	}
}

Modulo Operator

Modulo operator division के बाद जो बचता है वह return करता है, इसलिए x % 2 == 0 यह test करने के लिए standard idiom है कि x even है, क्योंकि कोई भी even number 2 से divide होने पर 0 remainder छोड़ता है।

उदाहरण: Modulo Operator

java
public class Main {
	public static void main(String[] args) {
		int x = 10;
		System.out.println(x % 2 == 0); // true - standard idiom for testing even numbers
	}
}

Increment and Decrement

Prefix (++x) variable increment करता है और फिर नई value तक evaluate होता है; postfix (x++) पहले पुरानी value तक evaluate होता है और बाद में increment करता है -- difference सिर्फ तब मायने रखता है जब expression का result तुरंत उपयोग हो, जैसे array[i++] में।

उदाहरण: Increment and Decrement

java
public class Main {
	public static void main(String[] args) {
		int x = 5;
		System.out.println(++x); // prefix: increments first, then evaluates - prints 6
		int y = 5;
		System.out.println(y++); // postfix: evaluates old value first - prints 5
	}
}

Compound Assignment

+= जैसे Compound operators read, calculate, और write-back को एक step में fold करते हैं -- total += price total = total + price के लिए shorthand है, और brevity से आगे यह variable name फिर type करने और एक typo का जोखिम उठाने से भी बचाता है।

उदाहरण: Compound Assignment

java
public class Main {
	public static void main(String[] args) {
		int total = 100;
		int price = 20;
		total += price; // shorthand for total = total + price
		System.out.println(total);
	}
}
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. दो integers divide करना और एक decimal की उम्मीद करना, क्योंकि 5 / 2 2 है; एक को double में cast करें।
  2. एक integer को zero से divide करना, जो ArithmeticException throw करता है, जबकि एक double Infinity देता है।
  3. यह मान लेना कि % हमेशा एक positive result देता है, जब -7 % 3 -1 है।

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.