← Back to Advanced Java Course | Chapter 4: Modern Java Features | Lesson 6 of 12

Java के Text Blocks

एक text block कई-line वाले text को बिल्कुल वैसे ही लिखने का एक तरीका है जैसा वह दिखता है, जैसे किसी paragraph को सीधे page पर copy करना। आपको बहुत सारे quotes और line breaks की ज़रूरत नहीं।
Syntax
java
String text = """
    first line
    second line
    """;

Introduction to Text Blocks

Text blocks आपको backslash line-continuation characters या अंतहीन string concatenation की ज़रूरत के बिना multi-line string literals लिखने देते हैं। एक text block तीन double quotes (""") से शुरू और खत्म होता है, और उन delimiters के बीच सब कुछ जैसे लिखा गया है वैसे ही बना रहता है, newlines सहित।

उदाहरण: Introduction to Text Blocks

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		String text = """
			Line one
			Line two""";
		// Print a line to the console
		System.out.println(text);
	}
}

Indentation Handling

Java किसी text block के अंदर indentation को अपने आप handle करता है: compile time पर, यह हर line द्वारा share किए गए common leading whitespace को तय करता है और हर एक से बिल्कुल उतना ही हटा देता है। यह आपको text block को अपने आस-पास के code से मेल खाने के लिए indent करने देता है बिना उस indentation के असली string value में लीक हुए।

उदाहरण: Indentation Handling

java
public class Main {
	public static void main(String[] args) {
		String text = """
				Hello
				World""";
		System.out.println(text); // common leading whitespace is stripped
	}
}

Escape Sequences

आप किसी text block के अंदर साधारण double quotes को बिना escape किए उपयोग कर सकते हैं, क्योंकि triple-quote delimiters वह अस्पष्टता हटा देते हैं। अगर आप output में एक असली line break दिखाए बिना text की एक लंबी logical line लिखना चाहते हैं, तो आप newline को suppress करने के लिए उस line को backslash (\\) से खत्म कर सकते हैं।

उदाहरण: Escape Sequences

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		String text = """
			She said "hello" without escaping. \
			This line joins the one above.""";
		// Print a line to the console
		System.out.println(text);
	}
}

Dynamic Formatting

आप block को एक template की तरह उपयोग करते हुए String.format() से, या साफ़ syntax के साथ बराबर behavior के लिए सीधे text block पर नए formatted() instance method को call करके runtime पर text block में values को dynamically format कर सकते हैं।

उदाहरण: Dynamic Formatting

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		String template = """
			Name: %s
			Age: %d""";
		// Print a line to the console
		System.out.println(template.formatted("Zoya", 28));
	}
}

Replacing HTML Templates

Text blocks Java source code के अंदर सीधे embedded HTML, SQL, या JSON templates लिखना और बनाए रखना कहीं आसान बना देते हैं, क्योंकि उन formats की multi-line structure अब visually संरक्षित की जा सकती है बजाय escaped, पढ़ने में मुश्किल single-line strings में flatten होने के।

उदाहरण: Replacing HTML Templates

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		String html = """
			<html>
			  <body>Hello</body>
			</html>""";
		// Print a line to the console
		System.out.println(html);
	}
}
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. opening """ की उसी line पर text रखना, जैसे """Hello, जो एक compile error है क्योंकि content को अगली line से शुरू होना चाहिए।
  2. यह न समझना कि closing """ की position मायने रखती है: अगर यह अपनी ही line पर हो तो string एक newline के साथ खत्म होती है और यह यह भी तय करता है कि कितनी indentation हटाई जाएगी।
  3. block के अंदर बिना escape किए तीन double quotes लिखना, जैसे \""", क्योंकि लगातार तीन text block को खत्म कर देंगे।

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.