Java के Assignment Operators
In this page:
variable = value;
variable += value; // also -=, *=, /=, %=
Simple Assignment (=)
Plain = operator इसके right side पर जो भी हो evaluate करता है और उस result को left पर नाम किए variable में store करता है -- यह इकलौता operator है जो सिर्फ एक value compute करने के बजाय असल में assignment perform करता है।
उदाहरण: Simple Assignment (=)
public class Main {
public static void main(String[] args) {
int x = 5 + 3; // evaluates the right side, stores into x
System.out.println(x);
}
}
Login to try C/C++/Java/PHP code in the editor
Add and Subtract Assignment
+= और -= एक arithmetic step को एक token में assignment के साथ combine करते हैं, इसलिए count += 5 count की current value में 5 जोड़ता भी है और उस नए total को वापस count में लिखता भी है।
उदाहरण: Add and Subtract Assignment
public class Main {
public static void main(String[] args) {
int count = 10;
count += 5; // adds 5, writes the total back into count
count -= 3;
System.out.println(count);
}
}
Login to try C/C++/Java/PHP code in the editor
Multiply and Divide Assignment
*= और /= multiplication और division के लिए उसी तरह काम करते हैं -- price *= 1.1 price पर एक 10% increase apply करता है और result वापस price में store करता है।
उदाहरण: Multiply and Divide Assignment
public class Main {
public static void main(String[] args) {
double price = 100;
price *= 1.1; // applies a 10% increase, stores it back
System.out.println(price);
}
}
Login to try C/C++/Java/PHP code in the editor
Modulo and Bitwise Assignment
%= variable को एक value से divide करने का remainder compute करता है और उस remainder को वापस variable में store करता है, जो एक counter को एक fixed range के अंदर wrap रखने का एक compact तरीका है।
उदाहरण: Modulo and Bitwise Assignment
public class Main {
public static void main(String[] args) {
int counter = 8;
counter %= 5; // keeps counter wrapping within a fixed range
System.out.println(counter);
}
}
Login to try C/C++/Java/PHP code in the editor
Shift Assignment Operators
<<=, >>=, और >>>= किसी variable पर एक bit shift apply करते हैं और तुरंत shifted result वापस इसमें save करते हैं, जो low-level code में आम है जो कई values को एक single integer में pack या unpack करता है।
उदाहरण: Shift Assignment Operators
public class Main {
public static void main(String[] args) {
int packed = 0b0010;
packed <<= 2; // shifts left, saves result back into packed
System.out.println(packed);
}
}
Login to try C/C++/Java/PHP code in the editor
- Compare करते समय
=और==को confuse करना। x += 5के बजायx =+ 5लिखना, जो सिर्फ positive 5 assign करता है और जोड़ता नहीं।+=के बजाय=+लिखना, जो सिर्फ एक positive value assign करता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: