Java की सामान्य गलतियाँ
In this page:
Comparing Strings with ==
== String के लिए object references compare करता है, यह जांचते हुए कि दो variables memory में बिल्कुल उसी object की ओर point करते हैं या नहीं, उनकी contents बराबर हैं या नहीं नहीं — string content को सही से compare करने के लिए .equals() उपयोग करें।
उदाहरण: Comparing Strings with ==
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
}
}
Login to try C/C++/Java/PHP code in the editor
NullPointerException
एक currently null वाले variable पर एक method call करना NullPointerException throw करता है, Java का सबसे आम runtime error — इसका आमतौर पर मतलब है एक value जो आपने initialized माना था असल में कभी नहीं हुई।
उदाहरण: NullPointerException
// 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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
equals()के बजाय==से strings compare करना।- एक
nullreference पर एक method call करना, जोNullPointerExceptionthrow करता है। 5 / 2उपयोग करना और2.5की उम्मीद करना, जब integer division2देता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: