Solved

arrays

Posted on 2000-05-13
2
177 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
Comment Utility

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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 59
java set up 1 46
solarwind tftp server 2 30
Error with Java/Cache JDBC Classpath 2 19
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now