View Full Version : [Java] Enums
Aradon
12-06-2006, 08:47 PM
Hey everyone,
I know, me asking a question in Java! What?! Heh. In any case here it is. I know that certain classes can't use the usual == method for the reasons of pointers. Being a 1.4.2 Java guy and slowly starting to conform with 1.5 I was wondering if enumerated types followed this same exclusion. That is, if we have three enumerated types
FUNCTION
VARIABLE
CONSTANT
How do they end up being compared? Just like primitives or do they fall within the class definition?
So is it
return FUNCTION == FUNCTION;
or
return FUNCTION.equals(FUNCTION);
And if so why? My knowledge of enums is at the basic level in comparison to my knowledge of the other specifics of Java. Anyways should make for a good discussion!
Thanks.
-Aradon
Goat Spirit
12-06-2006, 09:15 PM
Hey everyone,
I know, me asking a question in Java! What?! Heh. In any case here it is. I know that certain classes can't use the usual == method for the reasons of pointers. Being a 1.4.2 Java guy and slowly starting to conform with 1.5 I was wondering if enumerated types followed this same exclusion. That is, if we have three enumerated types
FUNCTION
VARIABLE
CONSTANT
How do they end up being compared? Just like primitives or do they fall within the class definition?
So is it
return FUNCTION == FUNCTION;
or
return FUNCTION.equals(FUNCTION);
And if so why? My knowledge of enums is at the basic level in comparison to my knowledge of the other specifics of Java. Anyways should make for a good discussion!
Thanks.
-Aradon
Hi Aradon!
== Means comparing.
I am not sure wether you can do:
return FUNCTION.equals(FUNCTION);
Or:
return FUNCTION = FUNCTION;
I would think it would be the second one because a single = is giving a value while == is comparing. Some other comparing methods:
>= //Greater than or equal to
== //Is equal to
<= //Less than or equal to
> //Less than
< //Greater than
!= //Does not equal
Aradon
12-06-2006, 09:32 PM
Thank you for your response. However comparing such as the ones you listed do NOT work for strings in Java.
Saying something like
String dur = "dur";
String dur2 = "dur";
return dur == dur2;
Will return false. Why? Because when you compare Strings or Classes for that matter you are comparing to see if they point to the same place, aka if the pointer is basically the same. However it does NOT compare if the strings are the same.
For that you have to use
String dur = "dur";
String dur2 = "dur";
return dur.equals(dur2);
To get true.
So, do enums work that way or through a binary comparator such as == ?
daniel_g
12-06-2006, 10:00 PM
Is this what you mean?
public class Enums{
public static void main(String[] args){
System.out.println(foo());
}//end main
public static boolean foo(){
return fee(1)==faa(1);
}//end foo
public static int faa(int fa){
return fa;
}
public static int fee(int fe){
return fe;
}//end fee
}//end Enums
foo() ends up returning true in this case.
I guess the use of == or .equals would depend on how you declare the function. For instance if the function is
public String str(){}
public String str2(){}
then you would need to use .equal()
If that's not what you meant, just ignore my answer, and read:
http://alumnus.caltech.edu/~leif/OO/Enum.html
I didn't understand anything about any of that, but it migth be helpful to you maybe?
Aradon
12-06-2006, 10:06 PM
Hrm, let me a bit more specific I suppose. When I say java enums what I mean is this:
enums in java (http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html)
Enums are enumerated types.
So I would be comparing:
public enum TermType {CONSTANT, FUNCTION, VARIABLE}
Between those enumerated types.
Does that clear things up a little?
Hi Aradon...hope all is well.
Here is an example I wrote to help you out. It is somewhat self explanatory.
First comes the Enumerated type...in this case Day...and I wrote it in a file called Day.java
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
} //-- ends definition of Day
Second...the class that does the comparison. I called it CompareEnum.java and it will only accept instances of type Day. You can easily modify this if you wish to suit your particular situation.
import java.util.Comparator;
public class CompareEnum implements Comparator <Day>{
/*
returns negative is d1 less than d2
returns zero if they are equal
returns positive value if d1 greater than d2
*/
public int compare( Day d1, Day d2 ) {
return d1.compareTo( d2 );
}
} //-- ends definition of CompareEnum
Finally...a class that performs the testing...called Tester.java
public class Tester {
public static void main( String [] args ) {
CompareEnum comparer = new CompareEnum();
Day d1 = Day.MONDAY;
Day d2 = Day.TUESDAY;
if( comparer.compare( d1, d2 ) < 0 ) {
System.out.println( "Weekday " + d1 + " comes before " + d2 );
} else if ( comparer.compare( d1, d2 ) > 0 ) {
System.out.println( "Weekday " + d2 + " comes before " + d1 );
}
} //-- ends class method main
} //-- ends definition of Tester
the output of running Tester is...
Weekday MONDAY comes before TUESDAY
I am sure that there are other ways to do this...such as implementing Comparable....
Good luck.
Ess
Aradon
12-07-2006, 12:40 AM
Thank you Ess, that does indeed answer my question with a little modification.
So for future searching let me go ahead and explain.
In your Day class you created a sort of "natural ordering" of the enumerated types by order in which you declared them. I now make an assumption that it is like an Integer ordering, although this may or may not be true.
So if you were to edit Day.java
public enum Day {
TUESDAY, MONDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
} //-- ends definition of Day
And then run the Tester, Tuesday Suddenly becomes before Monday! So what does this tell us? Well it tells us that in order to do < > <= >= to comparisons we must make a comparable class. However with a little modification..
public class Tester {
public static void main( String [] args ) {
Day d1 = Day.MONDAY;
Day d2 = Day.MONDAY;
if(d1 == d2)
{
System.out.println("True!");
}
else
System.out.println("Not True!");
} //-- ends class method main
} //-- ends definition of Tester
Outputs
True!
Which means that == comparison can indeed be done!
Thanks again everyone.
daniel_g
12-08-2006, 05:20 AM
Ah, I see what enums are now.
So, would the method equals() also work?
Well, here I answer my own question:
public class Days{
public enum Day {
TUESDAY, MONDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
} //end Day
public static void main(String[] args){
Day day1 = Day.MONDAY;
Day day2 = Day.MONDAY;
if(day1.equals(day2))
System.out.println("true");
else
System.out.println("false");
}//end main
}// end Days
Output: True.
So I guess it does, since d1 and d2 are nothing more than variables, and the assigned value of those variables are the enums.
Yet, if we can directly check if 1 == 1, why can't we do MONDAY == MONDAY?
I guess the reason is that in enum Day{} you can't do anything more than creating the enums?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.