Solved

arrays

Posted on 2000-05-13
2
180 Views
Last Modified: 2010-04-01
Its told that arrays hold similar data?
but when declare array for object we store different datatype like Int,String,Float why this ?
0
Comment
Question by:queryelango
2 Comments
 
LVL 7

Accepted Solution

by:
Ravindra76 earned 5 total points
ID: 2806715

In that case arrays hold similar set of objects which may contain different data types. So the array definition meaning was not chagned. If You treat each object as a item , then arrays hold similar
set of items. But these items may or may not contain different  data types.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2806959
In Java, every class extends Object. The primitive types (byte, short, int, long, float, double, char) are not objects.

When you declare an array to hold one of these types, the compiler enforces the type-safety of the array by only allowing values of the primitive type to be stored in array elements.

When you declare an array to hold some kind of object, say String, the compiler will likewise enforce type-safety by only allowing object instances of type String or its subclasses (assuming one could subclass String) to be stored in array elements. Since every Java class extends Object, you can see that declaring an array to hold Objects means that the array can reasonably store instances of any Java class.

This same principle is in effect with the Java collection classes. Several of these, like Vector, actually use an array as the storage for the collection elements. Collection methods take or return Object instances. This is actually one area in which many have criticized Java because the creation of a type-safe collection takes considerably more effort than the use of templates in C++.

Best regards,
Jim Cakalic
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 80
eclipse package explorer vs project explorer view 2 132
micro services spring boot application error 3 194
web services creation SOAP vs REST 5 41
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question