當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。