Java के Arithmetic Operators
In this page:
result = a + b; // also -, *, /, %
Addition and Subtraction
Addition और subtraction Java के सभी numeric types (int, double, long, वगैरह) में बिल्कुल उम्मीद के अनुसार काम करते हैं, और calculation के दौरान ज़रूरत पड़ने पर Java अपने आप छोटे types widen करता है।
उदाहरण: Addition and Subtraction
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
Modulo Operator
Modulo operator division के बाद जो बचता है वह return करता है, इसलिए x % 2 == 0 यह test करने के लिए standard idiom है कि x even है, क्योंकि कोई भी even number 2 से divide होने पर 0 remainder छोड़ता है।
उदाहरण: Modulo Operator
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
}
}
Login to try C/C++/Java/PHP code in the editor
Increment and Decrement
Prefix (++x) variable increment करता है और फिर नई value तक evaluate होता है; postfix (x++) पहले पुरानी value तक evaluate होता है और बाद में increment करता है -- difference सिर्फ तब मायने रखता है जब expression का result तुरंत उपयोग हो, जैसे array[i++] में।
उदाहरण: Increment and Decrement
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
}
}
Login to try C/C++/Java/PHP code in the editor
Compound Assignment
+= जैसे Compound operators read, calculate, और write-back को एक step में fold करते हैं -- total += price total = total + price के लिए shorthand है, और brevity से आगे यह variable name फिर type करने और एक typo का जोखिम उठाने से भी बचाता है।
उदाहरण: Compound Assignment
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);
}
}
Login to try C/C++/Java/PHP code in the editor
- दो integers divide करना और एक decimal की उम्मीद करना, क्योंकि
5 / 22है; एक कोdoubleमें cast करें। - एक integer को zero से divide करना, जो
ArithmeticExceptionthrow करता है, जबकि एक doubleInfinityदेता है। - यह मान लेना कि
%हमेशा एक positive result देता है, जब-7 % 3-1है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: