Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array initialization Problem.

 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This Code Compiles Fine

int[] g={ new Integer(4),new Short("6"),new Character('c')};

but i get a Compilation error When try to write

int[] g={ new Integer(4),new Short(6),new Character('c')};

Although two Short Constructors are perfectly Legal

Please help me.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you haven't used both valid constructors
 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Short(String s), Short(short s) are Valid and that is what i did.

Constructor Summary
Short(short value)
Constructs a newly allocated Short object that represents the specified short value.
Short(String s)
Constructs a newly allocated Short object that represents the short value indicated by the String parameter.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, but i fear you invoked Short(int s)

Try to do the following and your code will compile:

 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why inside an Array an implicit casting is Required which is not in case we initialize outside an Array
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a matter of arrays.

This code won't compile as well, and for the same reason.


 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya i got that.. Thanks a lot Nicola...
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are always welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic