0

Currently, I'm learning arrays in JAVA. when I was trying to print reference variable of Primitive and non-primitive data types, I was getting usual results (as I have thought I will get) but when I tried to print the reference variable of 'Char' Datatype, I got characters back as a string result. can someone please explain why does it work like that? and plus if you know why the reference variable of string data type also gives different output as compared to others?

int[] arr1 = {1,2,3,4,5};
System.out.println(arr1); // Output : [I@e9e54c2

char[] arr2 = {'a','b','c','d','e'};
System.out.println(arr2); // Output : abcde

String[] arr3 = {"Luke", "Phill","Alex"};
System.out.println(arr3); // Output : [Ljava.lang.String;@6d311334
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 3
    About the `char[]`, here's why:https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/PrintStream.html#print(char%5B%5D) – Federico klez Culloca Mar 23 '22 at 15:12
  • This answer from Duncan Jones might be useful: https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – Komiii Mar 23 '22 at 15:13

0 Answers0