Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Q26 Exam Marcus03  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is true -
The elements in a Java array can only be of primitive types, not objects. Why this is wrong?
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi:
arrays can hold objects and primitives. It depends on the decleration type
Object[] a = new Object[] //creats array of objects
int[] b = new int[] // creats array of integers
However, you can't assign a primitive type array to an Object type array.
b = a // compiler error
a = b // compiler error

and so on
 
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If arrays could not hold Objects it would not be possible to create an array of Strings.
 
My, my, aren't you a big fella. Here, have a tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!