Solved

arrays

Posted on 2000-05-13
2
192 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 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

630 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