Question

what does the output means... if we print 'intarray' we get some hash code... & same is with 'floatarray' but if we print 'chararray' we doesn't get anything what does the folowing data means?????

class Test
{
    public static void main(String []aeg)
    {
        int []intarray = new int[5];
        char []chararray = new char[5];
        float []floatarray = new float[5];
        System.out.println(intarray);
        System.out.println(chararray);
        System.out.println(floatarray);
    }
 }

Output starts here

after printing on the console we get following output....

      F:\Mehnat\my sc\By me\ArrayList\2\2>javac Test.java
      F:\Mehnat\my sc\By me\ArrayList\2\2>java Test
      [I@546da8eb

      [F@6b6d079a

Output ends here

Was it helpful?

Solution 2

You're seeing this behavior because PrintStream.println has an overload that takes a char[]. From that method's documentation:

Prints an array of characters and then terminate the line.

Of course, the elements of your array haven't been initialized, so they are all the default char, which is '\u0000', the null character. If you populate the array with visible characters, you'll be able to see a result:

char[] charArray = new char[] {'a', 'b', 'c', 'd', 'e'};
System.out.println(charArray); //prints "abcde"

The other method calls are using println(Object), which prints the result of the object's toString. Arrays don't override toString, and so you see the result of the default Object.toString implementation:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

As a workaround, the Arrays utility class provides toString helper methods to get String representations of arrays. For example:

int[] intArray = new int[] {1, 2, 3, 4, 5};
char[] charArray = new char[] {'a', 'b', 'c', 'e', 'f'};
float[] floatArray = new float[] {1.0F, 1.1F, 1.2F, 1.3F, 1.4F};
System.out.println(Arrays.toString(intArray));   // [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(charArray));  // [a, b, c, e, f]
System.out.println(Arrays.toString(floatArray)); // [1.0, 1.1, 1.2, 1.3, 1.4]

OTHER TIPS

Printing an array of characters tries to print the array's content as a string (one character at a time). Your new array is filled with null characters which aren't printed. Try to fill it with something and check again:

chararray[0] = 'H';
chararray[1] = 'e';
chararray[2] = 'l';
chararray[3] = 'l';
chararray[4] = 'o';
System.out.println(chararray);

Should print Hello

Character Represented in 16-bit Unicode '\u0000' to '\uFFFF'. Can be treated as 16-bit unsigned integers in the range of [0, 65535] in arithmetic operations

        int []intarray = new int[5];

By default value 0;

            char []chararray = new char[5];

new array is filled with null characters which aren't printed

          float []floatarray = new float[5];




    Ex:


 class Whatever {
 public static void main(String args[]) {
 char[] hello = { 'h', 'e', 'l', 'l', 'o', '.'};
 String hello_str = new String(hello);
 System.out.println(hello + " " + hello_str);
 }
 }

It's because an array only contains memory addresses to each other element. It's called a "pointer" and is a programming concept that will probably thoroughly frustrate you in the future. However, printing a char[] array is overloaded as a String because a String is really just a char array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top