• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Array initialization

 
Ranch Hand
Posts: 118
Android Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


out put for the above program is

[[I@45bab50a
0

cause the first one is trying to print the referrence variable that is the reason we are getting the 2d array class name @ symbol and hexa code. Now for the second output since we did not give values to it so by default its taking 0 since the array data type is int. Now if there is a slight change in code



the output being

[[I@8dc8569
[I@45bab50a

x is point to the referrence variable of the array and x[0] is the referrence variable to the second layer array.

is my assumption correct. Please do correct me if am wrong.




 
Marshal
Posts: 76811
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

krishnadhar Mellacheruvu wrote:. . . print the referrence variable that is the reason we are getting the 2d array class name @ symbol and hexa code. . . .

I am afraid that is incorrect. For a start there is no such thing as a 2D array. All Java® arrays are 1D. That is a 1D array of 1D arrays. And yes there is a difference. Arrays of arrays are better than 2D arrays because it is easier to create a jagged array.

The corret style for declaring arrays of arrays is int[][] myArray ... No space before [][] but 1 space after it.

No, that is not why you get that particular output. You get that particular output because arrays do not override the Object#toString() method and that returns class name, an at sign and hash code in hex.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

campbell

is it because that every time i am trying to print a reference variable (not initialized) but has default values the toSring() method is being called(i.e. implemented) which prints the reference variable's output.... ?
 
Campbell Ritchie
Marshal
Posts: 76811
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This applies to everything except a char[]:-
When you call System.out.println(),

the API documentation wrote:This method calls at first String.valueOf(x) to get the printed object's string value.

… except for Strings which don't need to be turned into Strings, then as if it used this print() method followed by println() which moves you to the next line. [Primitives can also be turned into Strings with the appropriate valueOf method of the String class.] If you go to the String#valueOf(Object) method, it tells you that if the reference passed points to null, it returns “null”, otherwise it calls the toString() method. You will see from that last link that you get class name, the at sign, and the hash code in hex. If you have overridden the toString method, you get something different, but you can see in the Java® Language Specification that the only overridden method in an array is clone(). If you look up the Class#getName() method, it tells you what sort of result you get from class names for arrays.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic