← Back to Core Java Course | Chapter 6: Strings | Lesson 1 of 5

Java String परिचय

एक String text का एक टुकड़ा है, letter beads की एक string जैसी। एक बार बनने पर Java strings बदली नहीं जा सकतीं, और आप हर letter count या देख सकते हैं।
Syntax
java
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

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

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

java
public class Main {
	public static void main(String[] args) {
		String a = "hello";
		String b = "hello";
		System.out.println(a == b); // true: same pooled object
	}
}

String Immutability

Strings immutable हैं: एक बार बनने पर, एक String object के characters को कभी जगह पर नहीं बदला जा सकता।

हर method जो एक string को "modify" करता दिखता है — toUpperCase(), concat(), replace() — असल में एक बिल्कुल नया String object return करता है, original को untouched छोड़ते हुए।

उदाहरण: String Immutability

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

Checking String Length

length() method किसी String का total character count return करता है, spaces, punctuation, और digits सहित, सिर्फ letters नहीं। चूंकि यह एक method है (एक array की length जैसी field नहीं), parentheses याद रखें: str.length()।

उदाहरण: Checking String Length

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

Accessing Single Characters

charAt(index) एक दी zero-based position पर एक single character retrieve करता है, इसलिए charAt(0) पहला character पाता है। इसे string की length के बराबर या इससे आगे एक index के साथ call करना एक StringIndexOutOfBoundsException throw करता है।

उदाहरण: Accessing Single Characters

java
// 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));
	}
}
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. यह उम्मीद करना कि s.toUpperCase() s बदल देगा, जब strings immutable हैं और यह एक नया string return करता है।
  3. s.charAt(s.length()) access करना, जो StringIndexOutOfBoundsException throw करता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

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.