Java का System.out.println()
In this page:
System.out.println(value);
System.out.print(value);
Basic Printing
println() आप जो भी पास करें (numbers, strings, objects) उसे इसके text form में convert करता है और इसे console में लिखता है, फिर अपने आप output cursor को अगली line की शुरुआत में move करता है।
उदाहरण: Basic Printing
// 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("moves to the next line automatically");
}
}
Login to try C/C++/Java/PHP code in the editor
print() vs println()
print() cursor को वहीं छोड़ता है जहां output खत्म हुआ, इसलिए एक दूसरी print() call उसी line पर जारी रहती है -- कई statements में एक line of output बनाने के लिए उपयोगी, println() के विपरीत।
उदाहरण: print() vs println()
public class Main {
public static void main(String[] args) {
System.out.print("Same ");
System.out.print("line"); // continues right where the last print ended
}
}
Login to try C/C++/Java/PHP code in the editor
Concatenating Output
println() के अंदर + operator उपयोग करना, जैसे println("Score: " + score), non-string values को text में convert करता है और print करने से पहले उन्हें एक single string में जोड़ता है -- Java left से right evaluate करता है।
उदाहरण: Concatenating Output
public class Main {
public static void main(String[] args) {
int score = 90;
System.out.println("Score: " + score); // non-string joined into one string
}
}
Login to try C/C++/Java/PHP code in the editor
Printing Expressions
Java parentheses के अंदर कोई भी arithmetic पहले compute करता है -- println(2 + 3) 5 print करता है, literal text "2 + 3" नहीं -- इसलिए एक line में math और string concatenation mix करने के लिए operator precedence के साथ care चाहिए।
उदाहरण: Printing Expressions
public class Main {
public static void main(String[] args) {
System.out.println(2 + 3); // prints 5, not the text "2 + 3"
}
}
Login to try C/C++/Java/PHP code in the editor
Special Escape Characters
\n (newline) और \t (tab) जैसे Escape sequences आपको कई print statements की ज़रूरत के बिना एक single string literal के अंदर spacing और line breaks control करने देते हैं।
उदाहरण: Special Escape Characters
// 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("Line one\nLine two\tTabbed");
}
}
Login to try C/C++/Java/PHP code in the editor
print()उपयोग करना जब आप एक नई line चाहते हों, इसलिए अगली output उसी line पर दिखती है।- किसी string के अंदर quotes escape करना भूल जाना, जैसे
"He said "hi"", जो एक compile error है;\"उपयोग करें। System.out.println("a" + 1 + 2)लिखना और arithmetic की उम्मीद करना, जब numbers text के रूप में जुड़ते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: