Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Array value, memory address or?

Tags:

java

arrays

This code

public static void main(String [] args){
     int c[] ={10,21,34,36,90,33,44,5};
     int tot = 0;
  for(int i:c){
      System.out.println(c);
   }
}

Prints out

 [I@1242719c
 [I@1242719c
 [I@1242719c
 [I@1242719c
 [I@1242719c
 [I@1242719c
 [I@1242719c
 [I@1242719c

I know I'm supposed to print out the int variable, but I'm curious as to what this means. Thanks

like image 854
Mob Avatar asked Dec 28 '22 15:12

Mob


1 Answers

You're printing out the internal representation of c. Essentially [I@1242719c breaks down to two important things. First, the [ indicates that you're printing an array. Second, the I indicates that it's an integer. Therefore, you're printing an array of integers! Try replacing the integer array with a string array and watch the I get replaced accordingly. From this page:

The name of an array's class has one open square bracket for each dimension plus a letter or string representing the array's type. For example, the class name for an array of ints is "[I". The class name for a three-dimensional array of bytes is "[[[B". The class name for a two-dimensional array of Objects is "[[Ljava.lang.Object". The full details of this naming convention for array classes is given in Chapter 6, "The Java Class File."

like image 162
Chris Bunch Avatar answered Jan 14 '23 01:01

Chris Bunch