Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explain the output? ( Anonymous Arrays)

 
Srikanth CS
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class A{
public static void main{
System.ou.println(new int[] {1,2,3,4,5});
}
}

What dose it print in the output screen and why???


Thank you in advance.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srikanth,

Code has compiler error!!!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you compile and run it? If not do so. If you do not understand the output that was printed, then ask again.

And please check our JavaRanch Naming Policy and change your displayed name to conform with it. I believe that you have been previously requested to do so in another topic's thread.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Chandra Bhatt pointed out, the code you have presented here is just wasting peoples' time.
 
Suman Sharma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srikanth,
It gives compiler error because of incorrect use of System.ou.println.
If it is a typing error and it should be Syste.out.println, it will output the memory address of the array object only. It does not print elements of the array object because it does not access array elements in this code.
-Suman
 
Vidya Singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct code
class A{
public static void main (String args[]){
System.out.println(new int[] {1,2,3,4,5});
}
}

would print memory address of Anonymous array
 
Suman Sharma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, it should be System.out.println, not Syste.out.println. My computer has a problem. Suddenly a black screen appears and I miss a letter even if I type it.
-Suman
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srikanth,

I found some interesting observation with your code.

output
[S@1b90b39 --> Sop(new short[] {1,2,3,4,5});
[I@1b90b39 --> Sop(new int[] {1,2,3,4,5});
[B@1b90b39 --> Sop(new byte[] {1,2,3,4,5});

1.Array's are represented as objects in java.
2.Object's toString() method displays fallowing information:
className@hashCode.

in our case its an array.
So for class name it has taken

S for short
I for int
B for byte.
fallowed by @ , 1b90b39(hashcode).

Please get back to me ...
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above pattern works for byte,short,int,float,double arrays.
But will not work for char and long !

Why doesn't work for char:Reason: PrintStream has function which takes char[] and prints the contents.
void println(char[] x)
Print an array of characters and then terminate the line.

Why doesn't work for long:
I too don't know.
But i got this result: [J@1b90b39.

I think its waste of time in certification view point.
Any way have nice time. I think we are crossing boundaries.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srini,

1- For long prefix "J" J@hashCode
2- for char some three boxes

Good observation! But reason unknown!


reply my equals() post please!!!

cmbhatt
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know their reasoning, but the class name for a long[] is [J.



prints [J

The reason that SOP acts differently with a long[], int[], float[], double[], etc. than with a char[] is that there is a method named println in PrintStream which matches char[], but the only println method that the other arrays match is the one that accepts Object.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both Suman and Vidya wrote that
(whatever) would print out the memory adress of (something).


No way.
Java is platform independent.

Bu.
 
Suman Sharma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Date problem.

Can anyone tell me how I can enter following data in publisher table?

PublishID, PublishDate, AuthId
111, 07-06-05, SSS

It is giving problem with entering the date value.

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic