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


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


在 Java 中,我们有以下函数可以在控制台中打印任何内容。

  • 系统.out.print()
  • 系统.out.println()

但两者之间有一个细微的差别,即

系统.out.println()打印内容并在执行语句后切换到下一行,而

系统.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)"); 
    } 
}
输出
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)


相关用法


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