📜  Java中 print() 和 println() 的区别

📅  最后修改于: 2022-05-13 01:54:35.374000             🧑  作者: Mango

Java中 print() 和 println() 的区别

print() : Java中的 print() 方法用于在控制台上显示文本。此文本作为参数以字符串的形式传递给此方法。此方法在控制台上打印文本,并且光标保持在控制台上的文本末尾。下一次打印就是从这里进行的。各种 print() 方法:

例子:

Java
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        // The cursor will remain
        // just after the 1
        System.out.print("GfG1");
 
        // This will be printed
        // just after the GfG2
        System.out.print("GfG2");
    }
}


Java
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        // The cursor will after GFG1
        // will at the start
        // of the next line
        System.out.println("GfG1");
 
        // This will be printed at the
        // start of the next line
        System.out.println("GfG2");
    }
}


输出:
GfG1GfG2

println() : Java中的 println() 方法也用于在控制台上显示文本。此文本作为参数以字符串的形式传递给此方法。此方法在控制台上打印文本,并且光标保持在控制台下一行的开头。下一次打印从下一行开始。各种 println() 方法:

例子:

Java

import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        // The cursor will after GFG1
        // will at the start
        // of the next line
        System.out.println("GfG1");
 
        // This will be printed at the
        // start of the next line
        System.out.println("GfG2");
    }
}
输出:
GfG1
GfG2

print() 和 println() 的区别

println()print()
It adds new line after the message gets displayed.It does not add any new line.
It can work without arguments.This method only works with argument, otherwise it is a syntax error.