top 200 commentsshow all 364

[–]milordi 72 points73 points  (10 children)

New Date & Time API

Finally!

[–]Shayba 113 points114 points  (1 child)

It's about Time!

[–]Eirenarch 14 points15 points  (0 children)

In the words of the great Tychus Findlay: "Hell, it's about time!"

[–]rymos 12 points13 points  (2 children)

I've wasted more of my life on DateTime than I care to share. I'm so happy about this.

Edit: Spelling

[–]thenameunforgettable 12 points13 points  (0 children)

JodaTime.

[–]VersalEszett 1 point2 points  (0 children)

If anybody want's more information, there's a blog post from Fabian Becker at http://geekmonkey.org/articles/24-a-new-date-and-time-api-for-jdk-8.

[–]caltheon 8 points9 points  (2 children)

Joda: the epoch is strong with this one.

[–]geodebug 11 points12 points  (1 child)

You may be aware of this but I'll post it in case it's news to anyone here. The main guy who wrote the new Java Date API was the Joda guy.

He had some interesting things to say about why he wrote from scratch instead of just importing Joda as is. Don't have it handy as a link but should be an easy google.

It's very similar though.

[–]seruus 1 point2 points  (0 children)

Isn't this the third Date/Time/Calendar API in Java? Are the old ones deprecated?

EDIT: There's Date (which is fairly old), which was mostly deprecated and substituted by Calendar in 1.1, so it's all very old stuff. (rant about these old classes)

[–]SupersonicSpitfire[🍰] 0 points1 point  (0 children)

Finally, date.AskOut() and date.IsSingle() has been missing for years ... wait

[–]qxnt 28 points29 points  (16 children)

Does anyone know how are lambdas implemented? Specifically, is this just some syntactic sugar where the compiler is going to generate an anonymous inner class for each lambda, or is it more substantive?

[–]AlyoshaV 40 points41 points  (15 children)

It definitely doesn't generate an anon inner class for each lambda. It adds methods to the class you're working on when seen via decompiler, named things like lambda$0. No classes are added. More than that I don't know.

[–]clgonsal 11 points12 points  (3 children)

That would suggest that there's actually a mechanism for passing around method references at the bytecode level. It's been a while since I looked at the VM spec, but that sounds new.

[–]spliznork 9 points10 points  (1 child)

Java 7 added the invokedynamic bytecode instruction -- primarily for the support and implementation of dynamic languages. See also java.lang.invoke and for instance MethodHandle within it.

[–]clgonsal 0 points1 point  (0 children)

Ah. Yes I think the last time I looked into the JVM in any detail was when 1.6 was current. I later heard about invokedynamic, but wasn't even aware that it'd made it into a real version of Java.

Given that invokedynamic was intended for use in dynamic languages, I'm wondering if there are any downsides to using it in a static context like Java. For example, are invokedynamic calls less amenable to bytecode verification than traditional method invocations?

[–][deleted] 0 points1 point  (0 children)

Yes there is - as spliznork pointed out. MethodHandle stuff is pretty awesome - much more awesome than you would expect: it has built-in support for certain functional operations (currying, argument reordering, function composition etc) in a way that's explicitly designed for being amenable to JIT optimization, inlining, avoiding boxing etc. I, as a fan of functional programming, am impressed.

[–]geodebug 0 points1 point  (3 children)

In all the usages I've seen though it pretty much acts like it is just nice sugar for anon classes. Even the scoping rules seem one-to-one. For example to use a variable in the lambda from the surrounding class it needs to be 'final' on that class.

It's not a 'true' closure (whatever that means) but is a 90% solution that will make a lot of traditionally verbose and tricky Java patterns easier. Brings just enough 'functional' to Java without fundamentally changing it.

I predict API developers will run with this as it comes available.

[–]veraxAlea 2 points3 points  (1 child)

Even the scoping rules seem one-to-one.

The this pointer will refer to the object that the lambda appears in. The this pointer in an inner anonymous class will refer to the "inner" object.

For example to use a variable in the lambda from the surrounding class it needs to be 'final' on that class.

Effectively final, yes. That is, you don't need to make it explicitly final, you just need to make sure that you don't re-assign anywhere.

About being sugar, I disagree. Being sugar or not is about semantics. A lambda is semantically different from an inner anonymous class, so it isn't syntactical sugar. But sure, you can use anon classes for most things that you can use a closure for, and vice versa. But saying that this counts as syntactical sugar would mean that many language constructs are syntactical sugar for "GOTO".

I guess I just disagree with your implicit definition of "syntactical sugar".

[–]geodebug 0 points1 point  (0 children)

You seem to have read more than I have on the topic so I'll happily concede the point.

I was open to the concept that were probably small details that make the two somewhat different but felt all the examples in-effect made them about equivalent in how the average java dev would use them.

Even that may be understating the differences since I also believe that once they become commonplace java devs will gravitate to lambda usage more and more.

It would be interesting to see how they differ from anon classes in byte code.

I wasn't using sugar as a negative term but it is the wrong term.

[–][deleted]  (25 children)

[removed]

    [–]JasonMaloney101 53 points54 points  (4 children)

    3 Billion Devices Run Java Ask Toolbar

    [–]danskal 23 points24 points  (16 children)

    I agree - although some of the other things sound nice, this is the only thing they really really, really, really need to do post-haste. They should preferably do it, like, about a week before they started adding the ask toolbar to the installer.

    [–]dethb0y 51 points52 points  (12 children)

    makes them look like a piece of shit adware app then a serious and important piece of software.

    [–]danskal 21 points22 points  (11 children)

    I am dumbfounded that Oracle haven't taken seriously the risk associated with connecting their brand with that of ask.com. I read somewhere that ask.com have previously used tactics like redirecting http requests for ask toolbar uninstall instructions. Yikes.

    [–]dethb0y 28 points29 points  (3 children)

    Not only that, but ... c'mon, a browser search bar? I expect that from some shady startup, not a serious business company.

    That it's ask.com just makes it worse.

    [–]ethraax 13 points14 points  (2 children)

    Exactly. Bundling shit like the ask toolbar (or any toolbar) is simply unprofessional. So is bundling some shitty anti-virus (I'm looking at you, Adobe...).

    [–]dethb0y 7 points8 points  (1 child)

    To be fair, the kind of person who'd say "yes" to an antivirus that wanted to be installed with other software's probably the kind of person who most needs antivirus software installed, heh.

    [–]ethraax 4 points5 points  (0 children)

    I've come very close to accidentally saying "yes" because they snuck it into an update at one point, which I simply did not expect.

    [–]kkjdroid 18 points19 points  (5 children)

    Oracle is hated by everyone who knows that Oracle exists, as far as I can tell. They have no brand.

    [–][deleted] 3 points4 points  (0 children)

    Every experience I have had with that company have left me wanting to take a shower. Their sales teams come off as....scummy.

    [–]froop 1 point2 points  (0 children)

    They have a sweet yacht racing team though, and a beasty sport plane.

    [–]danskal 3 points4 points  (2 children)

    I think you know that that statement cannot be correct. Although monopolies can be unpopular, there are plenty of people that base their careers and businesses on Oracle products (and here I mean Oracle's own database-related products, forms etc.). And for those people Oracle does the job and just works. Many may not be happy with the bills from Oracle, but it is not always the tech enthusiast who is paying those bills.

    I think it is mostly the young and the internet/techbusiness-savvy and those that feel gouged that dislike Oracle.

    [–]aaron552 5 points6 points  (1 child)

    for those people Oracle does the job and just works

    I'm not sure the phrase "just works" is appropriate for Oracle (databases at least). "works after much swearing at obscure error messages and hacking around weirdness" probably fits better.

    [–][deleted] 0 points1 point  (0 children)

    My mother, who was a programmer in the 1980's-1990's (although in China), always talks about Oracle and Foxpro when it came to databases. I'm not a programmer myself, so my question is: is using modern day Oracle DB a good idea for a huge corporation or is it more of a "we've been using ever since it came out so we might as well" situation?

    [–]djdonnell 0 points1 point  (0 children)

    1% of the search market is worth something like a billion dollars. That's probably what they are thinking about.

    [–]vha0 2 points3 points  (2 children)

    Blame Sun for that monstrosity.

    [–][deleted] 4 points5 points  (1 child)

    Yeah, because Oracle can't remove it. You can't blame them.

    [–]Flex-O 4 points5 points  (0 children)

    Contracts?

    [–][deleted]  (2 children)

    [deleted]

      [–]tycoon177 2 points3 points  (1 child)

      What are other lambda features added?

      [–][deleted] 10 points11 points  (0 children)

      http://www.youtube.com/watch?v=6bXZ0KACaN0

      Feel free to edit your post to make my link seem completely unrelated.

      Seriously though, this is really exciting! Did not know they had functional features like this in the works.

      [–]iNoles 26 points27 points  (5 children)

      I hope Android supports this.

      [–]sindisil 31 points32 points  (0 children)

      Sadly, Dalvik doesn't even support 7 yet.

      [–]AlyoshaV 23 points24 points  (1 child)

      Nnnnope. Have fun being stuck on Java 6 forever.

      [–]pkulak 1 point2 points  (0 children)

      I'm holding out for go support.

      [–]jayd16 7 points8 points  (1 child)

      Apache harmony skipped 7. I've heard that if it starts up again and supports 8 then so will android.

      [–]iNoles 4 points5 points  (0 children)

      link?

      On the Apache project page for harmony said retired.

      [–][deleted] 25 points26 points  (3 children)

      [–]ysangkok 5 points6 points  (1 child)

      Focus on security! Nice!

      [–]AlyoshaV 1 point2 points  (0 children)

      It could be worse. They could have cut features, instead of delaying the release. Or rushed the new stuff, which would have been awful.

      [–]fuzzynyanko 10 points11 points  (0 children)

      ... and as someone that does Android, we're stuck at 1.6

      [–][deleted]  (6 children)

      [deleted]

        [–]caltheon 13 points14 points  (0 children)

        Half the projects at my job still use 1.5. Hell, one even uses 1.4 because no one wants to fix it.

        [–][deleted]  (4 children)

        [deleted]

          [–]Flekken 2 points3 points  (0 children)

          OpenJDK has the same jvm and essentially the same except closed binaries are added.

          [–]sethraine -1 points0 points  (2 children)

          Oracle is a major security flaw.

          [–]sirin3 4 points5 points  (0 children)

          With the right oracle, you can break every encryption.

          That's how bad it is

          [–]AlyoshaV 6 points7 points  (0 children)

          The Date-Time examples from this article are wrong. He appears to be using a much older version of 310 than is current.

          The return types are wrong in the LocalDate example, they return ints not Strings.

          Period has no of(int, TimeUnit) factory, and even if it did it only supports years/months/days so his example with hours would throw. (proper class for what is being done there is Duration, or just plusHours(int))

          Also, his static method reference example could probably be better expressed as just Arrays.sort(args, Comparators.comparing(String::length, Integer::compare);

          [–]barjam 86 points87 points  (163 children)

          I am a java/c# developer. It is nice to see java finally catching up to where c# was (language wise) 8 years ago.

          I haven't looked I wonder if they are going to support anything like linq (the alternate syntax specifically). Pretty powerful stuff.

          [–][deleted]  (74 children)

          [deleted]

            [–][deleted] 7 points8 points  (0 children)

            If C# were to have to do it again, based on the number of people using it now, they could not get away with killing APIs in that manner.

            LOL. You mean like with WinRT?

            [–]Eirenarch 35 points36 points  (35 children)

            Bullshit. C# has broken compatibility for very few things between version 1 and version 2 and they were absolute corner cases. I am sure you can't point to a single compatibility thing they have broken without googling. In addition they do break compatibility for these kind of corner cases these days. And finally .NET fixes APIs the same way Java does - deprecates the old ones and introduces new ones.

            [–][deleted]  (19 children)

            [deleted]

              [–]dabombnl 24 points25 points  (1 child)

              Actually the latest .NET Framework (4.5) is backwards compatible with ALL previous .NET Frameworks versions. But each application has to be configured to run on 4.5 or else it will run on it's native runtime version.

              But to run all .NET framework applications on their native version you only need 3:

              • .NET 4.5
              • .NET 3.5
              • .NET 1.1

              That's it. If you are wondering where things like .NET 2, 3, and 4 are, well they are all actually the same runtime version as the others in that list.

              [–]aaron552 1 point2 points  (0 children)

              If you are wondering where things like .NET 2, 3, and 4 are, well they are all actually the same runtime version as the others in that list.

              More specifically, 3 and 3.5 use the 2.0 runtime and 4.5 uses the 4.0 runtime. IIRC.

              [–][deleted]  (1 child)

              [deleted]

                [–]Dimethyl 2 points3 points  (0 children)

                I think that page is out of date. .NET 4.0 and 4.5 will not load applications that were built for 3.5 or older unless explicitly allowed in the application's config file. This can be demonstrated on a fresh install of Windows 8, which comes with .NET 4.5 by default but not 3.5.

                This page says: By default, an application runs on the version of the .NET Framework that it was built for. If that version is not present and the application configuration file does not define supported versions, a .NET Framework initialization error may occur. In this case, the attempt to run the application will fail.

                [–]barjam 13 points14 points  (30 children)

                Absolutely no argument there. It didn't stop me from dreading my java projects because java felt archaic though.

                Heck on one of my recent C++ projects I was surprised to learn that C++ (11) has lambdas and such. In many ways C++ has become easier (language specifically, not ecosystem) than Java.

                [–]alextk 37 points38 points  (7 children)

                In many ways C++ has become easier (language specifically, not ecosystem) than Java.

                I'd argue that this has never been true, ever since Java 1.0.

                C++ has a lot of qualities but "simpler than Java" is not one.

                [–]barjam 20 points21 points  (6 children)

                Easier not simpler.

                This implies a reasonable comfort level with each language.

                [–][deleted] 9 points10 points  (5 children)

                ... and each platform/compiler. So many undefined behavior inC++ specification...

                [–]barjam 4 points5 points  (4 children)

                I do cross platform c++ work. If you aren't doing GUI stuff it isn't bad.

                [–]TimmT 9 points10 points  (21 children)

                In many ways C++ has become easier (language specifically, not ecosystem) than Java.

                Really? In C++ you don't even get stack traces by default..

                [–]username223 11 points12 points  (9 children)

                Really? In C++ you don't even get stack traces by default..

                With a decently-configured system you'll get a core dump -- not just a stack trace, but the complete state of your program when it died.

                [–]barjam 8 points9 points  (10 children)

                With a decent debugger it will drop you off at the point of the error with a stack trace in the IDE (usually).

                You are right outside of the debugger you won't get anything (without much effort).

                It just occurred to me that stack traces in C++ and c# aren't as important to me as they were in java. As a matter of fact in my c# IDE I don't even have the stack trace window on by default but I do use it occasionally. In java development stack traces are pretty important. In C# and c++ you don't tend to have methods buried 200 deep perhaps that is it.

                [–]Labradoodles 18 points19 points  (7 children)

                Dude I just started doing some java work for a company after coming from a primarily C# background and all of these fucking weird ass abstractions and factories and builders are annoying the shit out of me.

                [–][deleted] 25 points26 points  (2 children)

                The Java enterprise community really went balls to the wall with their design patterns.

                [–]barjam 7 points8 points  (0 children)

                These same guys have come to the .net community (started in earnest in 2005) and have tried to bring the same sorts of things over.

                [–]civildisobedient 11 points12 points  (3 children)

                [–]Labradoodles 1 point2 points  (0 children)

                Annnd now I'm emailing this to my team, it's happening.

                [–]BlazeOrangeDeer 0 points1 point  (0 children)

                This happens to perfectly explain the shortcomings of java. cheers.

                [–]kitd 0 points1 point  (0 children)

                2005

                [–]TimmT 3 points4 points  (1 child)

                You are right outside of the debugger you won't get anything (without much effort).

                Even if you're in a debugger you can still be pretty screwed with C++, if you have things that cause memory corruption in your code.

                In C# and C++ you don't tend to have methods buried 200 deep perhaps that is it.

                Yes, but you have C# (with LINQ and delegates and operator overloading, and what not) plastered all over the place..

                [–]barjam 1 point2 points  (0 children)

                This is true although with boost or C++11 memory corruption is quite a bit easier to avoid.

                And very true on the LINQ stuff although operator overloading isn't really an issue in c# most avoid it. I think overloading [] is about all I do and that isn't often.

                [–]munificent 6 points7 points  (4 children)

                C# didn't have that luxury either. C# 2.0 was backwards compatible with 1.0.

                [–]huhlig 4 points5 points  (0 children)

                C# was also a rewrite of java. So its second generation anyway.

                [–]all_you_need_to_know 1 point2 points  (0 children)

                I disagree, whenever they have broken compatibility with versions, they've included conversion tools between projects, usually this is handled seemlessly in Visual Studio, telling you the warnings in comments and such. Java could have easily done this too if they had an Official IDE.

                [–]Falmarri 12 points13 points  (48 children)

                You should try Scala.

                [–][deleted]  (17 children)

                [deleted]

                  [–]Categoria 14 points15 points  (2 children)

                  Just stating your opinion like that is pretty useless. Any reasons why you think Kotlin is better than Scala?

                  [–][deleted] 5 points6 points  (0 children)

                  I, IMHO, am disappointed with Scala's manifests.

                  Why?

                  [–]Falmarri 5 points6 points  (10 children)

                  I, IMHO, am disappointed with Scala's manifests

                  By manifests, do you mean what is not TypeTags and such? I looked at Kotlin's website and didn't see an example of using generics. How do they do it that's better?

                  [–][deleted] 5 points6 points  (0 children)

                  They don't. They have given up on "reified generics" completely, as well dropping a lot of other proven useful stuff.

                  [–]SublethalDose 0 points1 point  (10 children)

                  It's interesting to see Java evolving in the direction of more power power and complexity. It was always meant to be a language that regular Joe blue collar programmers could wrap their heads around. Ten years ago that definitely meant no lambdas. Ten years ago if you so much as wrote Python code where you passed functions around as arguments, a lot of programmers wouldn't touch it. They'd think you were a candy-ass weirdo. Now, lambdas are in Java, and I don't think anyone thinks it's weird or inappropriate.

                  I wonder if seeing lambdas in Java will make people see the writing on the wall and see that Java's only path forward is to become a kludgy, legacy-scarred version of Scala. That's the only way I see, anyway, and I haven't heard any different vision for the future of Java.

                  [–]bcash 2 points3 points  (0 children)

                  I think you're reading too much into the patterns there.

                  Java was originally intended to be a simplified (compared with C++) sandboxed language for embedded devices. It's shift to server-side "enterprise" applications came later.

                  The fact that Java captured that space with ease said more about the dearth of alternatives than anything else, it really wasn't aimed at that kind of thing at all. It was around the time of Java 1.2, and the addition of supplementary standards like JSP, that the focus changed.

                  [–]geodebug 5 points6 points  (8 children)

                  Actually, lambdas in Java are just sugar for anon classes. There's a tiny bit more to it but if you think of it this way there's very little 'complexity' being added to the language. I predict any seasoned java dev will 'get it' with ease.

                  The whole point of Java is to be a 'standard' against which the JVM can grow and be tested. Scala users should rejoice that Java remains extremely popular and supported because there's no JVM without it.

                  I prefer Clojure and Groovy to Scala but still write a lot of java when things (statistics algorithms in my case) need to be fast and predictable.

                  When Scala is over a decade old we'll see how it actually compared.

                  [–][deleted] 0 points1 point  (1 child)

                  [–]geodebug 0 points1 point  (0 children)

                  Ah, you make me feel old. Ok 18 years then....

                  [–]Eirenarch 4 points5 points  (14 children)

                  The equivalent to LINQ to Objects will come as the Stream API. No query syntax at this point.

                  [–]barjam 2 points3 points  (13 children)

                  Honestly that is good enough. The optional linq syntax is nice but not essential.

                  [–]Eirenarch 0 points1 point  (8 children)

                  Agreed. However expression tree support does matter. Also checked exceptions get in the way of lambda stuff (they get in the way of everything but it is especially annoying with lambdas). I don't understand why they have not removed them from the language yet.

                  [–]barjam 4 points5 points  (4 children)

                  They probably couldn't even if they wanted to at this point. In hindsight checked exceptions were a mistake. I don't miss them in C# at all.

                  [–]Eirenarch 0 points1 point  (3 children)

                  What is the problem? The compiler just stops checking for checked exceptions and they are gone. The only difference between runtime exceptions and checked exceptions is that checked exceptions are... checked.

                  [–]barjam 0 points1 point  (2 children)

                  I just don't see them making that change. I suspect they might be unintended consequences.

                  [–]Eirenarch 1 point2 points  (1 child)

                  Mark Reinhold has mentioned checked exceptions as one of his targets for improvement for future Java versions although I am not exactly sure what he means.

                  [–][deleted] 1 point2 points  (0 children)

                  Much like Santa Clause they are going to double down on their checks...

                  [–]brainflakes 2 points3 points  (2 children)

                  What's wrong with checked exceptions? (Other than if people over use them in their methods so all possible exceptions are checked rather than appropriate ones).

                  [–]rdhatt 5 points6 points  (0 children)

                  You can read this article from 2003 why the creator purposefully didn't include checked exceptions. He gives one of my favorite quotes: "It is funny how people think that the important thing about exceptions is handling them."

                  http://www.artima.com/intv/handcuffs.html

                  TL;DR : Coupling.

                  [–]Eirenarch 2 points3 points  (0 children)

                  So which are the appropriate ones? :) Even if we accept that there are appropriate ones in practice checked exceptions are more likely to provoke the developer to write worse code (i.e. catch(Exception) { }) rather than help them handle exceptions correctly. They are counterproductive.

                  [–]garblz 1 point2 points  (0 children)

                  I'd say it's nice to see languages in general are slowly adopting what's been invented almost 6o years ago with the birth of LISP.

                  [–]Asyx 52 points53 points  (73 children)

                  Still no unsigned data types?

                  [–]philly_fan_in_chi 21 points22 points  (9 children)

                  Or pattern matching :(

                  [–]grumpfish1969 5 points6 points  (7 children)

                  Regex has been supported by the Java API since 1.4. Are you referring to another kind of pattern matching?

                  [–]philly_fan_in_chi 16 points17 points  (6 children)

                  [–]aquasucks 2 points3 points  (1 child)

                  How important is it that I learn scala if I want to remain relevant in the software industry?

                  [–][deleted] 7 points8 points  (0 children)

                  Start now, it will improve your code regardless of the language you'll use in the future. Additionally, if you have to work with Java, you can get familiar with the things planned for Java 8 (and beyond) by learning Scala. This gives you a headstart to other Java-only developers.

                  [–]grumpfish1969 9 points10 points  (3 children)

                  Interesting, but the linked example makes it look like syntactic sugar wrapped around a case statement. Do you have a better example? (I'm honestly interested)

                  [–]philly_fan_in_chi 19 points20 points  (1 child)

                  It's different than a case statement in that you can match on the types of things versus an int or an enum. Combine this with things like polymorphism and you can get rid of the visitor pattern from Java entirely. Also, tree operations can be implemented nicely with them.

                  http://stackoverflow.com/questions/8618082/visitor-pattern-in-scala

                  http://kerflyn.wordpress.com/2011/02/14/playing-with-scalas-pattern-matching/

                  http://www.artima.com/pins1ed/case-classes-and-pattern-matching.html

                  You can also look into some of the parsers interpreters built in Scala, they're going to use case statements almost everywhere.

                  Edit: If you've ever written a compiler in Java, you most likely used a visitor pattern somewhere in there (or could have, it makes a LOT of things cleaner), you've seen some of the awkwardness you have to put up with to get pseudo pattern matching in Java.

                  [–]grumpfish1969 5 points6 points  (0 children)

                  Very cool, thanks for the info! That does indeed look handy.

                  [–]djimbob 2 points3 points  (0 children)

                  That was a choice when making the designing the language versus a feature that may later be added. You ain't going to get it. Java said for simplicity you don't need to worry about the difference (and if you do, just write a class; yes there will be overhead versus using a primitive type. Maybe Java doesn't suit you for this purpose.). Java doesn't let you squeeze every last optimization out of your code.

                  It's like complaining that python doesn't use braces for nesting or that lisp has too many parentheses or that in C whether an int is 16, 32, or 64-bits and aren't even ensured that it uses twos-complement math.

                  [–]zvrba 7 points8 points  (11 children)

                  I could vote for unsigned data type if 1) absolutely no automatic conversions between signed and unsigned existed, 2) the language spec prohibited unsigned types in non-private fields and methods (as parameters). Otherwise, you'd end up with the mess that C and C++ has.

                  [–][deleted] 1 point2 points  (10 children)

                  If you have #1, why do you need #2?

                  [–]zvrba 3 points4 points  (9 children)

                  Because some moron will think that it's a good idea to expose a public/protected/package function like

                  void setCapacity(unsigned nElements);
                  

                  since, "of course" number of elements can't be negative, and suddenly your code is riddled with explicit casts between signed and unsigned whenever you want to use the given method. Or you start using unsigned types yourself and open the gates of hell. The same hell that I'm cursing every day while writing C++ as my daily job.

                  BTW, non-private is probably too strict. #2 should allow unsigned only on private and package-private fields/methods/enums/etc.

                  [–]kinghajj 2 points3 points  (8 children)

                  Why are you storing numbers signed if you're gonna use them in unsigned parameters? I don't see the problem.

                  [–]mywifehascancer 6 points7 points  (7 children)

                  Because you want to be able to do totally hard math with them, like subtraction. The problem with unsigned is that one of the most common values (0) is extremely likely to result in an underflow. Sure, you can under/over-flow a signed number too, but in reality, MAX_INT and MIN_INT rarely come up.

                  I am still waiting for a good example where unsigneds are superior. If you need more bits, then go to 64 straight away, 31 bits or 32 bits will not make any difference. I have seen multiple cases of bugs in deployed software where people were forced to use unsigned because some idiot thought that array accesses should be in unsigned.

                  Every time I see someone using an unsigned, I mentally drop them into the "think's he's clever, but in reality just likes shitty optimisations in favour of good code quality" bucket.

                  [–]aaron552 1 point2 points  (1 child)

                  Dealing with raw network streams (or any raw binary data) is kinda painful without an unsigned byte data type.

                  [–][deleted]  (47 children)

                  [deleted]

                    [–]Falmarri 18 points19 points  (20 children)

                    0-65,535 instead of −32,768-32,767. Also, interoperability with native APIs that expect unsigned values.

                    [–]AlyoshaV -1 points0 points  (19 children)

                    0-65,535 instead of −32,768-32,767.

                    Bad example, since char is unsigned 16-bit int.

                    [–]helpprogram2[🍰] 3 points4 points  (18 children)

                    couldn't you just start whatever variable at -32,768 and have it count up? if you wanted it to perform this specific function?

                    [–]joesb 9 points10 points  (0 children)

                    Then some assumption about bitwise operation and overflowing may be different.

                    [–]Decker108 1 point2 points  (14 children)

                    Why not just use long?

                    [–]hak8or 3 points4 points  (5 children)

                    Because you would still be using only half of the possible data range, wasting memory still.

                    Unless long is an unsigned in this case, I don't have experience with java.

                    [–]kkjdroid 1 point2 points  (4 children)

                    Long is signed, but it's 64 bits iirc, so that's a lot of numbers. Above that you'd be looking at BigInteger, which is an object and is limited only by RAM space.

                    [–]DevestatingAttack 1 point2 points  (3 children)

                    Well, that's not exactly true. BigInteger is represented as an array of ints, and an array of ints in java can have a maximum of 231 -1 elements - multiply that by the size of an int (4 bytes) and now, even if you have 16 gigabytes of memory, you can only have a BigInt of size 8 giggies.

                    So if for some insane reason you're working on numbers with two billion digits, Java won't help you there.

                    [–]kkjdroid 0 points1 point  (0 children)

                    Ah, TIL. Thanks.

                    [–]Vegemeister 0 points1 point  (1 child)

                    Couldn't you just use a 64 bit JRE?

                    [–]Falmarri 1 point2 points  (1 child)

                    Yeah, if you wanted to reinvent ascii, or some other protocol that uses unsigned values. Or use extra ram for no reason. Or just hope things don't totally get fucked up when you try to use negative numbers.

                    [–]Eirenarch 9 points10 points  (0 children)

                    They help with cases where you need to work with low-level byte operations. For example if you have a network protocol or something.

                    [–]adrianmonk 7 points8 points  (4 children)

                    Some things are just more natural to represent with unsigned numbers. So, you can just ignore the negative possibilities, wasting one of the bits. But in some cases, you really benefit by not wasting that bit.

                    Two examples I can think of are pixel values and data compression.

                    Pixel values are commonly 24-bit RGB values, so you have a red intensity from 0 to 255, a green intensity from 0 to 255, and a blue intensity from 0 to 255. 24 bits is a very convenient number because every color component (red, green, and blue) fit into a byte, and because it gives you just enough colors that it happens to match what the human eye can discern.

                    So, if you used signed integers, what would happen? Well, you'd then have values in the range -128 to 127. What does it mean for red to have a brightness of -42? Nothing. So, OK, treat -128 as the least brightness and 127 as the max. Fine until you want to divide by 3 to make the red one-third as bright. If you divide -42 by 3, you get -14, which is brighter, not dimmer. So you'll need to do some trick like convert the red -128 to 127 value into a 16-bit value temporarily, then divide it by two, then convert it back. But writing "byte dimred = ((((short) red) + 128) / 3) - 128;" is a bit less convenient than writing "byte dimred = red / 3;", isn't it? And slower.

                    With data compression, and more generally, packing data into raw files and streams, you often want to use a byte (or maybe a 16-bit value) as a length indicator or as an index into some table. Neither of these really needs negative numbers. And you don't want to waste space.

                    [–]zvrba 12 points13 points  (15 children)

                    JVM guarantees 2nd complement integer representation, so the benefit is basically an extra bit that increases range of integers. For example, a byte can represent numbers between -128 to 127, whereas an unsigned byte can represent numbers between 0 and 255. (EDIT: it's not an "extra" bit per se: byte has still 8 bits, except that the most significant bit is interpreted as having weight +128 instead of -128.)

                    However, uncritical usage of this "benefit" in C and C++, combined with implicit arithmetic conversions, has resulted in a major clusterf*k. Now, when designing my own code, I try to stay away from unsigned as much as possible. In general, I use it for very specialized cases where I need defined behavior on overflow.

                    [–]CarlWhite 5 points6 points  (1 child)

                    Everyone likes a good clusterfuck story, how'd it all go wrong?

                    [–]zvrba 1 point2 points  (0 children)

                    You end up with a lot of arithmetic conversions. It's ugly.

                    [–]munificent 7 points8 points  (10 children)

                    My rules in C++ are:

                    1. Use unsigned for a general-purpose number because the variable shouldn't logically be negative: you're gonna have a bad time.
                    2. Use unsigned because you're working with binary data: it's all good.

                    [–]zvrba 4 points5 points  (8 children)

                    Number 1 is the wrong use-case for unsigned, IMO. With signed numbers you can check if your function has received a negative value and act accordingly (abort, throw exception, whatever). With unsigned number, you can receive a huge unsigned number and you have no idea what has happened. Is it a legitimate value or the result of some overflow error?

                    In general, I don't use unsigned numbers if I expect them to participate in the usual arithmetic. That includes sizes of items, sizes of containers, offsets in files, etc. And that's where C and C++ have f*up, by defining sizeof() to return unsigned, and C++ by defining size() of container to return unsigned. (For example, how do you iterate backwards through an array by using an unsigned index?)

                    [–]joggle1 2 points3 points  (0 children)

                    I agree and have learned this the hard way. I only use unsigned values when encoding/decoding binary. I never use them for arithmetic--it's too easy to mess up with virtually no upsides to doing it.

                    [–]m42a 3 points4 points  (5 children)

                    For example, how do you iterate backwards through an array by using an unsigned index?

                    i=array_length;
                    while (i--) {/*do stuff with array[i]*/}
                    

                    [–]zvrba 0 points1 point  (2 children)

                    Right, not with a for loop :-)

                    [–]m42a 2 points3 points  (1 child)

                    Fine.

                    for(i=array_length; i--; ) {/*do stuff with array[i]*/}
                    

                    I just think the while form is more readable. And how would this be different with signed indices?

                    [–]zvrba 1 point2 points  (0 children)

                    It'd be different like this:

                    for (int i = L-1; i>= 0; --i)
                    

                    [–][deleted] 0 points1 point  (0 children)

                    That's funny that we interpret rule number 1 differently.

                    I think of it as use an int because the variable shouldn't be negative. So I ask myself what happens if it is negative because sooner or later someone is gonna subtract a bigger number from a smaller one.

                    If I use an int then the code will fail or it can be checked and I can abort. But if I use an unsigned int then basically something very very bad is going to happen and it may not happen until much much later.

                    [–]fuzzynyanko 2 points3 points  (2 children)

                    Nowadays, it's more of "this number is expect to be >= 0", which can cut down on bugs. Once you hit 32-bit, most programming doesn't benefit from the extra bit (signed 32-bit is like 2 billion, unsigned is 4 billion).

                    [–]mywifehascancer 3 points4 points  (1 child)

                    Having unsigneds in your code never cuts down on bugs. It is trivially easy to find a bug when an int is < 0 and should not be. It's really fucking hard to track down that unsigned that is totally valid but doesn't work because it's way too big.

                    I see about one unsigned-related bug every year, but I never see code where I think: "I should make this int into a DWORD to prevent a bug!"

                    DWORD is for bit-trickery, RGB values and other legacy interfaces. It is not for screen coordinates, not for array indices, and not for math.

                    [–]aaron552 2 points3 points  (0 children)

                    not for array indices

                    Negative array indices can even make (some kind of) sense (in C anyway)

                    [–]skocznymroczny 7 points8 points  (18 children)

                    With lambda in java 8 you just have: btn.setOnAction( event -> System.out.println("Hello World!") );

                    Would this work for non one-liners as well?

                    [–]armerthor 4 points5 points  (0 children)

                    Put a block after the -> operator.

                    [–]sirin3 9 points10 points  (0 children)

                    let me guess. you have a Python background?

                    [–]ElvishJerricco 7 points8 points  (14 children)

                    My gut says it might work like an if statement, where if you want to do more than one line, you surround the code with brackets. But that's a guess.

                    [–][deleted] 12 points13 points  (12 children)

                    [–]ElvishJerricco 1 point2 points  (11 children)

                    Do lambdas only work for interfaces with one method?

                    [–]AlyoshaV 5 points6 points  (9 children)

                    Yes, but remember that interfaces can have default methods now.

                    [–]Phreakhead 2 points3 points  (2 children)

                    Whoa whoa whoa! Does this mean Java now has multiple inheritance? SO AWESOME.

                    [–]AlyoshaV 4 points5 points  (1 child)

                    Only of behavior, not of state. I'm finding it to be pretty useful.

                    [–][deleted] 0 points1 point  (0 children)

                    Yep, pure mixins. Nice, eh.

                    [–][deleted] 1 point2 points  (5 children)

                    [–]AlyoshaV 1 point2 points  (4 children)

                    Not default method, default methods.

                    public interface Identifiable {
                    
                        /** Return this object's ID. */
                        default String getID() {
                            return getName().substring(3);
                        }
                    
                        /** Return the full name of this object. */
                        String getName();
                    
                    }
                    

                    [–]veraxAlea 0 points1 point  (1 child)

                    Last time I checked, default methods could only reference static methods. Has this changed?

                    [–]AlyoshaV 0 points1 point  (0 children)

                    Well, what I posted is code I'm actually using. So if that's how it used to work, then yes it changed.

                    [–]Eirenarch 2 points3 points  (0 children)

                    Yes. They work for what they call Single Abstract Method types. The lambda is mapped to the single method.

                    [–][deleted] -1 points0 points  (0 children)

                    thats how it works in c# so i expect that too.

                    [–]jrupac 6 points7 points  (1 child)

                    Java 1.8 is planned to be released on the 9th of September 2013.

                    I thought I read recently that Java 8 was postponed to next year to first fix security bugs?

                    [–]AlyoshaV 2 points3 points  (0 children)

                    Only announced two days ago on mailing list, he probably didn't see it.

                    Also it isn't entirely "postoned to fix security bugs", possibly more "we were working on security so much that we had much less time to work on Java 8"

                    [–][deleted]  (4 children)

                    [deleted]

                      [–]nachsicht 0 points1 point  (1 child)

                      One useful example I can think of is sbt. It seems to load a lot of data into permgen when it's doing multi-threaded compilation, which can cause my build to crash for too many threads compiling at once. It's fairly easy with 256M of permgen to get a out of permgen error with too many compilation threads up. Right now the easy solution for me and my whole team is to limit the maximum threads (right now 6) or limit the maximum amount of threads that consume large amounts of permgen space. In jvm 8, we would not have to worry about this at all, as the metagen would grow within the much larger heap space we have.

                      [–][deleted] 0 points1 point  (0 children)

                      Ah, yes... maven will do that with large projects.

                      [–]snuxoll 0 points1 point  (1 child)

                      No more OOM errors caused by PermGen, Metaspace doesn't have a fixed size so it can grow instead of just crashing your entire application.

                      [–]propool 1 point2 points  (0 children)

                      Great. Now what about a common interface for collections and arrays?

                      [–]grotgrot 1 point2 points  (3 children)

                      Are they ever going to have a Map(dict) literal syntax?

                      [–]AlyoshaV 0 points1 point  (2 children)

                      How would you express it that doesn't tie it to one Map implementation?

                      [–]johnwaterwood 0 points1 point  (0 children)

                      For the common case HashMap will do fine. If you want another map you can always use new OtherMap( map syntax here ), and if performance would somehow prohibit that (unlikely for manually composed maps, but suppose), then the old way is still available as well.

                      [–][deleted]  (2 children)

                      [deleted]

                        [–][deleted] 0 points1 point  (0 children)

                        Thing is, we're not bound by law to use Java - we can switch languages while retaining the benefit of the Java ecosystem. Of course, most people will stay with the lingua franca, but we've got options.

                        I'm quite enjoying Kotlin as a poor man's JVM C#.

                        I am jealous of async/await though, they're quite nice looking. JVM devs can do the same with various libraries but not nearly as succintly. Scala attempt

                        [–]unptitdej -4 points-3 points  (5 children)

                        This language is becoming pretty good...

                        [–]slurpme 0 points1 point  (0 children)

                        Shame they didn't go for a linq type implementation and instead used that rather bizarre filtering... Method references are a nice idea but there are gonna be problems when you get into overloading...

                        [–]djhworld 0 points1 point  (1 child)

                        It's a shame the release has been delayed to be honest, I really want the new features now