bem’s blog

 
Filed under

java

 

Fun with constants in Java

The following code does not compile:

public class A {
public static void main(String[] args) {
int i = Integer.parseInt(args[0]);
switch (i) {
case X: System.out.println("ONE!"); break;
case Y: System.out.println("TWO!"); break;
default: System.out.println("ZERO!"); break;
}
}
public static final int X = B.getNumber("X");
public static final int Y = B.getNumber("Y");
}

class B {
public static int getNumber(String s) {
if (s.equals("X")) {
return 1;
}
else if (s.equals("Y")) {
return 2;
}
else {
return 0;
}
}
}

The compile error is:

A.java:5: constant expression required
case X: System.out.println("ONE!"); break;
^
A.java:6: constant expression required
case Y: System.out.println("TWO!"); break;
^
2 errors

I think I'll leave explaining why as an exercise for the reader. (Or for a future post. ;-)

Filed under  //   geek   java   programming  

Comments [0]

JRuby lead developer expounds on useful JVM flags

I probably start up a JVM a thousand times a day. Test runs, benchmark runs, bug confirmation, API exploration, or running actual apps. And in many of these runs, I use various JVM switches to tweak performance or investigate runtime metrics. Here's a short list of my favorite JVM switches (note these are Hotspot/OpenJDK/SunJDK switches, and may or may not work on yours. Apple JVM is basically the same, so these work).

If you write any serious Java at all, I highly recommend that you click the link above and read his post. It is full of many amazing gems that I can see being really useful for optimizing and debugging Java applications.

Filed under  //   geek   java   programming  

Comments [0]

Java: InvokeDynamic explained

Charles Oliver Nutter explains the whole InvokeDynamic thing in the JVM (And why it's really cool), in a way that those of us that aren't elite JVM hackers will understand. Awesome stuff.

Filed under  //   geek   java   programming  

Comments [0]

Mock Objects

Brett shared this post from the Google Open Source blog. While I'm not into Python, a mock object generator seems totally awesome. (Why didn't anyone tell me about this earlier?) Anyways, the Google Blog post linked to EasyMock, which is a mock object generator for Java. Now that looks really useful, since I spend my day job hacking on a very large Java system with not enough tests. EasyMock has quickly made it onto the short list of things I need to look into.

Filed under  //   geek   java   programming   python  

Comments [0]