Java中 print() 和 println() 的区别
print() : Java中的 print() 方法用于在控制台上显示文本。此文本作为参数以字符串的形式传递给此方法。此方法在控制台上打印文本,并且光标保持在控制台上的文本末尾。下一次打印就是从这里进行的。各种 print() 方法:
void print(boolean b) – Prints a boolean value. void print(char c) – Prints a character. void print(char[] s) – Prints an array of characters. void print(double d) – Prints a double-precision floating-point number. void print(float f) – Prints a floating-point number. void print(int i) – Prints an integer. void print(long l) – Prints a long integer. void print(Object obj) – Prints an object. void print(String s) – Prints a string.
例子:
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() 方法:
void println() – Terminates the current line by writing the line separator string. void println(boolean x) – Prints a boolean and then terminate the line. void println(char x) – Prints a character and then terminate the line. void println(char[] x) – Prints an array of characters and then terminate the line. void println(double x) – Prints a double and then terminate the line. void println(float x) – Prints a float and then terminate the line. void println(int x) – Prints an integer and then terminate the line. void println(long x) – Prints a long and then terminate the line. void println(Object x) – Prints an Object and then terminate the line. void println(String x) – Prints a String and then terminate the line.
例子:
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.