Java String परिचय
In this page:
String variable = "text";
String variable = new String("text");
Creating Strings
Java में एक String characters के एक immutable sequence को represent करने वाला एक object है, और आप एक या तो एक literal ("hello") से बना सकते हैं या explicitly new String("hello") से, हालांकि literal form लगभग हमेशा पसंद किया जाता है।
दोनों approaches वही methods वाला एक पूरी तरह usable String object produce करते हैं।
उदाहरण: Creating Strings
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare a and set it to "hello"
String a = "hello";
// Create a new String object and store it in b
String b = new String("hello");
// Print a line to the console
System.out.println(a);
// Print a line to the console
System.out.println(b);
}
}
Login to try C/C++/Java/PHP code in the editor
Understanding the String Pool
Memory बचाने के लिए, Java string literals को string pool नाम के एक special region में store करता है, इसलिए identical text वाली दो literals असल में memory में बिल्कुल उसी object की ओर point करती हैं बजाय दो अलग copies के।
आप यह देख सकते हैं दो identical literals के लिए == true compare होने से, जो बिल्कुल वही कारण है कि string content compare करने के लिए == गलत tool है।
उदाहरण: Understanding the String Pool
public class Main {
public static void main(String[] args) {
String a = "hello";
String b = "hello";
System.out.println(a == b); // true: same pooled object
}
}
Login to try C/C++/Java/PHP code in the editor
String Immutability
Strings immutable हैं: एक बार बनने पर, एक String object के characters को कभी जगह पर नहीं बदला जा सकता।
हर method जो एक string को "modify" करता दिखता है — toUpperCase(), concat(), replace() — असल में एक बिल्कुल नया String object return करता है, original को untouched छोड़ते हुए।
उदाहरण: String Immutability
public class Main {
public static void main(String[] args) {
String original = "hello";
String upper = original.toUpperCase();
System.out.println(original); // unchanged
System.out.println(upper); // new string
}
}
Login to try C/C++/Java/PHP code in the editor
Checking String Length
length() method किसी String का total character count return करता है, spaces, punctuation, और digits सहित, सिर्फ letters नहीं। चूंकि यह एक method है (एक array की length जैसी field नहीं), parentheses याद रखें: str.length()।
उदाहरण: Checking String Length
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare text and set it to "Hello World"
String text = "Hello World";
// Print a line to the console
System.out.println(text.length());
}
}
Login to try C/C++/Java/PHP code in the editor
Accessing Single Characters
charAt(index) एक दी zero-based position पर एक single character retrieve करता है, इसलिए charAt(0) पहला character पाता है। इसे string की length के बराबर या इससे आगे एक index के साथ call करना एक StringIndexOutOfBoundsException throw करता है।
उदाहरण: Accessing Single Characters
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare text and set it to "Hello"
String text = "Hello";
// Print a line to the console
System.out.println(text.charAt(0));
}
}
Login to try C/C++/Java/PHP code in the editor
equals()के बजाय==से strings compare करना।- यह उम्मीद करना कि
s.toUpperCase()sबदल देगा, जब strings immutable हैं और यह एक नया string return करता है। s.charAt(s.length())access करना, जोStringIndexOutOfBoundsExceptionthrow करता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: