Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,143,506 members, 7,781,557 topics. Date: Friday, 29 March 2024 at 04:58 PM

What The Different Between ENUM And ARRAY In Java - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / What The Different Between ENUM And ARRAY In Java (3657 Views)

Java Array Problem / Can An Array In Java Hold Multiple Types? / Community Service | I Am To Teach Everything I Know In Java For Free! (2) (3) (4)

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

What The Different Between ENUM And ARRAY In Java by HOJOSKID: 5:25pm On Oct 03, 2016
plz someone should help are they the same thing and is enum use for storing stuffs like array sad
Re: What The Different Between ENUM And ARRAY In Java by elfico(m): 5:47pm On Oct 03, 2016
Enums are like defined named constants. they are user-defined data types.
For example an enum called Months could be defined as follows:
enum months {
january = 1;
febraury =2;
.....
....
}

when you want to use the above

year thismonth, lastmonth;

thismonth = january;
lastmonth = march;

arrays on the other hand are used to store similar data e.g to store thr age of 20 students, you declare an array of size 20.
Re: What The Different Between ENUM And ARRAY In Java by HOJOSKID: 5:57pm On Oct 03, 2016
so enums is not use for storage inshort
Re: What The Different Between ENUM And ARRAY In Java by kentho(m): 6:11pm On Oct 03, 2016
I think enums are basically used to enforce a list of pre defined options that must be adhered to. For example.. True or false... Only one can be chosen at a time.. External values won't be accepted
Re: What The Different Between ENUM And ARRAY In Java by seunthomas: 6:22pm On Oct 03, 2016
@elfico is actually right on enum but i want to slightly differ on arrays. Arrays may not necesarily always store the same type. There are specialized arrays that can hold any value but they still follow basic rules guiding array datatype.
Re: What The Different Between ENUM And ARRAY In Java by HOJOSKID: 6:57pm On Oct 03, 2016
you guys are still not getting me right my definition of array is<<< array is a data type and also a container which can be use to store varables of same data types
so my question is is enum also container use for storing values cos i didnt really get what an enum is plz help a newbeeis oooo
Re: What The Different Between ENUM And ARRAY In Java by seunthomas: 7:21pm On Oct 03, 2016
HOJOSKID:
you guys are still not getting me right my definition of array is<<< array is a data type and also a container which can be use to store varables of same data types
so my question is is enum also container use for storing values cos i didnt really get what an enum is plz help a newbeeis oooo

Both are data types that hold a value.

However enum holds a predefined value while the value of arrays can be changed at runtime.

1 Like

Re: What The Different Between ENUM And ARRAY In Java by Nobody: 7:32pm On Oct 03, 2016
err let me ENUMERATE my ANNOTATIONS a little. . .
Re: What The Different Between ENUM And ARRAY In Java by elfico(m): 8:06pm On Oct 03, 2016
seunthomas:
@elfico is actually right on enum but i want to slightly differ on arrays. Arrays may not necesarily always store the same type. There are specialized arrays that can hold any value but they still follow basic rules guiding array datatype.
Thanks. I actually posted as per general rule. I am not conversant with java. In javascript, array holds multiple types. In C#, arraylist also holds multiple types, but array must specify the type they hold and I believe this holds true especially for strongly-typed languages.

1 Like

Re: What The Different Between ENUM And ARRAY In Java by timtoday: 8:39pm On Oct 03, 2016
seunthomas:
@elfico is actually right on enum but i want to slightly differ on arrays. Arrays may not necesarily always store the same type. There are specialized arrays that can hold any value but they still follow basic rules guiding array datatype.

I disagree in this sense ...
A "vanilla" array will store the same type in JAVA and a few of her cousins like C and C++.

E.g

final int SIZE = 10;
int[] numbers = new int[SIZE]; // you can't include Strings


Even if you write a class and make an array of it. Though the object of that class consist of various datatypes, you will still only be able to store the "type" of that class in it's array, even if you make a subclass of it.

E.g


class PersonDemo {
public static void main(String[] args) {
Person[] human = new Person[3];

human[0] = new Person("Name", 12);
human[1] = new Person("My Name", 15);

human[2] = new Person(false, null); // you can't do this
}
}

class Person {
private String name;
private int age;

Person(String name, int age) {
this.name = name;
this.age = age;
}
// set and do other stuff...
String getName() {
return this.name;
}
};

It is quite different to what one can do in languages like Perl, Python and some others.
Just saying...
Re: What The Different Between ENUM And ARRAY In Java by timtoday: 8:43pm On Oct 03, 2016
elfico:
Thanks. I actually posted as per general rule. I am not conversant with java. In javascript, array holds multiple types. In C#, arraylist also holds multiple types, but array must specify the type they hold and I believe this holds true especially for strongly-typed languages.

In Java, the likes of ArrayList<type>() also holds multiple types unlike the "vanilla" or plain array, which of course is the first data structure programmers are introduced to in so many programming languages.
Re: What The Different Between ENUM And ARRAY In Java by silento(m): 8:59pm On Oct 03, 2016
I don't know for java but in c language

enum is defined constant name for integer value for easy remembrance for the programmer

array is a collection of data or value address

in a real word

we know that we have 12 month in a year and enum of months can go like this

enum month{jan,Feb,...}

and
an array may contain how many days are in each month
ex.
days[12]={30,31,34,....}

to access how many days that is in February you can just like

days[Feb]

instead of remembering that February is the second month of the year


so with this example

enumeration type is a defined name for an integer value for easy remembrance

1 Like 1 Share

Re: What The Different Between ENUM And ARRAY In Java by seunthomas: 9:54pm On Oct 03, 2016
timtoday:


I disagree in this sense ...
A "vanilla" array will store the same type in JAVA and a few of her cousins like C and C++.

E.g

final int SIZE = 10;
int[] numbers = new int[SIZE]; // you can't include Strings


Even if you write a class and make an array of it. Though the object of that class consist of various datatypes, you will still only be able to store the "type" of that class in it's array, even if you make a subclass of it.

E.g


class PersonDemo {
public static void main(String[] args) {
Person[] human = new Person[3];

human[0] = new Person("Name", 12);
human[1] = new Person("My Name", 15);

human[2] = new Person(false, null); // you can't do this
}
}

class Person {
private String name;
private int age;

Person(String name, int age) {
this.name = name;
this.age = age;
}
// set and do other stuff...
String getName() {
return this.name;
}
};

It is quite different to what one can do in languages like Perl, Python and some others.
Just saying...

The beauty of inheritance in object oriented programming is that you can actually create new types from one type.

Therefore you can have a class inheriting from type Array and it will able to store different objects.
Re: What The Different Between ENUM And ARRAY In Java by timtoday: 10:02pm On Oct 03, 2016
silento:
I don't know for java but in c language

enum is defined constant name for integer value for easy remembrance for the programmer

array is a collection of data or value address

in a real word

we know that we have 12 month in a year and enum of months can go like this

enum month{jan,Feb,...}

and
an array may contain how many days are in each month
ex.
days[12]={30,31,34,....}

to access how many days that is in February you can just like

days[Feb]

instead of remembering that February is the second month of the year


so with this example

enumeration type is a defined name for an integer value for easy remembrance

You are right.
The beauty of all these is that Java took C lang., to another level by even making it alot easier to use enum.

Compare this:

Code in C lang.


#include <stdio.h>

#define MONTHS 12

typedef enum Month {
JAN = 1, FEB, MAR
} Month;

int main(void) {

int Days[MONTHS] = {31, 28, 31,};
Month month = FEB;

printf("%-5d %3d\n", month, Days[month-1]);

return 0;
}


Note that it is the programmers responsibility, to take care of the "one-off" since, array and enum in C, will start from 0. Same in Java, but it is all take care off.

Code in JAVA

class MonthDemo {
public static void main (String[] args) {
Month month = Month.FEB;
System.out.print(month.prints());
}
}

enum Month {
JAN(31),
FEB(28),
MAR(31);

private int days;

Month(int days) {
this.days = days;
}

String prints() {
return String.format("%-5s %3d\n", this, this.days);
}
};
Re: What The Different Between ENUM And ARRAY In Java by Nobody: 10:17pm On Oct 03, 2016
[aside]
Can someone please explain ANNOTATIONS in JAVA
[/aside]
Re: What The Different Between ENUM And ARRAY In Java by timtoday: 10:19pm On Oct 03, 2016
seunthomas:


The beauty of inheritance in object oriented programming is that you can actually create new types from one type.

Therefore you can have a class inheriting from type Array and it will able to store different objects.

Am sure you know that array is not a TYPE in itself.
If you inherit from a class say Employee, with a subclass say HourlyEmployee. The best you can have stored in the ARRAY instant object of Employee is HourlyEmployee, you can't store into it a class TYPE of class say Student. And that is if you can even instantiate Employee to start with, that is if Employee is not an abstract class or an interface.

In the same vein, you can't store Strings in the ARRAY of TYPE Integers or Doubles... It doesn't work in JAVA that way, like I said in my previous post.

Storing different sub-classes of a super-class in the ARRAY of that super-class is not the same as storing different objects of DIFFERENT types like you have in Perl, JavaScript and the likes. Except you would like to show that all datatypes are the same as regards "vanilla" ARRAYs.

One of the reasons for other "containers" like we have it C lang., and her cousins like Cpp, Java and the likes.
Re: What The Different Between ENUM And ARRAY In Java by seunthomas: 10:25pm On Oct 03, 2016
timtoday:


Am sure you know that array is not a TYPE in itself.
If you inherit from a class say Employee, with a subclass say HourlyEmployee. The best you can have stored in the ARRAY instant object of Employee is HourlyEmployee, you can't store into it a class TYPE of class say Student. And that is if you can even instantiate Employee to start with, that is if Employee is not an abstract class or an interface.

In the same vein, you can't store Strings in the ARRAY of TYPE Integers or Doubles... It doesn't work in JAVA that way, like I said in my previous post.

Storing different sub-classes of a super-class in the ARRAY of that super-class is not the same as storing different objects of DIFFERENT types like you have in Perl, JavaScript and the likes. Except you would like to show that all datatypes are the same as regards "vanilla" ARRAYs.

One of the reasons for other "containers" like we have it C lang., and her cousins like Cpp, Java and the likes.

In java everything is an object. There is the primitive type array and an object type Array built on it. So maybe you should do some more research.
Re: What The Different Between ENUM And ARRAY In Java by timtoday: 10:37pm On Oct 03, 2016
seunthomas:

In java everything is an object. There is the primitive type array and an object type Array built on it. So maybe you should do some more research.

I don't need to do any research, I gave you examples of what am saying. Maybe you need to do more research as everything in Java is NOT an OBJECT. Java is not Ruby! Please don't get it confused.
Every primitive type has what is called a Wrapper Class Type which is an Object. Though these can take the primitive datatype but it does mean that the primitive datatype in Java is an Object.

More so, there is a class called Arrays, from java.util.Arrays. Used to make working with arrays of different datatypes a bit easier. That also doesn't make an ARRAY of a TYPE a datatype in itself. So what other research, do you think you will like to recommend?

In Java, an ARRAY of a type holds a single type! Plain and simple! Please don't complicate things.
Re: What The Different Between ENUM And ARRAY In Java by seunthomas: 10:40pm On Oct 03, 2016
timtoday:


I don't need to do any research, I gave you examples of what am saying. Maybe you need to do more research as everything in Java is NOT an OBJECT. Java is not Ruby! Please don't get it confused.
Every primitive type has what is called a Wrapper Class Type which is an Object. Though these can take the primitive datatype but it does mean that the primitive datatype in Java is an Object.

More so, there is a class called Arrays, from java.util.Arrays. Used to make working with arrays of different datatypes a bit easier. That also doesn't make an ARRAY of a TYPE a datatype in itself. So what how research, do you think you will like to recommend?

In Java, an ARRAY of a type holds a single type! Plain and simple! Please don't complicate things.

The first thing you are ever taught in any java class is "In Java everything is an object". Are you saying this assertion is wrong. This also plays out in every object oriented language.
Re: What The Different Between ENUM And ARRAY In Java by timtoday: 10:47pm On Oct 03, 2016
seunthomas:


The first thing you are ever taught in any java class is "In Java everything is an object". Are you saying this assertion is wrong. This also plays out in every object oriented language.

Whoever taught that need go check his or her book again! Am sure, you read my last post and why everything in Java is not an object! Except Ruby is now the NEW Java!

Every OOP language "implements" the OO ideals to varying degrees. If the assertion is true that all OOP lang has everything as Object, then the statement will be true that "In Cpp everything is an object"!!! I laugh in Bangladeshi!
Re: What The Different Between ENUM And ARRAY In Java by seunthomas: 10:53pm On Oct 03, 2016
timtoday:


Whoever taught that need go check his or her book again! Am sure, you read my last post and why everything in Java is not an object! Except Ruby is now the NEW Java!

Every OOP language "implements" the OO ideals to varying degrees. If the assertion is true that all OOP lang has everything as Object, then the statement will be true that "In Cpp everything is an object"!!! I laugh in Bangladeshi!

The whole concept of OOP is that to fundamentally grasp it you have to see everything as an object.

I took a very long time to learn this concept.

I could recommend a book for you to go and read.

OOP is a programming pattern and not necessarily a language thing.

You can actually decide to write any language in OOP and you will still conform to that language syntax and semantics.

If you need more help understanding this may you can contact me directly.


But you can read Bruce Eckel's Book "Thinking in Java" or the C++ variant "Thinking in C++"
Re: What The Different Between ENUM And ARRAY In Java by timtoday: 11:16pm On Oct 03, 2016
seunthomas:


The whole concept of OOP is that to fundamentally grasp it you have to see everything as an object.

I took a very long time to learn this concept.

I could recommend a book for you to go and read.

OOP is a programming pattern and not necessarily a language thing.

You can actually decide to write any language in OOP and you will still conform to that language syntax and semantics.

If you need more help understanding this may you can contact me directly.


But you can read Bruce Eckel's Book "Thinking in Java" or the C++ variant "Thinking in C++"

You don't get it, do you?
I really don't care the YEARS you used in learning the concept of OO. On the ARRAY stuff in Java, bros you miss am! Go back and check what I wrote, I know the OOP is not specific to a lang., However, i have been specific in this thread since what we are taking about is Java, a lang, that implement OOP.

Need more help? From whom? If with all the experience you often claim you don't know that ARRAY in Java are of single type. I think you need to come down from your high horse of self-importance, and take a second look at yourself and everything around you. You think more highly of yourself, than you really worth!

Contact you?! So that you will tell me that "everything in Java is an Object"? Over-the-bar!

As for both Bruce Eckel's Books, I have them. In fact, I can give you the third one, which is Thinking in C++, Volume 2. I bet you don't know there are two volumes to that book!

So, thanks. No thanks. I think I have to say good-nite... I better be watching a movie than having this discussion.

1 Like

Re: What The Different Between ENUM And ARRAY In Java by seunthomas: 11:30pm On Oct 03, 2016
timtoday:


You don't get it, do you?
I really don't care the YEARS you used in learning the concept of OO. On the ARRAY stuff in Java, bros you miss am! Go back and check what I wrote, I know the OOP is not specific to a lang., However, i have been specific in this thread since what we are taking about is Java, a lang, that implement OOP.

Need more help? From whom? If with all the experience you often claim you don't know that ARRAY in Java are of single type. I think you need to come down from your high horse of self-importance, and take a second look at yourself and everything around you. You think more highly of yourself, than you really worth!

Contact you?! So that you will tell me that "everything in Java is an Object"? Over-the-bar!

As for both Bruce Eckel's Books, I have them. In fact, I can give you the third one, which is Thinking in C++, Volume 2. I bet you don't know there are two volumes to that book!

So, thanks. No thanks. I think I have to say good-nite... I better be watching a movie than having this discussion.

I have all the volumes of his book. So i dont need them.

Arrays can be of single type or can hold multiple types. Its simple, a derivative of a type is also a "type of that type", so a derivative of array is also a type of array.

If you dont believe this to be true, i can do a quick code to show you what i mean.

A derivative of array that can hold multiple objects.

I think your confusion is simple, you see array only as "[]" and not class called "Array" which is a derivative of [].
Re: What The Different Between ENUM And ARRAY In Java by timtoday: 11:53pm On Oct 03, 2016
seunthomas:


Arrays can be of single type or can hold multiple types.

The high-lighted words of yours is WRONG!! And I have shown that even in my first post!

seunthomas:

Its simple, a derivative of a type is also a "type of that type", so a derivative of array is also a type of array.

If you dont believe this to be true, i can do a quick code to show you what i mean.

A derivative of array that can hold multiple objects.

Go back and read what I posted as far back as 10.19pm. You have not said anything new! Stop playing on words.


seunthomas:

I think your confusion is simple, you see array only as "[]" and not class called "Array" which is a derivative of [].

Beautiful, you mentioned that you "THINK"! Well let me help you see properly so that you will get it that you are the one confused not me!
Check here https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
it will clear up your confusion.

The documentation first sentence shows who is confused here .. "..An array is a container object that holds a fixed number of values of a single type"

Read the rest yourself... You may know some other language, but java I doubt it! Maybe not as well as you think you do.

1 Like

Re: What The Different Between ENUM And ARRAY In Java by seunthomas: 12:11am On Oct 04, 2016
timtoday:


The high-lighted words of yours is WRONG!! And I have shown that even in my first post!



Go back and read what I posted as far back as 10.19pm. You have not said anything new! Stop playing on words.




Beautiful, you mentioned that you "THINK"! Well let me help you see properly so that you will get it that you are the one confused not me!
Check here https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
it will clear up your confusion.

The documentation first sentence shows who is confused here .. "..An array is a container object that holds a fixed number of values of a single type"

Read the rest yourself... You may know some other language, but java I doubt it! Maybe not as well as you think you do.

And an array of objects is what exactly?? Object s[]={}

Can this hold multiple types or not?
Re: What The Different Between ENUM And ARRAY In Java by seunthomas: 12:16am On Oct 04, 2016
class Test{



public static void main(String args[]){
Object s[]=new Object[3];
s[0]=true;
s[1]="Hello World";
s[2]=20;

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

}


}


Check that out and lets continue that argument.

Thats an array that holds different primitive types.

1 Like

Re: What The Different Between ENUM And ARRAY In Java by timtoday: 12:32am On Oct 04, 2016
seunthomas:


And an array of objects is what exactly?? Object s[]={}

Can this hold multiple types or not?

Ahhhhaaa.. multiples of what types? Every Object defined in Java extends the "Mother" object called "Object".

Which means that every class is a sub-class of the super class Object.
And as far back as 10.19pm last night. I mentioned to you that
..Storing different sub-classes of a super-class in the ARRAY of that super-class is not the same as storing different objects of DIFFERENT types like you have in Perl, JavaScript and the likes...
So what point are you making?

In fact, in that same post of 10.19pm I said this

If you inherit from a class say Employee, with a subclass say HourlyEmployee. The best you can have stored in the ARRAY instant object of Employee is HourlyEmployee, you can't store into it a class TYPE of class say Student. And that is if you can even instantiate Employee to start with, that is if Employee is not an abstract class or an interface.

In the same vein, you can't store Strings in the ARRAY of TYPE Integers or Doubles... It doesn't work in JAVA that way, like I said in my previous post.

Of what difference is what I said and what you are bringing up now? Some 2 hrs afterwards? Common!!
Re: What The Different Between ENUM And ARRAY In Java by seunthomas: 12:33am On Oct 04, 2016
timtoday:


Ahhhhaaa.. multiples of what types? Every Object defined in Java extends the "Mother" object called "Object".

Which means that every class is a sub-class of the super class Object.
And as far back as 10.19pm last night. I mentioned to you that
So what point are you making?

In fact, in that same post of 10.19pm I said this


Of what difference is what I said and what you are bringing up now? Some 2 hrs afterwards? Common!!



Seriously.....

Dude cant you just be honourable and agree you are wrong??

By the way my array is holding primitives

It is this kind of attitude that makes one rude and talk to people.

Just admit you are wrong and its gone.
Re: What The Different Between ENUM And ARRAY In Java by timtoday: 12:39am On Oct 04, 2016
seunthomas:
class Test{



public static void main(String args[]){
Object s[]=new Object[3];
s[0]=true;
s[1]="Hello World";
s[2]=20;

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

}


}


Check that out and lets continue that argument.

Thats an array that holds different primitive types.

Like I mentioned, you might know some other language NOT Java.
Because if you know Java and how JVM works, you will not that what you have in the are not "primitive" types.
It is has been converted to the Wrapper Class Type of the primitive type I mentioned to you.

In fact, using a String should have prompted you to know that. Because String is not a PRIMITIVE TYPE, and She is immutable. At anytime you call String name = "My Name"; What you are doing REALLY is
String name = new String("My Name"wink;
.

Let me give you a taste of it using your own code do this:


public class Test{



public static void main(String args[]){
Object s[]=new Object[3];
s[0]=true;
s[1]="Hello World";
s[2]=20;

System.out.println(s[0].getClass()); //class java.lang.Boolean
System.out.println(s[1].getClass()); //class java.lang.String
System.out.println(s[2].getClass()); //class java.lang.Integer
System.out.println();

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


You see yourself?
Re: What The Different Between ENUM And ARRAY In Java by seunthomas: 12:42am On Oct 04, 2016
timtoday:


Like I mentioned, you might know some other language NOT Java.
Because if you know Java and how JVM works, you will not that what you have in the are not "primitive" types.
It is has been converted to the Wrapper Class Type of the primitive type I mentioned you.

In fact, using a String should have prompted you to know that. Because String is not a PRIMITIVE TYPE, and She is immutable. At anytime you call String name = "My Name"; What you are doing REALLY is String name = new String("My Name"wink;.

You give you a taste of it using your own code do this:


public class Test{



public static void main(String args[]){
Object s[]=new Object[3];
s[0]=true;
s[1]="Hello World";
s[2]=20;

System.out.println(s[0].getClass()); //class java.lang.Boolean
System.out.println(s[1].getClass()); //class java.lang.String
System.out.println(s[2].getClass()); //class java.lang.Integer
System.out.println();

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


You see yourself?
JEEEZ.

I am tired of arguing with you.

Next time, try to know before arguing.
Re: What The Different Between ENUM And ARRAY In Java by timtoday: 12:42am On Oct 04, 2016
seunthomas:


Seriously.....

Dude cant you just be honourable and agree you are wrong??

By the way my array is holding primitives

It is this kind of attitude that makes one rude and talk to people.

Just admit you are wrong and its gone.

I should admit am wrong? When you don't even know what you are saying? Holding primitives indeed!!!
Bro, you don't know these things! Just go and sleep!

Google can help you. Just ask is everything in Java an Object?!
Re: What The Different Between ENUM And ARRAY In Java by seunthomas: 12:45am On Oct 04, 2016
timtoday:


I should admit am wrong? When you don't even know what you are saying? Holding primitives indeed!!!
Bro, you don't know these things! Just go and sleep!

Google can help you. Just ask is everything in Java an Object?!

You are actually contradicting yourself.


Forget what you see online. Not all answers you find on stackoverflow etc are accurate.

(1) (2) (Reply)

Recommend A Good Software Programming Training Center In India / The Greatest Challenge Of The Programming World / Help On Being A Good Computer Science Student

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