Diamond - Java 7 new feature

Posted by Filip | Filed under ,

I spotted an article on InfoQ summarizing new futures of the upcoming JDK 7. Coming from a Java background, I was really curious to see what's in store. The article noted several "small language enhancements", so I binged around (yes, I actually used bing), and found the presentation slides from JavaOne by Joe Darcy.

While many of the enhancements make sense, I was really stunned by the "diamond operator" (page 64 of the slides). The point of it is to reduce typing when declaring generic types. Why specify the type twice, if you can do it in one spot?

List<String> myList = new List<>();

This reminded my of the var keyword in C# and the C++0x's new incarnation of the auto keyword. The difference is that the diamond operator occurs on the right-hand-side, while the type is specified on the left. Doesn't seem like a big deal, but in my opinion this idea is very short sighted.

By having the type on the right-hand-side, both C# and C++ allow much more flexibility. They can handle not only type inference of generics, but pretty much of anything. Specifying type on the right is very natural, and mimics assignment "from right to left". It seems silly of Java to ignore this obvious and proven strategy and instead introduce a new operator. This will be confusing for those programmers who do work with many different languages, and will make Java feel out of place and underpowered. For comparison, this is what Java could have had if it adopted the "auto" keyword:

auto a = 5;
auto b = "some string";
auto c = new RegularClass();
auto d = new List<String>()
auto e = obj.getSomething();
for (auto f : collection) { ... }

The above (or their equivalents) are already available in C# 3.0 and in draft C++0x. Unfortunately (judging by Joe's comments) Java 7 will be released without such type inference mechanisms. Disappointing, very disappointing...

Comments (32) -

July 17, 2010 16:49

cools shop

I just couldn't leave your website before saying that I really enjoyed the quality information you offer. Will be back often to check up on new stuff you post!

cools shop United States

August 7, 2010 07:12

floating diamond necklace

I just wanted to leave a quick comment to say that your blog was nice. I found it on google search after going through a lot of other information that was not really relevant. I thought I would find this much earlier considering how good the information is.

floating diamond necklace United States

September 28, 2010 11:50

Flowers to Japan

nice post great blog thanks to share

Flowers to Japan Japan

October 7, 2010 23:15

95percentmortgages

That makes a lot of sense to me now. C# does confuse a bit.

95percentmortgages United Kingdom

October 20, 2010 16:20

perth web design

I think it's more on the side of C++ interesting software as well, might try to play with it and see how it goes.

perth web design Australia

October 24, 2010 05:22

perth web design

Very similarly to C++ I see. I think I can blend in to this and I hope I can still put on my skills for this new language.

perth web design United States

October 24, 2010 05:23

covers for machines

Very similarly to C++ I see. I think I can blend in to this and I hope I can still put on my skills for this new language. I'm sure I can do it.

covers for machines United States

October 25, 2010 03:11

yery

<a href="http://www.uggssaleforcheap.com">ugg outlet</a>
<a href="http://www.bootsshop.fr">bottes ugg</a>
<a href="http://www.salebalenciaga.com">balenciaga handbags</a>

yery People's Republic of China

November 16, 2010 18:47

Saugata

I understand the auto keyword scenario, it looks very similar to "def" in groovy.
But it is not clear, why the java 7 folks did not adopt, it.
There could be different causes as follows and more.
1) They did not know about it (seems unusual).
2) Knew about it but due to Political reason, did not adopt.
3) Did not adopt, because of Patents.
4) Knew about it, but simply because of ego problem, could not accept it Smile
4) Have good technical reasons (we do not know).
etc.
I think if we know that, it will make the picture more complete.

Saugata United States

November 16, 2010 19:07

Saugata

One more idea came to my mind, about not changing the language syntax to reduce typing. I would stress more on readability and understandability. It is seems logical to me to add features to enhance readability.
Reduction of typing should be done using an intelligent editor. e.g. Completion of statements.
If you type in
"List<String> myList ="
and hit "Ctrl-Space" it should complete the above statement to
"List<String> myList = new List<String>();"
In this case, having auto, would not work.
But to do this, you do not have to change the language, at all.
I am not sure if the language Gurus argued on this point or not.
It would be interesting to know their philosophy.
Saugata

Saugata United States

November 21, 2010 00:05

Haakon Nilsen

The next version of Project Lombok will extend Java to allow the "auto" functionality using the (IMO better) keyword "val": groups.google.com/.../75af87f0e3ec53a7

Haakon Nilsen Norway

November 24, 2010 21:16

David

Just wanted to pipe in and say that (as a language creator) I'd guess they didn't follow suit with C# and several other languages due to backwards compatibility. This is the first I've heard of this feature, but it does stand out in my mind that backwards compat. with this type of feature could cause problems.

Just my $0.02 Smile Thanks for the post.

David United States

November 25, 2010 10:12

pregnancy

Good post man, just looking around some blogs, seems a pretty nice platform you are using.

pregnancy India

November 28, 2010 09:13

Maskotki

Similar to C++ I think...
Nice blog, congrats!

Maskotki Poland

January 3, 2011 09:14

Gaz Pieprzowy

Maskotki. It's not even close to C++

- Gaz

Gaz Pieprzowy United Kingdom

January 11, 2011 10:12

C++ Source Code Directory

Appreciate the research.

C++ Source Code Directory United States

January 12, 2011 02:07

best printers

Exceptionally educative thank you, I think your audience could quite possibly want a whole lot more items like this keep up the great hard work.

best printers United States

January 25, 2011 17:39

Diamond Stud Earrings

Buy high quality diamond stud earrings, 14k yellow gold diamond and gemstone hoop earrings from EarringsForever. Build your own diamond earrings in round, princess and heart shapes quickly.

Diamond Stud Earrings United States

February 1, 2011 10:16

kaufen Viagra

I suppose that some programmers will like this feature, the others will manage to make the necessary changes if needed. Smile

kaufen Viagra United States

February 6, 2011 13:18

vacaciones

I've been stralling the SERP of Google and just came across your blog.Amazing article on that topic!
Love your way of thinking.
Cool, man!

vacaciones Sweden

March 22, 2011 13:14

Lautaro

Good post.

For me is more natural to have the type specified on the left side. After all, what you are going to use on the remaining code is such variable and on such variable the type controls will be applied. Maybe this was the reason behind this design.

Lautaro Argentina

March 30, 2011 07:01

Coach Crossbody Bags

<strong><a href="www.timberlandoutlet.org/">Timberland Boots Sale</a></strong> has become an indispensable female dress with a single product. Because the <strong><a href="www.timberlandoutlet.org/timberland-mens-boots-c-27.html">Timberland Boots</a></strong>, girls do not have to be monotonous style pants and heavy shoes are tied, not as tall slim slender enough to worry about; a wide range of <strong><a href="www.timberlandoutlet.org/">Timberland Outlet</a></strong> has become a fashion for the protagonist. Come and choose a most suitable for your beautiful <strong><a href="www.timberlandoutlet.org/...-26.html">Work Boots</a></strong> it. <br>
<strong><a href="www.christianlouboutinschuhesale.com/">Christian Louboutin Shoes</a></strong> are designed for women who crave for fashion.Whether you want to find trend shoes out for dating or leisure shoes for entertaining activities,<strong><a href="www.christianlouboutinschuhesale.com/christian-louboutin-pumps-c-115_118">Christian Louboutin Pumps</a></strong> can satisfy you.The first thing is to make it clear what the style of your dress is and what you are going to outline which part of your body.For instance, are you intending to show your long legs or sexy foot fingers? After that, your task is to find the shoes for you. Of course, for common people,dress style would not change as frequently as some celebrities do. <br>
This is the kind of feminine consciousness <strong><a href="www.christianlouboutinschuhesale.com/">Discount Christian Louboutin</a></strong>, you can see the stars.The lively shades of the insignia of the term along with the other more classic ensign <strong><a href="www.themanoloblahnik.com/discount-christian-louboutin-c-6">Christian Louboutin</a></strong> that there make it a good enough buy to have on your feet. The <strong><a href="http://www.themanoloblahnik.com/">Manolo Blahnik</a></strong> allocate you to know the rewards of investing in these <strong><a href="http://www.themanoloblahnik.com/">Manolo Blahnik Shoes</a></strong>. They make you feel on top of the world where, and when you clothes them. <br>
There is no double that every solo ladies would desire to turn into observed getting a <strong><a href="www.coachwiki.net/coach-handbags-c-107">Coach Handbags Outlet</a></strong> which may possibly be considered a sequence of <strong><a href="http://www.coachwiki.net/">Coach Handbags</a></strong> and <strong><a href="www.coachwiki.net/coach-large-purses">Coach Purse</a></strong>. perfect after all, Coach have been pretty well-known over the years, and appear to turn into linked with prestige. coach bags are produced with material or leather. offered options are sometimes, nylon or in leather-based and ostrich epidermis snake. <strong><a href="http://www.coachwiki.net/">Coach Bags</a></strong> could possibly be invested in in different shapes, options and colors. Therefore, you can choose out one color, material, shape, sizing that meet you.

Coach Crossbody Bags People's Republic of China

March 31, 2011 09:14

axnon

great thread

axnon Bolivia

March 31, 2011 09:15

MAX nokx

Amazing

MAX nokx Argentina

April 19, 2011 03:07

what to invest in

You have some really good information here on the features of the new software. Thanks for sharing it.

what to invest in United States

April 29, 2011 13:28

Anonymous

I disagree with your view. In my opinion the type declaration should not be dynamic. The type declaration is a guarantee. It is the only part I need to know, if I want to use the object. This allows me to change a LinkedList into an ArrayList without changing anything else. Your suggestion overthrows this concept, because I have to check, what is actually in the List<>. As a firm believer in static type checking, I come to the conclusion, that the diamond operator is right on the right side.

Anonymous Germany

April 30, 2011 00:53

unicode

In my experience, the "auto" keyword (I used var in C#) is not for simply save a few characters to type. After all the time saved is really small in general. It is much more useful when you do not immediately see what the type of the value you are assigning to it. In the case of C#, var most often contains type of IEnumerable<A>, IOrderedEnumerable<A> and other classes of the same family, from the return value of a Linq statement. In these cases, it is much faster for the compiler to infer the type than the user.
That said, I do not even feel this diamond operator is of much use, since the IDE already takes care of auto-completing the redundancy. It does offer some convenience when the type needs to be changed, as there is only 1 place to modify instead of 2 (or more, if the variable is assigned more than once).

unicode Canada

May 6, 2011 06:02

long island gutters

  Interesting post and thanks for sharing. Thank you, how cool post, which is really very written.will I refer many friends here. Keep blogging.

long island gutters United States

May 9, 2011 03:03

What to Invest In

Have to admit that I'm learning a lot reading so many of these more technical blogs. Appreciate the time and effort.

What to Invest In United States

May 9, 2011 08:52

israel tours

Thanks for this wonderful post.Admiring the time and effort you put into your blog and detailed information…..

israel tours United States

May 19, 2011 15:04

webdesigner

wonderful read

webdesigner United States

May 25, 2011 07:32

abraham

nice blog man . its too good , i really enjoyed this one
http://www.z4site.com

abraham United States

Comments are closed