Cool Stuff In Java I Did Not Understand


Recommended Posts

this site talks about using enums (I knew basic enums as simple list for look up , but this..)

http://java.sun.com/j2se/1.5.0/docs/guide/...uage/enums.html

This is an example of flexability

	public enum Operation {
PLUS { double eval(double x, double y) { return x + y; } },
MINUS { double eval(double x, double y) { return x - y; } },
TIMES { double eval(double x, double y) { return x * y; } },
DIVIDE { double eval(double x, double y) { return x / y; } };

// Do arithmetic op represented by this constant
abstract double eval(double x, double y);
}

Here is a sample program that exercises the Operation class. It takes two operands from the command line, iterates over all the operations, and for each operation, performs the operation and prints the resulting equation:

public static void main(String args[]) {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
for (Operation op : Operation.values())
System.out.printf("%f %s %f = %f%n", x, op, y, op.eval(x, y));
}

output

$ java Operation 4 2

4.000000 PLUS 2.000000 = 6.000000

4.000000 MINUS 2.000000 = 2.000000

4.000000 TIMES 2.000000 = 8.000000

4.000000 DIVIDE 2.000000 = 2.000000

I did not know you could put methods inside of enums..

note.. for printf %f = flotingpoint(or double) %s = string and I beleive %n is newline (I thought it was /n)

Link to post
Share on other sites

Thats pretty cool, I also thought enumerating was only for lists. I haven't used enum yet but that looks very interesting, can methods be enumerated like that or just classes?

%n is newline (I thought it was /n)

It's both.

Link to post
Share on other sites

not sure, Java is a little confusing in comparison to c++ on what is a class and what is a method.. I thought these were methods.. but now I look agian it acts like a class.

wow.. the more I learn, the more I understand that I don't know anything..

Edited by iccaros
Link to post
Share on other sites
It's both.

But they're different. \n is a literal Unicode linefeed, %n is a printf conversion specifier that's replaced with a platform-specific line terminator.

Edited by jcl
Link to post
Share on other sites
not sure, Java is a little confusing in comparison to c++ on what is a class and what is a method.. I thought these were methods.. but now I look agian it acts like a class.

The enums, you mean? The enum declaration is syntactic sugar for a class decl. The generated Operation class looks like this, sans method bodies and private members:

public abstract class Operation extends java.lang.Enum {
public static final Operation PLUS;
public static final Operation MINUS;
public static final Operation TIMES;
public static final Operation DIVIDE;
public static final Operation[] values();
public static Operation valueOf(java.lang.String);
abstract double eval(double, double);
Operation(java.lang.String, int, Operation$1);
}

A static block initializes the constants (PLUS, etc) when the class is loaded. When the enum contains instance methods the compiler generates a bunch of anonymous classes that inherit from the enum class that are used for the constants. (When you compile Operation.java the anonymous classes will be created as Operation$[1-4].class.) Otherwise it uses instances of the enum class.

Edited by jcl
Link to post
Share on other sites

I'll do my best here.

emun is to enumerate or to create list of linkable object.

so in the example we have a list of linked methods.

this is insted of using arrays, we now have a list that can be exploited in diffrent ways.

in this example we declared two doubles x and y and pushed them thourgh the enum

with this

for (Operation op : Operation.values())
System.out.printf("%f %s %f = %f%n", x, op, y, op.eval(x, y));

which I believe is the same as saying

for every type in the enum print x , enum type and run method op.eval(x,y)

I guess my explanation does no good if you do not understand objects..

sorry..

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...