Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

arrays

Posted on 2000-05-13
2
Medium Priority
?
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
Ravindra76 earned 15 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

704 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