FaltuTech.Club : Fane of Advanced Logical Thriving Utopian Technical Club

Convert an Array to String in Java (Using java.util)

Convert an Array to String in Java (Using java.util)



import java.util.Arrays;
class test{
	public static void main(String args[]){
		
	int arr[] = {4,6,3,6,2,6};
	String str = Arrays.toString(arr);
	System.out.println("1. arr to string : " + str);
	System.out.println("2. class of str: " + str.getClass());
	System.out.println("3. class name of str: " + str.getClass().getName());
	System.out.println("4. class of arr : " + arr.getClass());
	System.out.println("5. class name of arr : " + arr.getClass().getName());
	
	}

}


Discussion

This example has been compiled with JDK 9

Output


1. arr to string : [4, 6, 3, 6, 2, 6]
2. class of str: class java.lang.String
3. class name of str: java.lang.String
4. class of arr : class [I
5. class name of arr : [I


We can see that type (class) of array can be retrived from getClass() and Name of Class with getName().

In next post we will use getClass and getName on all the data types and see what will be the results.