aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explain the output? ( Anonymous Arrays) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explain the output? ( Anonymous Arrays)" Watch "Explain the output? ( Anonymous Arrays)" New topic
Author

Explain the output? ( Anonymous Arrays)

Srikanth CS
Greenhorn

Joined: Apr 10, 2007
Posts: 9
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

Joined: Feb 28, 2007
Posts: 1707
Hi Srikanth,

Code has compiler error!!!


cmbhatt
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
As Chandra Bhatt pointed out, the code you have presented here is just wasting peoples' time.
Suman Sharma
Ranch Hand

Joined: May 16, 2005
Posts: 72
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

Joined: Mar 21, 2007
Posts: 28
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

Joined: May 16, 2005
Posts: 72
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

Joined: Feb 15, 2007
Posts: 557
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 ...


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 25, 2006
Posts: 1274
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

Joined: May 16, 2005
Posts: 72
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explain the output? ( Anonymous Arrays)