Format text using printf() method in java

printf method in java:

  • Print() and println() methods are used to print text and object values or format data.
  • In order to format text we also have printf() method in java
  • Formatting data means displaying corresponding data type value. 
  • For example when print float ot double value if we want to specify upto 2 decimal numbers we user.
  • System.out.printf("%.2f", variable); 
  • Also alignment of string data. when we are printing string data in java using print() and pintf() 



#1. Write a program to print string using print() and printf() methods


  1. package printfinjava;
  2. /**
  3.  * How to format text using java printf() method
  4.  * @author www.instanceofjava.com
  5.  */
  6.  
  7. public class PrintfMethod {
  8.  
  9.     public static void main(String[] args) {
  10.         String str="java printf double";
  11.         
  12.         System.out.println ("String is "+str);
  13.         System.out.printf ("String is %s", str);
  14.  
  15.     }
  16.  
  17. }
Output:

  1. String is java printf double
  2. String is java printf double

java printf table



printf in java double int string


Format double using printf():
  • We can format double using printf() method in java
  • format double to 2 decimal places in java possible by using system.out.printf() method.

#2. Write a program to print double to 2 decimal places in java

  1. package printfinjava;
  2. /**
  3.  * How to format text using java printf() method
  4.  * @author www.instanceofjava.com
  5.  */
  6.  
  7. public class PrintfMethod {
  8.  
  9. public static void main(String[] args) {
  10.  
  11.      double value=12.239344;
  12.      System.out.printf("%.2f", value);
  13.  
  14. }
  15.  
  16. }
Output:

  1. 12.24

#3. Write a program to format text using printf() method in java


printf in java double

5 Different ways to print arrays in java

  •  Arrays in java are used to hold similar data types values.
  • System.out.print() method does not print values of array.
  • In order to print array values we have different ways.
  • Normally we use for loop and by using index we will print each element inside array.
  • let us see how many ways we can able to print values of array. 


1.Print array in java using for loop
  • How to print array in java using for loop?
  • Yes we can print arrays elements using for loop.
  • Find the length of the array using array.length and take initial value as 0 and repeat until  array.length-1. 
  • Then access each index values of an array then print.
#1. Write a program to print array in java using for loop

  1. package arraysinterview;
  2.  
  3. public class PrintArray {
  4.  
  5.     /**
  6.      * How to print java array using for loop
  7.      * @author www.instanceofjava.com
  8.      */
  9.     
  10. public static void main(String [] args){
  11.         
  12.         int[] array = { 12,13,8,34,2,7,9,43,54,21};
  13.         
  14.         for (int i = 0; i < array.length; i++) {
  15.             System.out.println(array[i]);
  16.         }
  17.         
  18. }
  19.  
  20. }

Output:

  1. 12
  2. 13
  3. 8
  4. 34
  5. 2
  6. 7
  7. 9
  8. 43
  9. 54
  10. 21

2. Print string array in java using Enhanced for loop

  • From java 1.5 using enhanced for loop also we can print array values.

#2.   How to print string array in java using for each loop



3. Using Arrays.toString(array) method
  • By  using Arrays.toString(array) method we can print array values.

#3 Write a program to print array in java using Arrays.toString(array)

  1. package arraysinterview;
  2. import java.util.Arrays;
  3.  
  4. public class PrintArray {
  5.     /**
  6.      * How to print java array using Arrays.toString(array)
  7.      * @author www.instanceofjava.com     */
  8.  
  9.   public static void main(String [] args){
  10.         
  11.         String[] array = { "hi", "hello", "java"};
  12.         
  13.         
  14.    System.out.println(Arrays.toString(array));
  15.                
  16.   }
  17. }

Output:

  1. [hi, hello, java]
4.Using Arrays.deepToString(array) Method

  • Arrays,deepToString(array) method added in java 5 with generics and varargs.

#4 Write a program to print array in java using  Arrays.deepToString(array)

  1. package arraysinterview;
  2. import java.util.Arrays;
  3.  
  4. public class PrintArray {
  5.     /**
  6.      * How to print java array using Arrays.deepToString(array)
  7.      * @author www.instanceofjava.com     */
  8.  
  9.   public static void main(String [] args){
  10.         
  11.         int[][] array = new int[][]{
  12.                 {1,2,3},
  13.                 {11,12,13},
  14.                 {4 ,5,6},
  15.                 };
  16.         
  17.   System.out.println(Arrays.deepToString(array));
  18.       
  19.                
  20.   }
  21. }

Output:
  1. [[1, 2, 3], [11, 12, 13], [4, 5, 6]]

5.Using Arrays.asList(array) Method

  • Arrays,asList(array) method we can print array elements

#5 Write a program to print array in java using  Arrays.asList(array)

  1. package arraysinterview;
  2. import java.util.Arrays;
  3.  
  4. public class PrintArray {
  5.     /**
  6.      * How to print java array using Arrays.asList(array)
  7.      * @author www.instanceofjava.com     */
  8.  
  9.   public static void main(String [] args){
  10.         
  11.       int[] array ={1,2,3,4,5,6};        
  12.      System.out.println(Arrays.asList(array));
  13.  
  14.      String[] strarray ={"hi","array","print"};
  15.       System.out.println(Arrays.asList(strarray));
  16.                
  17.   }
  18. }

Output:
  1. [[I@2a139a55]
  2. [hi, array, print]

Exception handling multiple choice interview questions and answers in java

Java objective type questions and answers on static keyword



6.What is the output of below program ?

java multiple choice questions with answers

x=10 x=10
x=10 x=20
x=30 x=30
x=30 x=20




Java mcq with answers on this keyword




Core java multiple choice questions with answers on method overloading




Select Menu