Archive for the 'Java' Category

The Evils of Equals and Autoboxing

December 18th, 2011 | Category: Java, Programming

This was a fun, if particularly stupid, one:

public void method check(int value){
  if(this.x == value)
  {
    //do whatever
  }
}

the previous works as long as x and value are small, but not when they are big. Why? Because x is an Integer not an int. Java helpfully autoboxes the int by building an Integer and then does a pointer comparison. This still works for small numbers because java caches the lower Integer constants rather than creating them on the fly all the time so new Integer(value) actually resolves to the same object as x, but once the number gets big enough Java has to create a new object and then suddenly the comparison fails.

I actually don’t blame autoboxing for this one. Autoboxing was a good idea. Having == mean pointer equality (or really fundamental type equality) rather than actual equality combined with not modeling all fundamental types as objects was not such a hot move.

7 comments

Composition vs. Inheritance (hint: usually not inheritance)

December 17th, 2011 | Category: Java, Programming

A co-worker of mine recently made the comment that a lot of times you see Java code using inheritance where composition is a much better solution. This is very true and worth pointing out.
Read more

3 comments

More fun with Java (scope and duplicates)

July 27th, 2010 | Category: C++, Java, Programming

I thought this was interesting… In Java, a block scoped variable may conflict with a local variable declared later in method scope, but not previously (as shown below):

class A{
  public static void main(String args[]){
    {int k = 5;}
    int k = 12; //this is OK
    {int k = 22;}//this is not....
  }
}

This is not the same as C++ where this compiles happily:

int main(){
  {int i;}
  int i;
  {int i;}
  return 0;
}

Usually C++ has the more obscure behavior….

Comments are off for this post

Protected within Java Enums

July 22nd, 2010 | Category: Java, Programming

I just noticed that java enums allow protected member variables and methods. Why is this interesting? Can you think of any code that might actually make use of a protected member in an enum (as opposed to private)? Example after the jump (warning, not practical…).
Read more

Comments are off for this post

SWIG, Java, and JRuby

July 22nd, 2010 | Category: C++, Java, Networking, Programming, Ruby

Providing a robust, maintainable, and interactive interface to your C/C++ application can be a challenge, but I’ve found that a combination of SWIG, Java, and JRuby (or Jython if you prefer) makes for a very powerful combination.
Read more

Comments are off for this post

DVSL: An Alternative to XSLT

June 26th, 2009 | Category: Java, XML, XPath, XSLT

DVSL is a fairly small, little known, product associated with the Velocity project over at Apache which takes the best part of XSLT: XPATH, and replaces the verbose and frustrating scripting of XSLT with a java-based template language instead.
Read more

Comments are off for this post

Embedding Rhino Part I: Parsing Command Line Arguments in JavaScript

March 11th, 2008 | Category: Java, JavaScript, Programming

Rhino is a JavaScript/ECMAscript implementation in Java. Using the new java1.6 scripting API you can now easily embed JavaScript into your Java applications. I thought I’d try writing yet another implementation of the net_tool application using Rhino. Originally this was going to be a single article, but it turns out to be a two-fer.
Read more

Comments are off for this post

Converting hex to Binary in Java Too

March 05th, 2008 | Category: Java, Programming

So apparently the hex to binary in 4 languages portion of this web page is what gets the most google hits and who am I to argue? So without further ado, hex to binary in Java as well…

Read more

Comments are off for this post

Java’s @Override Annotation

February 28th, 2008 | Category: Java, Programming

Most people are aware of java annotations by now, but I’m not sure everyone knows about @Override. They really should.

Read more

1 comment

Using Javacc

February 19th, 2008 | Category: Java, Programming

If you want to parse a custom language in java then Javacc is your tool. At the moment this is probably more a reminder to myself than a great article for anyone else to read, but as you find it useful feel free to take a look. More after the link…
Read more

Comments are off for this post

Next Page »