java system.out.println format code example

Example 1: system.out.println

public static void main(String[] args){
	System.out.println("Hello World"); 
}

Example 2: system.out.println

System.out.println("Hello World");

Example 3: java printf format string and int

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("================================");
            for(int i=0;i<3;i++){
                String s1=sc.next();
                int x=sc.nextInt();
                //Complete this line                
                System.out.printf("%-15s%03d%n",s1,x);
            }
            System.out.println("================================");

    }
}

Example 4: java system.out.println shortcut

--eclipse syso: ctrl+Space
--intelij sysout: ctrl+space

Example 5: java system.out.println

System.out.print(<string>); //prints in the same line as the previous print
System.out.println(<string>); //prints in a new line

// Example
System.out.print("This ");
System.out.print("will ");
System.out.print("be ");
System.out.print("all ");
System.out.print("in ");
System.out.print("one ");
System.out.print("line.");

System.out.println("Hello World!");
System.out.println("second line");

Tags:

Java Example