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

Java के Assignment Operators

Assignment operators एक variable में एक value डालते हैं, और short versions इसे अपनी पुरानी value उपयोग करके update करते हैं, जैसे 'मेरे score में 5 जोड़ो'। यह variable का नाम दोहराने से बचाता है।
Syntax
java
variable = value;
variable += value;  // also -=, *=, /=, %=

Simple Assignment (=)

Plain = operator इसके right side पर जो भी हो evaluate करता है और उस result को left पर नाम किए variable में store करता है -- यह इकलौता operator है जो सिर्फ एक value compute करने के बजाय असल में assignment perform करता है।

उदाहरण: Simple Assignment (=)

java
public class Main {
	public static void main(String[] args) {
		int x = 5 + 3; // evaluates the right side, stores into x
		System.out.println(x);
	}
}

Add and Subtract Assignment

+= और -= एक arithmetic step को एक token में assignment के साथ combine करते हैं, इसलिए count += 5 count की current value में 5 जोड़ता भी है और उस नए total को वापस count में लिखता भी है।

उदाहरण: Add and Subtract Assignment

java
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);
	}
}

Multiply and Divide Assignment

*= और /= multiplication और division के लिए उसी तरह काम करते हैं -- price *= 1.1 price पर एक 10% increase apply करता है और result वापस price में store करता है।

उदाहरण: Multiply and Divide Assignment

java
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);
	}
}

Modulo and Bitwise Assignment

%= variable को एक value से divide करने का remainder compute करता है और उस remainder को वापस variable में store करता है, जो एक counter को एक fixed range के अंदर wrap रखने का एक compact तरीका है।

उदाहरण: Modulo and Bitwise Assignment

java
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);
	}
}

Shift Assignment Operators

<<=, >>=, और >>>= किसी variable पर एक bit shift apply करते हैं और तुरंत shifted result वापस इसमें save करते हैं, जो low-level code में आम है जो कई values को एक single integer में pack या unpack करता है।

उदाहरण: Shift Assignment Operators

java
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);
	}
}
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. Compare करते समय = और == को confuse करना।
  2. x += 5 के बजाय x =+ 5 लिखना, जो सिर्फ positive 5 assign करता है और जोड़ता नहीं।
  3. += के बजाय =+ लिखना, जो सिर्फ एक positive value assign करता है।

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.