This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Array Initialization

 
Kiran Sonaje
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting lot compilation errors while compiling the program below.



The errors are occuring on all the lines commented in the above code.
I'm specifically interested to know why the line "hms = new float[2][5];" fails in compilation.
Can anybody please explain this to me?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You always need to be careful when declaring several arrays on one line.
The lineis actually decalring a two dimensional array of floats and a one dimensional array of floats. It is equivalent to

Declare all your arrays on separate lines and then it should be more clear.
 
Kiran Sonaje
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joanne,

Thanks for the reply.

For the above java code I am getting following compilation error




As per your suggetion if it is one dimensional array how does compiler complaining about 3 dimensional array. What you explained is solution for compilation and i am expecting what is the root cause for this? How JVM is handing this mechanism.

Waiting for reply .

Thanks

Kiran
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My mistake.
will declare a three dimensional array. So the reason for your compilation error is that you are trying to assign a two dimensional array to a three dimensional array reference.
 
Kiran Sonaje
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joanne

But this is not really i was expecting. I wanted why not how?

This is the answer, hope it will help you out.






Thanks

Kiran
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic