how to print a String[] in java code example

Example 1: print in java

//print and create new line after
System.out.println("text");
System.out.println(String);
//You can use any variable type, not just strings, although
//they are the most common

//Print without creating a new line
System.out.print("text");
System.out.print(String);

Example 2: java print array

System.out.println(Arrays.toString(arr));

Example 3: how to print string array in java

import java.util.Arrays;
//How to print Arrays in Java
public class Printarray{
    
  public static void main(String[] args){
      
    String[] arr = new String[]{"Mumbai","Delhi","Kolkata","Chennai"};
    
    //Using for loop;
    for(int i=0; i<arr.length;i++){
      System.out.println(arr[i]);
    }
    
    //Using for-each loop;
    for(String city:arr){
      System.out.println(city);
    }
  
    //Using Arrays.toString() method
    System.out.println(Arrays.toString(arr));  
    
   
    //Using Arrays.deepToString() method its also converting multidimensional arrays to strings.
    
    System.out.println(Arrays.deepToString(arr));  
    
    //Using Array as list method
    System.out.println(Arrays.asList(arr));  
    
  }
  
}

Example 4: how to print something in java

System.out.print("one");
System.out.print("two");
System.out.println("three");

// RESULT = 'onetwothree'

Example 5: print statement in java

System.out.println("Hello!"); //prints then ends line
System.out.print("Hello!!");//prints without line spacing

Example 6: print in java

System.out.println(String someString); /** Can take in other types as well such as integers (ints) */

Tags:

Java Example