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

Java की सामान्य गलतियाँ

यह lesson उन slips को list करता है जो कई नए Java learners करते हैं, आम potholes का एक map जैसा। उन्हें पहले से जानना आपको उनसे बचने में मदद करता है।

Comparing Strings with ==

== String के लिए object references compare करता है, यह जांचते हुए कि दो variables memory में बिल्कुल उसी object की ओर point करते हैं या नहीं, उनकी contents बराबर हैं या नहीं नहीं — string content को सही से compare करने के लिए .equals() उपयोग करें।

उदाहरण: Comparing Strings with ==

java
public class Main {
	public static void main(String[] args) {
		String a = new String("hi");
		String b = new String("hi");
		System.out.println(a == b); // false: different objects
		System.out.println(a.equals(b)); // true: same content
	}
}

NullPointerException

एक currently null वाले variable पर एक method call करना NullPointerException throw करता है, Java का सबसे आम runtime error — इसका आमतौर पर मतलब है एक value जो आपने initialized माना था असल में कभी नहीं हुई।

उदाहरण: NullPointerException

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare value and set it to null
		String value = null;
		// Try code that might throw an exception
		try {
			value.length();
		// Handle NullPointerException if it was thrown
		} catch (NullPointerException e) {
			// Print a line to the console
			System.out.println("Caught NullPointerException");
		}
	}
}

Modifying List in For-Each Loop

एक for-each loop से iterate करते समय सीधे list.remove() call करना ConcurrentModificationException throw करता है — जब आपको mid-loop elements हटाने हों तो इसके बजाय एक explicit Iterator और इसका अपना remove() method उपयोग करें।

उदाहरण: Modifying List in For-Each Loop

java
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in list
		ArrayList<Integer> list = new ArrayList<>(java.util.List.of(1, 2, 3));
		// Try code that might throw an exception
		try {
			for (Integer n : list) {
				list.remove(n);
			}
		// Handle java.util.ConcurrentModificationException if it was thrown
		} catch (java.util.ConcurrentModificationException e) {
			// Print a line to the console
			System.out.println("Caught: use Iterator.remove() instead");
		}
	}
}

Array Index Out of Bounds

एक array या list के valid range से बाहर एक index access करना (size खुद उपयोग करना सहित, जो हमेशा आखिरी valid index से एक आगे होता है) ArrayIndexOutOfBoundsException throw करता है — एक classic off-by-one bug।

उदाहरण: Array Index Out of Bounds

java
public class Main {
	public static void main(String[] args) {
		int[] arr = {1, 2, 3};
		try {
			System.out.println(arr[3]); // size itself is one past the last valid index
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Caught: index out of bounds");
		}
	}
}

Integer Division Truncation

Java में दो int values divide करना integer division perform करता है और कोई भी decimal remainder truncate करता है, इसलिए 5 / 2 2 देता है, 2.5 नहीं — अगर आपको एक fractional result चाहिए तो पहले कम से कम एक operand को double में cast करें।

उदाहरण: Integer Division Truncation

java
public class Main {
	public static void main(String[] args) {
		System.out.println(5 / 2); // 2: truncated
		System.out.println(5 / (double) 2); // 2.5: cast avoids truncation
	}
}
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. equals() के बजाय == से strings compare करना।
  2. एक null reference पर एक method call करना, जो NullPointerException throw करता है।
  3. 5 / 2 उपयोग करना और 2.5 की उम्मीद करना, जब integer division 2 देता है।

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.