Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,754 members, 7,813,492 topics. Date: Tuesday, 30 April 2024 at 12:59 PM

Can An Array In Java Hold Multiple Types? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Can An Array In Java Hold Multiple Types? (4110 Views)

Java Array Problem / What The Different Between ENUM And ARRAY In Java / Community Service | I Am To Teach Everything I Know In Java For Free! (2) (3) (4)

(1) (2) (Reply) (Go Down)

Can An Array In Java Hold Multiple Types? by seunthomas: 5:40pm On Oct 05, 2016
Please we got into a small argument me and a NL user called @timtoday (@leprosy started making some noise also) about if this was possible.

I argued that since java was an object oriented language it was possible to create a new object which extends array and could handle that.

I am open to opinions, can this assertion be 100% true.
Re: Can An Array In Java Hold Multiple Types? by larisoft: 6:57pm On Oct 05, 2016
To the best of my knowledge; the assertion that a java array can contain different types is false.

You could do this of course by making your objects extend one parent object, or implement a common interface, but then, they wouldnt be 'different types' anymore.

Also, to the best of my knowledge, arrays in java cannot be extended.

1 Like

Re: Can An Array In Java Hold Multiple Types? by lekropasky(m): 8:20pm On Oct 05, 2016
Seriously, this guy is dumb. You finally clear my doubt of you being a programmer, how can someone keep ranting over what he doesnt know for Gods sake. Shit!!!. Do you think nairaland is full of novice/dummy programmers? Even, a two weeks old Java programmer knows that storing different objects in an array is not possible.
class User {
private int userID;
}
class Admin {
private int adminID;
}
User[] users = new User[2];
users[0] = new Admin();

Any real Java programmer must be very sure that the code above is not possible to implement. And here is seunthomas ranting about shit, hes not even ashame of himself. I really mean this, Go And Learn....i can help you with that if you need it.
Re: Can An Array In Java Hold Multiple Types? by FincoApps(m): 8:31pm On Oct 05, 2016
larisoft:
To the best of my knowledge; the assertion that a java array can contain different types is false.

You could do this of course by making your objects extend one parent object, or implement a common interface, but then, they wouldnt be 'different types' anymore.

Also, to the best of my knowledge, arrays in java cannot be extended.


Exactly!!
Re: Can An Array In Java Hold Multiple Types? by FincoApps(m): 8:37pm On Oct 05, 2016
I don't think an Array can hold different data types.... Seunthomas, I saw your code on the other thread where you created an Array of Objects.

Now it might seem like this Array is holding different types but since the Object class is the Parent of all other classes in Java, automatically all other classes are of type Object.

So technically you are putting the same type into the Array
Re: Can An Array In Java Hold Multiple Types? by 2mNaira: 8:40pm On Oct 05, 2016
larisoft:
To the best of my knowledge; the assertion that a java array can contain different types is false.

You could do this of course by making your objects extend one parent object, or implement a common interface, but then, they wouldnt be 'different types' anymore.

Also, to the best of my knowledge, arrays in java cannot be extended.


Does java have the tuple dats type.
Re: Can An Array In Java Hold Multiple Types? by silento(m): 9:00pm On Oct 05, 2016
I don't code in java but in c
yes
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 9:06pm On Oct 05, 2016
larisoft:
To the best of my knowledge; the assertion that a java array can contain different types is false.

You could do this of course by making your objects extend one parent object, or implement a common interface, but then, they wouldnt be 'different types' anymore.

Also, to the best of my knowledge, arrays in java cannot be extended.

Then check this and explain if your assertion is true.
class Test{



public static void main(String args[]){
boolean status=true;
char c='Y';
int v=100;
Object s[]=new Object[3];
s[0]=status;
s[1]=c;
s[2]=v;

System.out.println(s[0].getClass().getName());
System.out.println(s[1].getClass().getName());
System.out.println(s[2].getClass().getName());

}

}


There is actually a class Array which is an extension of the primitive type [] and that can be extended.

1 Like

Re: Can An Array In Java Hold Multiple Types? by seunthomas: 9:09pm On Oct 05, 2016
FincoApps:
I don't think an Array can hold different data types.... Seunthomas, I saw your code on the other thread where you created an Array of Objects.

Now it might seem like this Array is holding different types but since the Object class is the Parent of all other classes in Java, automatically all other classes are of type Object.

So technically you are putting the same type into the Array
They are actually not of type object rather the primitive types are upcasted to their prevalent java.lang type.

But in the real sense all data structures by design are flexible.

My first experience with this type of thing was in objective c and i later discovered truely in object oriented programming its possible to store almost anything in an array or list.
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 9:12pm On Oct 05, 2016
lekropasky:
Seriously, this guy is dumb. You finally clear my doubt of you being a programmer, how can someone keep ranting over what he doesnt know for Gods sake. Shit!!!. Do you think nairaland is full of novice/dummy programmers? Even, a two weeks old Java programmer knows that storing different objects in an array is not possible.
class User {
private int userID;
}
class Admin {
private int adminID;
}
User[] users = new User[2];
users[0] = new Admin();

Any real Java programmer must be very sure that the code above is not possible to implement. And here is seunthomas ranting about shit, hes not even ashame of himself. I really mean this, Go And Learn....i can help you with that if you need it.


You dont know anything. Shaking my head for people like you. Dumb cannot define you.
Re: Can An Array In Java Hold Multiple Types? by danidee10(m): 11:34pm On Oct 05, 2016
In Summary Java is just a shitty language....where the simplest things are surrounded by stupid idioms and patterns that make the developers feel that they've learnt some amazing skill when they finally learn the "Workaround" pattern.

Deep down Experienced Java programmers know the language sucks balls but they won't admit it because they've invested a lot of their time learning it and the weird ways to do some things (How do methods not have default arguments ).

Don't bother replying cuz i've made that conclusion a long time ago....and will only change it if someone pays me a million per hour to write java.

#JavaTroll cool
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 11:42pm On Oct 05, 2016
Java is a language of languages.

It takes the lessons learnt from several languages.

Most times people don't understand the OOP in Java and they are confused because of its strong typing enforcing rule.
Re: Can An Array In Java Hold Multiple Types? by Nobody: 1:12am On Oct 06, 2016
seunthomas:

Then check this and explain if your assertion is true.
class Test{



public static void main(String args[]){
boolean status=true;
char c='Y';
int v=100;
Object s[]=new Object[3];
s[0]=status;
s[1]=c;
s[2]=v;

System.out.println(s[0].getClass().getName());
System.out.println(s[1].getClass().getName());
System.out.println(s[2].getClass().getName());

}

}


There is actually a class Array which is an extension of the primitive type [] and that can be extended.

All I can say is....PRAISE THA LORD! You broke your "all talk and no code" streak, wonderful.

1 Like

Re: Can An Array In Java Hold Multiple Types? by larisoft: 4:59am On Oct 06, 2016
seunthomas:

Then check this and explain if your assertion is true.
class Test{



public static void main(String args[]){
boolean status=true;
char c='Y';
int v=100;
Object s[]=new Object[3];
s[0]=status;
s[1]=c;
s[2]=v;

System.out.println(s[0].getClass().getName());
System.out.println(s[1].getClass().getName());
System.out.println(s[2].getClass().getName());

}

}


There is actually a class Array which is an extension of the primitive type [] and that can be extended.


In ur example, u are putting these types as their parent object (java.lang.object) and I already mentioned that while you could do this, you'd no longer be putting different types in one array as they are cast to their common type first.

Calling getclass on the object of course calls the overriding child method instead of the parent class and that's why you get the results you get.
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 5:13am On Oct 06, 2016
larisoft:


In ur example, u are putting these types as their parent object (java.lang.object) and I already mentioned that while you could do this, you'd no longer be putting different types in one array as they are cast to their common type first.

Calling getclass on the object of course calls the overriding child method instead of the parent class and that's why you get the results you get.
You are actually right and wrong at the same time. In my array at compilation time, i am putting in primitive types which are all different types into the array. Simply am inserting totally different types at compilation and the compiler naturally would throw errors but because like i said in my other threads concept of "everything is an object in java" (Which was also argued at by @timtoday) the compiler actually accepts this values.

But the magic you describe at runtime, where the JVM now allocates enough memory to hold a generic object for our array.

But in plain text and for a layman, our array is taking different types but the JVM is converting it into java objects(which by the way happens to every primitive type) during runtime. So irrespective of if you do this:

int x[]=int[3];
the JVM will still convert into the associated Java type Integer which extends object.
Re: Can An Array In Java Hold Multiple Types? by FincoApps(m): 6:10am On Oct 06, 2016
DanielTheGeek:


All I can say is....PRAISE THA LORD! You broke your "all talk and no code" streak, wonderful.

I wanted to say the same o
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 6:12am On Oct 06, 2016
FincoApps:


I wanted to say the same o

Like really, go and check my past posts.

I write code a lot here.

Una just wan be rebel.

Plain and simple.

angry angry angry angry angry
Re: Can An Array In Java Hold Multiple Types? by FincoApps(m): 6:49am On Oct 06, 2016
seunthomas:


Like really, go and check my past posts.

I write code a lot here.

Una just wan be rebel.

Plain and simple.

angry angry angry angry angry

In that your popular thread with dhtml, I glanced at your profile from time to time... nothingggg.... It appears recently, you have started recovering from your Ego boost and acting normally

1 Like

Re: Can An Array In Java Hold Multiple Types? by seunthomas: 7:11am On Oct 06, 2016
FincoApps:


In that your popular thread with dhtml, I glanced at your profile from time to time... nothingggg.... It appears recently, you have started recovering from your Ego boost and acting normally

I no dey abnormal na.

I am actually cool.

My actions na just filmtrick.

I think the drama makes the threads interesting.

cool

Sometimes when i imagine the faces of those who engage me in arguments i just laugh.
LWKMD.

By the way i write code a lot......................
Never been a day i did not write a few hundred lines of code in the languages i know.
Re: Can An Array In Java Hold Multiple Types? by Nobody: 9:17am On Oct 06, 2016
seunthomas:


By the way i write code a lot......................
Never been a day i did not write a few hundred lines of code in the languages i know.

I think this may be a lie or over-exaggeration, sometimes I take a day break from coding, sometimes on Sundays and when I code in those days, I don't code in all the languages I know and I think it's almost impossible to actively single-handedly code synchronously in all your lingua-francas in just a single complete rotation of the earth except if all the languages you know aren't >= 5.
Re: Can An Array In Java Hold Multiple Types? by larisoft: 9:18am On Oct 06, 2016
seunthomas:

You are actually right and wrong at the same time. In my array at compilation time, i am putting in primitive types which are all different types into the array. Simply am inserting totally different types at compilation and the compiler naturally would throw errors but because like i said in my other threads concept of "everything is an object in java" (Which was also argued at by @timtoday) the compiler actually accepts this values.

But the magic you describe at runtime, where the JVM now allocates enough memory to hold a generic object for our array.

But in plain text and for a layman, our array is taking different types but the JVM is converting it into java objects(which by the way happens to every primitive type) during runtime. So irrespective of if you do this:

int x[]=int[3];
the JVM will still convert into the associated Java type Integer which extends object.

At this point, I think we are just saying the same thing now.
Re: Can An Array In Java Hold Multiple Types? by timtoday: 9:37am On Oct 06, 2016
seunthomas:

You are actually right and wrong at the same time. In my array at compilation time, i am putting in primitive types which are all different types into the array. Simply am inserting totally different types at compilation and the compiler naturally would throw errors but because like i said in my other threads concept of "everything is an object in java" (Which was also argued at by @timtoday) the compiler actually accepts this values.

But the magic you describe at runtime, where the JVM now allocates enough memory to hold a generic object for our array.

But in plain text and for a layman, our array is taking different types but the JVM is converting it into java objects(which by the way happens to every primitive type) during runtime. So irrespective of if you do this:

int x[]=int[3];
the JVM will still convert into the associated Java type Integer which extends object.


Good morning everyone.

@seunthomas, Please you probably should not have brought me up into this.
I saw your post, though I went through it there was no need making a comments.
Why? Because it is obvious you are living in your own reality with alot of false claims.
But since you called me up by name, I will yet help you see clearly that you don't know
what you claim you know.

Secondly, am responding because I felt you think that those who post more are likely to be
right or correct. Multiplying words on falsehood doesn't make such claims correct and the loudest person
in the room is not the usually the correct ones!

So, let me try one more time and see if you will get it.

@larisoft, mentioned to you that your code "WORKED" because
by making your objects extend one parent object,
or implement a common interface, but then, they wouldn't be 'different types' anymore.

Your response:

Then check this and explain if your assertion is true.
class Test{



public static void main(String args[]){
boolean status=true;
char c='Y';
int v=100;
Object s[]=new Object[3];
s[0]=status;
s[1]=c;
s[2]=v;

System.out.println(s[0].getClass().getName());
System.out.println(s[1].getClass().getName());
System.out.println(s[2].getClass().getName());

}

}



My Comments:
If anyone runs that code of yours but have no idea what his going on such will immediately conclude
like you, that your answer to @FincoApps was TOTALLY right when you said

They are actually not of type object rather the primitive types are upcasted to their prevalent java.lang type.


But of course, with a little pain of code, the obvious is revealed! How?

Instead of using this

System.out.println(s[0].getClass().getName());
System.out.println(s[1].getClass().getName());
System.out.println(s[2].getClass().getName());


like you did, do this


System.out.println(s[0].getClass().getSuperclass().getSuperclass().getName());
System.out.println(s[1].getClass().getSuperclass().getSuperclass().getName());
System.out.println(s[2].getClass().getSuperclass().getSuperclass().getName());


Maybe then you will get what I have explained to you in the previous post and what others are
now telling you.

They are all ACTUALLY a type of Object class. ARRAY in Java has a single TYPE!

Secondly, you also said this
There is actually a class Array which is an extension of the primitive type [] and that can be extended.
Array "class" by design take the type they "refer" and as such are instances of such type and "sir" you can't extended,
of course nothing is impossible. Maybe you want to extend Java itself! Array are not like conventional classes! Get that sir?
And I don't think you can extend class java.util.Arrays. Please NOTE the "S".

Still on this your assertion:

They are actually not of type object rather the primitive types are upcasted to their prevalent java.lang type.
ABBA Oga sir?! shocked
The primitive types, you gave in your code are NOT just UPCASTED! They were autoboxED in other words wrapped
if you will into their reprepective Wrapper Classes, which in turn are autoupcast into their MOTHER (or super if you will) class.
That is why I have use the methods getSuperclass twice to make you see it!

But if you had used instanceof like I told you in the previous post maybe you would have "healed" of this confusion!
I don't expect you to anyway!

Now to the part where you called me up!

You are actually right and wrong at the same time. In my array at compilation time, i am putting in primitive types which are all
different types into the array. Simply am inserting totally different types at compilation and the compiler naturally would throw
errors but because like i said in my other threads concept of "everything is an object in java" (Which was also argued at by @timtoday)
the compiler actually accepts this values.

But the magic you describe at runtime, where the JVM now allocates enough memory to hold a generic object for our array.

But in plain text and for a layman, our array is taking different types but the JVM is converting it into java
objects(which by the way happens to every primitive type) during runtime. So irrespective of if you do this:

int x[]=int[3];
the JVM will still convert into the associated Java type Integer which extends object.

@larisoft was right and wrong at the same time? But you didn't tell where he was wrong and where he was right!
You are only trying to play safe!
More so, the reason why your code works was NOT because "everything is an object in java", Please stop saying
that it make you look ____. I can't say it because am 2 yrs old remember? A lang with primitive types, that needed
a wrapper class of it's type wouldn't be such where "everything is an object"!

Oh! because the compilers didn't scream out with errors makes you believe "everything is an object in java"?
If only, you have been writing Java since "Odun gbogboro" like you said, you should have know that before
Java 1.5 i.e Java5, the compiler would have handed you errors, but thanks to autoboxing and her cousins!

Moreso, in plain text, (and if you will always tell layman the absolute truth, cos they will find out with time!).
Which makes this statement
But in plain text and for a layman, our array is taking different types but the JVM is converting it into java
objects(which by the way happens to every primitive type) during runtime. So irrespective of if you do this:

int x[]=int[3];
the JVM will still convert into the associated Java type Integer which extends object.
of yours NOT TRUE!

What you called "conversion" doesn't happen EXPECT under some conditions, which is not part of your last quote.
You might have to go check that out! Am sure you will tell me you have read it all! Which is obvious you have not!

Try get the class name of "x" as you have it and check it's super class, then input primitive type int into the
array and try to dereference it like we did with the Object array previously!

Seriously, it would do you good to REALLY know what you are taking about! Or if you are Satisfied with where you are
please just stay in the thread where all you do is chest-beating and abuses. And if you must come up with some
study answering questions please just DON'T bring me in!

No, i don't hate you, in fact, you make me laugh hard with several of your "funny" assertion. But I hate to stay up hitting the same
point for several post!
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 9:44am On Oct 06, 2016
timtoday:



Good morning everyone.

@seunthomas, Please you probably should not have brought me up into this.
I saw your post, though I went through it there was no need making a comments.
Why? Because it is obvious you are living in your own reality with alot of false claims.
But since you called me up by name, I will yet help you see clearly that you don't know
what you claim you know.

Secondly, am responding because I felt you think that those who post more are likely to be
right or correct. Multiplying words on falsehood doesn't make such claims correct and the loudest person
in the room is not the usually the correct ones!

So, let me try one more time and see if you will get it.

@larisoft, mentioned to you that your code "WORKED" because

Your response:

Then check this and explain if your assertion is true.
class Test{



public static void main(String args[]){
boolean status=true;
char c='Y';
int v=100;
Object s[]=new Object[3];
s[0]=status;
s[1]=c;
s[2]=v;

System.out.println(s[0].getClass().getName());
System.out.println(s[1].getClass().getName());
System.out.println(s[2].getClass().getName());

}

}



My Comments:
If anyone runs that code of yours but have no idea what his going on such will immediately conclude
like you, that your answer to @FincoApps was TOTALLY right when you said


But of course, with a little pain of code, the obvious is revealed! How?

Instead of using this

System.out.println(s[0].getClass().getName());
System.out.println(s[1].getClass().getName());
System.out.println(s[2].getClass().getName());


like you did, do this


System.out.println(s[0].getClass().getSuperclass().getSuperclass().getName());
System.out.println(s[1].getClass().getSuperclass().getSuperclass().getName());
System.out.println(s[2].getClass().getSuperclass().getSuperclass().getName());


Maybe then you will get what I have explained to you in the previous post and what others are
now telling you.

They are all ACTUALLY a type of Object class. ARRAY in Java has a single TYPE!

Secondly, you also said this

Array "class" by design take the type they "refer" and as such are instances of such type and "sir" you can't extended,
of course nothing is impossible. Maybe you want to extend Java itself! Array are not like conventional classes! Get that sir?
And I don't think you can extend class java.util.Arrays. Please NOTE the "S".

Still on this your assertion:

ABBA Oga sir?! shocked
The primitive types, you gave in your code are NOT just UPCASTED! They were autoboxED in other words wrapped
if you will into their reprepective Wrapper Classes, which in turn are autoupcast into their MOTHER (or super if you will) class.
That is why I have use the methods getSuperclass twice to make you see it!

But if you had used instanceof like I told you in the previous post maybe you would have "healed" of this confusion!
I don't expect you to anyway!

Now to the part where you called me up!


@larisoft was right and wrong at the same time? But you did tell where he was wrong and where he was right!
You are only trying to play safe!
More so, the reason why your code works was NOT because "everything is an object in java", Please stop saying
that it make you look ____. I can't say it because am 2 yrs old remember? A lang with primitive types, that needed
a wrapper class of it's type wouldn't be such where "everything is an object"!

Oh! because the compilers didn't scream out with errors makes you believe "everything is an object in java"?
If only, you have been writing Java since "Odun gbogboro" like you said, you should have know that before
Java 1.5 i.e Java5, the compiler would have handed you errors, but thanks to autoboxing and her cousins!

Moreso, in plain text, (and if you will always tell layman the absolute truth, cos they will find out with time!).
Which makes this statement
of yours NOT TRUE!

What you called "conversion" doesn't happen EXPECT under some conditions, which is not part of your last quote.
You might have to go check that out! Am sure you will tell me you have read it all! Which is obvious you have not!

Try get the class name of "x" as you have it and check it's super class, then input primitive type int into the
array and try to dereference it like we did with the Object arrays previously!

Seriously, it would do you good to REALLY know what you are taking about! Or if you are Satisfied with where you are
please just stay in the thread where all you do is chest-beating and abuses. And if you must come up with some
study answering questions please just DON'T bring me in!

No, i don't hate you, in fact, you make me laugh hard with several of your "funny" assertion. But I hate to stay up hitting the same
point for several post!

There is no point trying to reason with you.
LOL.
Re: Can An Array In Java Hold Multiple Types? by timtoday: 9:51am On Oct 06, 2016
seunthomas:


There is no point trying to reason with you.
LOL.

I don't expect you to! Please just don't call me up on ridiculous post like this!
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 9:59am On Oct 06, 2016
timtoday:


I don't expect you to! Please just don't call me up on ridiculous post like this!

I could take my time to write you a proof but it wont make any difference.

Anyway no harm meant. Just having fun.

grin
Re: Can An Array In Java Hold Multiple Types? by okwyee(m): 10:11am On Oct 06, 2016
seunthomas:


I could take my time to write you a proof but it wont make any difference.

Anyway no harm meant. Just having fun.

grin
Mr seun, i really think you have an unfinished business here www.nairaland.com/3385948/nland-programmers-need-advise

and I've been waiting anxiously for a while now, at least reply my mail if you don't want to continue on the thread sad
Re: Can An Array In Java Hold Multiple Types? by asalimpo(m): 12:17pm On Oct 11, 2016
in dynamic languages, yes. Because they're typeless.
In static langauges like java...
Bro, you've go study java 101 more thoroughly and understand polymorphism cuz
this your argument displays some confusion on your part in this area.

array format is
<type> <identifier> <dimension> = new <type> <dimension>
e.g
String stringArray[][] = new String[1][2];

so if an array can contain multiple types , which are not of the same family (descendants of the same type)
how will the array instantiation statement be defined?

<type1><type2><typeN> <identifier> <dimension> = ...?
String, Integer, File [][] multiTypeArray = new String,Integer,File[3][5];

Sorry bro, but this wont work.
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 5:19pm On Oct 11, 2016
asalimpo:
in dynamic languages, yes. Because they're typeless.
In static langauges like java...
Bro, you've go study java 101 more thoroughly and understand polymorphism cuz
this your argument displays some confusion on your part in this area.

array format is
<type> <identifier> <dimension> = new <type> <dimension>
e.g
String stringArray[][] = new String[1][2];

so if an array can contain multiple types , which are not of the same family (descendants of the same type)
how will the array instantiation statement be defined?



<type1><type2><typeN> <identifier> <dimension> = ...?
String, Integer, File [][] multiTypeArray = new String,Integer,File[3][5];

Sorry bro, but this wont work.



Really dont need another e-battle this week. Generally by design array datatypes support same or different types. Maybe you should do some more research about this topic. You will be surprised...
Re: Can An Array In Java Hold Multiple Types? by asalimpo(m): 6:35pm On Oct 11, 2016
seunthomas:


Really dont need another e-battle this week. Generally by design array datatypes support same or different types. Maybe you should do some more research about this topic. You will be surprised...
Static languages cannot have heterogenous arrays. Simply impossible by design.
Polymorphism makes it seem that a container can hold multiple types. But that's only if the other types are
descendants of a single ancestor.
In which case they are upcast to their parent.
And stored as the parent type which is still one type.

Anyway,
here's a reference for you :
" A Java array is an ordered collection of primitives, object references, or other arrays.
Java arrays are homogeneous : except as allowed by polymorphism, all elements of an array must be of the same type.
Each variable is referenced by array name and its index.
Arrays may have one or more dimensions. "-http://www.java2s.com/Book/Java/0020__Language-Basics/Introduction_to_Arrays.htm
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 8:32pm On Oct 11, 2016
asalimpo:

Static languages cannot have heterogenous arrays. Simply impossible by design.
Polymorphism makes it seem that a container can hold multiple types. But that's only if the other types are
descendants of a single ancestor.
In which case they are upcast to their parent.
And stored as the parent type which is still one type.

Anyway,
here's a reference for you :
" A Java array is an ordered collection of primitives, object references, or other arrays.
Java arrays are homogeneous : except as allowed by polymorphism, all elements of an array must be of the same type.
Each variable is referenced by array name and its index.
Arrays may have one or more dimensions. "-http://www.java2s.com/Book/Java/0020__Language-Basics/Introduction_to_Arrays.htm

Array datatype can hold multiple types even in statically typed OOP languages. The whole idea behind OOP is that you can always create a new type. Like i said earlier, go and research a little more. lol
Re: Can An Array In Java Hold Multiple Types? by asalimpo(m): 8:43pm On Oct 11, 2016
I've done my research . Give some examples.

An array can hold only one type- if it holds seemingly different types,they are subtypes of the array type.

like i said,it is impossible with static languages to have heterogenous arrays.
<type> <identifier> . is the usual syntax.
Not <type> <type2> <type3> etc <identifier>.

e.g
Number numArray[] = null;
this array can hold Integers, Double, Floats etc
but all these types are subtypes of the array type , Number.
when stored in the array, they are stored as Number, not as Integer,Double etc.

So if you want to make your point provide some code.
Re: Can An Array In Java Hold Multiple Types? by mogozi: 8:48pm On Oct 11, 2016
@ seunthomas
I know what happened to you. You actually read a lot of programming resources but haven't gotten an actual opportunity to code major jobs. So you end up drawing false conclusions.
Fincoapps ,larrysoft and timtoday....
This dude has only programmed for himself so he thinks the world of his miserly outputs.

***Every class or object in java inherits from Objects.
***So any arrays of class Objects is logically able to be manipulated. But common sense should raise an alarm if you are any good.
Imagine the rich security measures in java your foolish code just bypassed.
I'm not done yet... I'll prove it to you with facts...

(1) (2) (Reply)

Pls I Need Help In My Final Year Project:creating An Online Recruitment System / What Is Your Favorite Language For Numerical Analysis & Scientific Computation? / Dual Boot IOS 8 On Android ( NO ROOT)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 131
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.