← Back to Core Java Course | Chapter 2: Input & Output | Lesson 2 of 6

Java में Numbers Print करना

System.out.println numeric values और arithmetic expressions के results सीधे print कर सकता है, और printf या String.format decimal places और thousands separators पर extra control देते हैं।

Printing Numeric Literals

System.out.println numeric literals सीधे print कर सकता है, बिना किसी conversion के, और number को बिल्कुल जैसा लिखा गया वैसा दिखाएगा, चाहे यह एक whole integer हो या एक decimal value।

उदाहरण: Printing Numeric Literals

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

Printing Arithmetic Results

जब println को एक mathematical expression दिया जाता है, Java पहले उस expression को पूरी तरह evaluate करता है और सिर्फ तब single resulting number print करता है, original expression text नहीं।

उदाहरण: Printing Arithmetic Results

java
public class Main {
	public static void main(String[] args) {
		System.out.println(5 + 3); // evaluated first - prints 8, not "5 + 3"
	}
}

Printing Different Number Types

println Java के हर numeric type के साथ काम करता है -- byte, short, int, long, float, और double -- और हर एक को उस type के अपने conventions के अनुसार formatted print करता है, जैसे printed output में कभी L suffix न दिखना।

उदाहरण: Printing Different Number Types

java
public class Main {
	public static void main(String[] args) {
		byte b = 10;
		long l = 100L;
		double d = 2.5;
		System.out.println(b);
		System.out.println(l); // no 'L' suffix in the printed output
		System.out.println(d);
	}
}

Concatenating Numbers with Text

जब एक number को + operator उपयोग करके एक String के साथ combine किया जाता है, Java concatenate करने से पहले अपने आप number को इसके String representation में convert करता है, यही कारण है कि numbers printed text में सीधे mix किए जा सकते हैं।

उदाहरण: Concatenating Numbers with Text

java
public class Main {
	public static void main(String[] args) {
		int age = 25;
		System.out.println("Age: " + age); // age auto-converted to a String
	}
}

Formatting Numbers in Output

किसी number के दिखने पर ज़्यादा control के लिए, printf और String.format decimal places की एक fixed संख्या के लिए %.2f या बड़े integers में thousands separators insert करने के लिए %,d जैसे format specifiers accept करते हैं।

उदाहरण: Formatting Numbers in Output

java
public class Main {
	public static void main(String[] args) {
		System.out.printf("%.2f%n", 3.14159);   // 2 decimal places
		System.out.printf("%,d%n", 1000000);    // thousands separators
	}
}
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. "Sum: " + 1 + 2 लिखना और 3 की उम्मीद करना, जब यह Sum: 12 print करता है; "Sum: " + (1 + 2) उपयोग करें।
  2. System.out.println(5 / 2) से 2.5 print होने की उम्मीद करना, जब integer division 2 print करता है।
  3. एक float literal पर f भूल जाना, इसलिए value एक double है और ज़्यादा digits के साथ print होती है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.