当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java print()和println()的区别用法及代码示例


Java中的print():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.

例:

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"); 
    } 
}
输出:

GfG1GfG2

Java中的println():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.

例:

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()
消息分发后,它将添加新行。 它不会添加任何新行。
它可以不带参数地工作。 此方法仅适用于参数,否则为语法错误。

相关用法


注:本文由纯净天空筛选整理自AnshulVaidya大神的英文原创作品 Difference between print() and println() in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。