Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,389 members, 7,808,374 topics. Date: Thursday, 25 April 2024 at 11:06 AM

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

Nairaland Forum / Science/Technology / Programming / Can An Array In Java Hold Multiple Types? (4105 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)

Re: Can An Array In Java Hold Multiple Types? by seunthomas: 8:51pm On Oct 11, 2016
asalimpo:
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.



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());

}

}

I already gave this example.

Its simple and clear.


We have an Object type and we insert primitives of different types into it.

If indeed a java array cant hold other types, the file would not be compiled.

Except you have another explanation.

We are talking about the compilation stage which is stage 1.

Explain why the compiler is not throwing up compilation errors but actually compiling the file or is this a bug in javac??
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 8:54pm On Oct 11, 2016
mogozi:
@ 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...

Prove what you already failed the first step.

Thats why i recommend having a better tertiary education.

How can you explain stuff when you are missing the foundation.
Re: Can An Array In Java Hold Multiple Types? by asalimpo(m): 9:17pm On Oct 11, 2016
seunthomas:




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());

}

}

I already gave this example.

Its simple and clear.


We have an Object type and we insert primitives of different types into it.

If indeed a java array cant hold other types, the file would not be compiled.

Except you have another explanation.

We are talking about the compilation stage which is stage 1.

Explain why the compiler is not throwing up compilation errors but actually compiling the file or is this a bug in javac??

you still repeating me.
the format is:
<type> <identifier> = ... initialization side.
then the array can hold any object that is a a type or subtype of its type.
e.g
if the array is type Object it can hold types of Object or its subtype.
But object is abstract so it can't be instantiated.
It holds only types of type char, int, boolean , because those are subtypes of type Object
Boxing is taking place behind the scenes to convert the primitives to their Object conterpart before storing them in the array.

what your are doing is similar to what one would do with generics and collections.
Create the collection to hold a higher type but store lower types, descendants , in it.
That doesnt mean that collections can hold different non-related types.
Arrays cannot hold different non-related types.
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 9:19pm On Oct 11, 2016
asalimpo:


you still repeating me.
the format is:
<type> <identifier> = ... initialization side.
then the array can hold any object that is a a type or subtype of its type.
e.g
if the array is type Object it can hold types of Object or its subtype.
But object is abstract so it can't be instantiated.
It holds only types of type char, int, boolean , because those are subtypes of type Object
Boxing is taking place behind the scenes to convert the primitives to their Object conterpart before storing them in the array.

what your are doing is similar to what one would do with generics and collections.
Create the collection to hold a higher type but store lower types, descendants , in it.
That doesnt mean that collections can hold different non-related types.
Arrays cannot hold different non-related types.
char,int,boolean are not sub types.
They are primitives. Primitives are the basic of every language. They are not built from class Object.

This may be difficult to explain to people sha. Anyway like i said last time thread was stopped.

Lets forget it. Since you guys dont understand it.
Re: Can An Array In Java Hold Multiple Types? by asalimpo(m): 10:03pm On Oct 11, 2016
seunthomas:

char,int,boolean are not sub types.
They are primitives. Primitives are the basic of every language. They are not built from class Object.

This may be difficult to explain to people sha. Anyway like i said last time thread was stopped.

Lets forget it. Since you guys dont understand it.

they are boxed into an object type.
converted from primitives to Objects behind the scene.
That's y you can store them in an array of objects and call getClass().getName()
on them.
You can't do that on a primitive.

Integer y = new Integer(20);// explicit integer object is created

Integer z = 5; // boxing takes place converting the primitive ,5, into an object and storing it in a referrence z.

Your using Boxing/Unboxing to justify your claim. Cool down.
You got it wrong.
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 10:11pm On Oct 11, 2016
asalimpo:


they are boxed into an object type.
converted from primitives to Objects behind the scene.
That's y you can store them in an array of objects and call getClass().getName()
on them.
You can't do that on a primitive.

Integer y = new Integer(20);// explicit integer object is created

Integer z = 5; // boxing takes place converting the primitive ,5, into an object and storing it in a referrence z.

Your using Boxing/Unboxing to justify your claim. Cool down.
You got it wrong.

DUDE OLODO Like you i have not seen before.

I had students like you sha, you practically have to give them factory reset before they can understand very basic stuff.


You are not boxing unboxing anything while compiling. Like i said you require factory reset.
Re: Can An Array In Java Hold Multiple Types? by asalimpo(m): 10:40pm On Oct 11, 2016
seunthomas:


DUDE OLODO Like you i have not seen before.

I had students like you sha, you practically have to give them factory reset before they can understand very basic stuff.


You are not boxing unboxing anything while compiling. Like i said you require factory reset.
It's done automatically . Behind the scenes, not something you as a programmer have to do manually.
Java arrays are homogenous . python, php are otherwise.
Others have corrected on this but you feel every1 is wrong except you.
You use primiives stored in Object array as justification for your case,when you dont even understand the mechanism behind the scene.
fyi, that code wouldnt compile on java 4 or earlier because boxing/unboxing wasnt introduced then.
Re: Can An Array In Java Hold Multiple Types? by larisoft: 4:52pm On Oct 12, 2016
Honestly.... I cant believe this is actually being debated!!!! Its so funny!
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 4:55pm On Oct 12, 2016
larisoft:
Honestly.... I cant believe this is actually being debated!!!! Its so funny!

Maybe beyond just reading books and documentation online, you should try to really study JVM behaviour.

Most of the assertion asalimpo and so many others made here are not accurate. I took the liberty to actually do verification in code.

But whats the point, once you dont know something you assume thats were all the knowledge in the world ends.

@larisoft In all honesty, i actually dont think you should even be commenting considering the best way to implement security in an app you build is by doing this:
(char) (toInt(c) - this.seed);

Am not about bringing people down anyway else i would make your code public, but i recommend you do something about that in caselaw, i may change my mind and just use that app for a thread lesson.
Re: Can An Array In Java Hold Multiple Types? by larisoft: 5:18pm On Oct 12, 2016
seunthomas:


Maybe beyond just reading books and documentation online, you should try to really study JVM behaviour.

Most of the assertion asalimpo and so many others made here are not accurate. I took the liberty to actually do verification in code.

But whats the point, once you dont know something you assume thats were all the knowledge in the world ends.

@larisoft In all honesty, i actually dont think you should even be commenting considering the best way to implement security in an app you build is by doing this:
(char) (toInt(c) - this.seed);

Am not about bringing people down anyway else i would make your code public, but i recommend you do something about that in caselaw, i may change my mind and just use that app for a thread lesson.

Please do! I'm sure a lot of us will benefit from such exposition. When developing an app, my strategy is to move quickly, release and Guage user response and then decide whether or not to continue. Your losing money every minute you spend fortifying something that may not work.

In the app you mentioned, security was the least of my worries and user validation was everything. Otherwise, you wouldn't have been able to decompile it in the first place.

The app is currently free since it wasn't validated as I would have liked. Dhtml18 was the first person to even comment on that hole but from my perspective user response was everything and if the users responded positively, one could go ahead to make the app fortified

In ur characteristic maturity, you imagined that that is my best way to secure an app...


Just feel free to do what you will with. It's open source now.

All these guys arguing against you are fools and you are the only great programmer on nl and we just can't see it because, your too far ahead... I understand.
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 5:23pm On Oct 12, 2016
larisoft:


Please do! I'm sure a lot of us will benefit from such exposition. When developing an app, my strategy is to move quickly, release and Guage user response and then decide whether or not to continue. Your losing money every minute you spend fortifying something that may not work.

In the app you mentioned, security was the least of my worries and user validation was everything. Otherwise, you wouldn't have been able to decompile it in the first place.

The app is currently free since it wasn't validated as I would have liked. Dhtml18 was the first person to even comment on that hole but from my perspective user response was everything and if the users responded positively, one could go ahead to make the app fortified

So feel free to do what you will with. It's open source now.


Dont worry am not like that.

Contrary to your view, am actually a very cool guy.

Its just many times a lot of you guys amaze me.

I could spend time explaining a lot of things here but whats the point una go still dey argue like say una sabi.

What i have over most of you guys is experience.

But seriously are you saying i should go ahead and publish your content as well as do a review thread on your work

I have not called anyone a fool except you are calling yourself that.

The point is most of you guys only limit yourself to what you find online.

Me i make mentors of the men that changed this profession, that is why i am better than a lot of you.

I know there are few who would read my postings and agree with what am saying intellectually but not my approach (I can be a pain sometimes).

What i wont do sha is be the YES guy.

Like i posted here, if you seek enlightenment on this subject matter do a very thorough research online.


LOL.
Re: Can An Array In Java Hold Multiple Types? by seunthomas: 5:41pm On Oct 12, 2016
Okay @larisoft, sorry. But next time dont be cocky and try to mind your own business.
Re: Can An Array In Java Hold Multiple Types? by ugwum007(m): 4:21pm On Nov 29, 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.

Boolean status; might be primitive or class.

The instances of Java classes are definitely objects so you are inevitably adding an object to that object array.

Conclusion: arrays in Java can hold different types of multiple data.
Re: Can An Array In Java Hold Multiple Types? by uwem18: 12:59am On Nov 30, 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.

Only Arraylist can contain different Types and not array

(1) (2) (Reply)

I'm Giving Out These Tech Books / Pls I Need Help In My Final Year Project:creating An Online Recruitment System / What Is Your Favorite Language For Numerical Analysis & Scientific Computation?

(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. 49
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.