What would you like to see in Java?

Like in topic - what would you see in Java?

My propositions:

1. Operator overloading
This would be VERY useful for vector and matrix math, I think this is obvious.

2. New “for” loop
We use code like this very often:

for (int i = 0; i<20; i++) {
    for (int i2 = 0; i2<20; i2++) {
        doOrGetSomething(i, i2);
    }
}

Why not replace it with something like this:

for (int i -> 0 : 20) {
    for (int i2 -> 0 : 20) {
        doOrGetSomething(i, i2);
    }
}

or this:

for (int i, i2 -> 0 : 20) {
    doOrGetSomething(i, i2);
}

or even this (only for arrays and collections) - I know we have “for each” loop, but it cannot be used in some cases:

for (int i, i2 -> array2d) {
    doOrGetSomething(array2d[i][i2]);
}
for (int i -> list) {
    doOrGetSomething(list.get(i));
}

Both of this ideas are just syntatic sugar, but in my opinion would be useful. :slight_smile:

I think those are for Java 8. But I prefer the legacy for loops.

I don’t want that for-loops (its just me), uninitialized variables oO, I like it as it is, and if you want to walk through a List, you can already do this:

for(Object o : List){
    o.doSomething();
}

Now I see al of that -> stuff, I actually miss pointers in Java, I like them a lot in C, in Java you have to make a lot static, but I understand that Java-developers actually want to dodge this, as this has something to do with memory management.

Now try to delete “o” from the same list. :slight_smile:

Also, variables are initialized - this is just syntatic sugar.

@Herjan
For removal, you have to create a new list to store items to remove and remove the elements later.

I don’t think that [icode]key.this()[/icode] is possible since [icode]this[/icode] is a keyword!

About the only significant things I’d like added to the Java language (asopposed to the JVM or library), in rough order of importance, are:

  • User defined value types (ie. structs)
  • Named parameters/keyword arguments
  • A reasonable way to have collection and object litterals espevially for (re)initialization.
  • First class functions (though I’m so used to using interfaces for call backs and handlers that this is secondary)

The removal/complete fixing of applets so the security ‘experts’ shut up about all of Java being dangerous.

If we are talking about language features, then I’d like to see operator overloading, and perhaps macros.

I’d like to see more stuff done in Java2D.
You know, built in gradients, anti-aliasing, etc.

It would be a bit neat if Oracle did some more stuff involving game-dev, but I doubt we will ever see that happen in the near future.

  • Jev.

instead of wishing for stuff, just use Scala already. For a start, it’s a better Java.

type inference
first class functions
complex type system
macros

  • a lot more cool stuff which just fits together perfectly

A few months ago I was a strong supporter of Java(saw no big flaw in it), but now I do want to do every project with Scala.

@Mac70
leaving aside that old C for loops and arrays are not that bad, but should be avoided in most cases, one can something like this in Scala:


for(x <- 0 to 20
     y <- 0 to 20) {
  val foo = bar(x, y)
}

And btw, in this example is no special syntax(the “to”) just a standard Scala for comprehension.

@lukasz1985
leaving aside that PHP is the worst most horrific programming language know to mankind, you already can do this in Java.


for(Map.Entry<Key, Value> pair : map)
{
  Key k = pair.getKey();
  Value v = pair.getValue();
}

//this is of course also shorter in Scala

for( (k,v) <- map ){

}

As far as I know you have gradients and anti-aliasing in Java2d.

I would also see something like LINQ built into default library.

java needs some more functional support. its starting to fall behind D:

Thats right lukas, Scala is really scalable and has no/few rules. And exactly this is what makes Scala so great, there is no need to change the language just because you want another cool feature. Things like LINQ in C# or foreach loops in Java do not need to be implemented by new language features, but can be provided at the library level.

Scala is one of the most simplest languages I ever encountered, it may be strange at first, but after a few days every Java dev should grasp the basic concepts. As you mentioned, you can do really impressive advanced stuff in scala, but there is no need for a beginner to understand these concepts until they want to know.

Know let me go over your points really quick.

  • The “_” can have the meaning of a placeholder, wildcard, undefined type or unnamed value. I think these are very similar and the symbol represents a simple concept.
  • what is confusing about var/val? val are final variables aka values and var are variables.
  • That one should code after some standard should be a given. I can’t count how many times I saw the Java language abused on this forum
  • no need for break/continue if you don’t have loops and switches. Pls give an example which can not be solved better in Scala
  • You said it right no requirement to use return, use it if you wish. Not having to use does make code smaller and simpler i.e. def square(a:Int) = a * a
  • Integration with Java is seamless if you want it to. There a conversions implicit and explicit between Java and Scala library classes.
  • Immutable collections is a big thing which Java misses. Also immutability as a default is in many ways better. Also no one forces you to do it
  • Scala/functional programming can even be faster then Java see tail recursion

Java explicitly went out of its way not to support Operator Overloading because of what it did to code in C++. I personally agree with the decision to not operator overload. If you have something like this:

a<<10;

In C++, that snippet of code tells you nothing. If it’s a input stream it could be writing to the stream (ascii art in code - sounds cool right? Not when you’re trying to understand it!) or it could be bit shifting. People did some really stupid (or overly ‘clever’) things with operator overloading and java decided to throw it out.

I never use operator overloads because it saves 5 characters to make the language less verbose in an area where we’ve all agreed to be verbose (function names.)

Operator overloading would be very useful even if we will have only +, -, * and / operators - this would make code easier to read in a lot of situations like:

  1. Vector/matrix math
  2. StringBuilder
  3. BigInteger/BigDecimal
  4. Calendar

You could define yourself a Range class and a range method and use it like this

for (int i: range(0,20)) {
    doOrGetSomething(i);
}

and with Java 8 you could even do this

nested(range(0,20), range(0,20)).forEach((i,j) ->
    doOrGetSomething(i,j);
);

or

for(Object elem: nested(array2d)) {
    doOrGetSomething(elem);
);

In Java 8 you can write


hashMap.forEach((key, value) ->
    doSomething(key, value);
);

That stuff is already in Java2D. But you could also use JavaFX.
It’s now part of the standard Java distribution.

Scala is a wonderful language. But you can’t expect to understand it on one weekend.
Also some of the features of the language are meant mostly for API developers.
You don’t need to know or understand all of them to use Scala.
In fact there is a Scala “Learning Environment” designed to teach programming to children.
http://www.kogics.net/kojo
Try out the “Kojo Overview Story”.

Making immutability the default was the right decision. It helps to reduce bugs and simplifies parallel programming.
The impact on performance is small in most cases. In the few cases where it actually causes a performance issue, it’s very easy to switch back to mutable collections.

Sorry to disagree but I don’t want operator overloading and new For loop. I want “private” as default modifier for variables.

I agree with the default private access modifier.

I very rarely ever use the default access modifier.

Yeah, I use the default access modifier so little that those few times that I actually need it, I assume that I’m mistaken and use one of the others anyway. Then my IDE shows a bug which I promptly ignore because there aren’t any other access modifiers to use (other than none at all).

I get the impression that one of the reasons they used this as the default access modifier is that they couldn’t think of another name for it.

I agree with you and it would be even better if Oracle stopped shipping malwares / useless toolbars with the JRE (like Runiter said).

I don’t want to see any new language features in Java as it is already possible to use other languages on the JVM instead of trying to put all language concepts into Java. I don’t want Java to become the new C++. I admit that Java may need some new ways of expressing things in parallel but changes of its syntax should be kept as small as possible. I would prefer obtaining improvements and security fixes in existing APIs. The bug I reported on OCSP has not yet been fixed.