Java中 System.out.print() 和 System.out.println()函数的区别
在Java中,我们有以下函数可以在控制台中打印任何内容。
- System.out.print()和
- System.out.println()
但是两者之间有细微的差别,即
System.out.println()打印内容并在语句执行后切换到下一行,而
System.out.print()只打印内容,执行此语句后不切换到下一行。
以下示例将帮助您更突出地理解它们之间的区别。
示例 1:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
System.out.println("Welcome to JAVA (1st line)");
// this print statement will be printed in a new line.
System.out.print("Welcome to GeeksforGeeks (2nd line) ");
// this print statement will be printed in the same line.
System.out.print("Hello Geeks (2nd line)");
}
}
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
System.out.print("Welcome to JAVA (1st line) ");
// this print statement will be printed in the same line.
System.out.println("Welcome to GeeksforGeeks (1st line)");
// this print statement will be printed in a new line.
System.out.print("Hello Geeks (2nd line)");
}
}
输出
Welcome to JAVA (1st line)
Welcome to GeeksforGeeks (2nd line) Hello Geeks (2nd line)
示例 2:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
System.out.print("Welcome to JAVA (1st line) ");
// this print statement will be printed in the same line.
System.out.println("Welcome to GeeksforGeeks (1st line)");
// this print statement will be printed in a new line.
System.out.print("Hello Geeks (2nd line)");
}
}
输出
Welcome to JAVA (1st line) Welcome to GeeksforGeeks (1st line)
Hello Geeks (2nd line)