3

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

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Yogesh Verma
  • 59
  • 1
  • 9

4 Answers4

7

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

zmbq
  • 38,013
  • 14
  • 101
  • 171
5

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]
Community
  • 1
  • 1
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • @YogeshVerma: That's the string representation of the array. The symbols mean different things (and you can look up Java Bytecode if you're really curious - that's telling me it's an integer array at some address), but instead, if you want the data, use `Arrays.toString(intArray)`. – Makoto Aug 07 '13 at 05:41
  • Float []Floatarray = new Float[5]; System.out.println(Floatarray); we get [Ljava.lang.Float;@50c8d62f it means that the array is of Float type.... but in my program if we print primitive type array, does it means that it is also a class??? – Yogesh Verma Aug 07 '13 at 05:43
  • @YogeshVerma I'm not totally clear on what you're asking, but all arrays are objects (reference types), including primitive arrays. The "class name" of an `int[]` is "[I", for whatever reason. – Paul Bellora Aug 07 '13 at 05:51
  • @YogeshVerma No, array types are special and their classes are provided by the JVM. See this post for more info: http://stackoverflow.com/a/8546532/697449 – Paul Bellora Aug 07 '13 at 05:58
0

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);
 }
 }
Mohsin Shaikh
  • 494
  • 1
  • 4
  • 11
0

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.

  • but there not memory addresses in java. we can get only hash codes of the arrays???? i just wanna know what does ' [I@546da8eb' means????? – Yogesh Verma Aug 07 '13 at 06:04
  • Well, it's kind of a memory address. It's an identity hash code: a hash code used specifically to identify things. And yes, there are memory addresses in Java, you just can't apply them. Whenever you create an object, the object does not actually "contain" all of the values within, it only points to them. As a result, you can make an object have a field of itself and you will not run out of memory due to recursion. for example, Object o=new Object(); o.setObject(o); –  Aug 07 '13 at 06:09