Java Plus Plus

From EggeWiki

Java++ is my yet to be implemented language which add features from C++ to Java. The language could be implemented either as a pre-compiler or it could create it's own byte-code. In either case, it would run in existing JVMs and can integrate into an existing Java code base.

Java was purposefully kept simple, and that makes the syntax easy to learn. However, after a while, it becomes so tedious typing long Java programs because you are limited by the language. This is one of the reasons why many Java programmers have switched to Ruby. Java++ is intended to be the language of choice for experienced Java developers who must code in Java.

Here are my language features. If I get time, I might even implement the language.

Operator Overloading

It has always struck me as arrogant that Sun allowed java.lang.String to have overloaded operators, but no other classes. This is the benevolent dictator approach, which says if you have the power to do something, you will surely abuse it, and so they take it upon themselves to protect their people.

Consider the Money class:

public class Money {
  Money add(Money rhs) { /*...*/ }
  Money multiply(Money rhs) { /*...*/ }
}
  List<Money> accountBalances = getAccountBalances();
  Money interest = getInterest();
  Money sum = Money.ZERO;
  for(Money money : accountBalances) {
    sum = sum.add(money.multiply(interest));
  }

This is how you would do this same thing in Java++:

public class Money {
  Money operator+Money rhs) { /*...*/ }
  Money operator*(Money rhs) { /*...*/ }
}
  List<Money> accountBalances = getAccountBalances();
  Money interest = getInterest();
  Money sum = Money.ZERO;
  for(Money money : accountBalances) {
    sum = sum + (money * interest);
  }

One level of implicit casting

If an class has a single argument constructor, which is not marked explicit, then Java++ can use that constructor to perform a type conversion.

Java way:

  Money money = new Money(5.0);
  money = money.add(new Money(3.0));

Java++:

  Money money = 5.0; // Money has a 'public Money(Double d)' ctor
  money = money + 3.0; // assume operator overloading and 3.0 gets wrapped with a 'new Money()'

Compiler generated equals and hashCode methods

Just like how the existing compiler gives you a default constructor if you haven't implemented one, you will get a free equals and hashCode method. If you don't like the compiler generated functions, then you can of course write your own. This will save both time and errors. It's fairly trivial for the compiler to look at the fields in a given class and come up with an equals and hashCode for them. Programmers often use IDE tools to create these functions, or attempt to use annotations. Both have their draw backs, and the vast majority of the time the compiler generated functions would server you just fine.