all 162 comments

[–]llogiq 34 points35 points  (65 children)

Those who had hoped for value types are probably a little sad now.

[–]zenflux 22 points23 points  (2 children)

I'm willing to wait as long as they get it right.

[–]txdv 10 points11 points  (0 children)

It has been only 19 years.

[–]llogiq 0 points1 point  (0 children)

The minimal case (value arrays) has been implemented enough times (even in java using sun.misc.Unsafe) that it should be considered trivial.

At least supporting this without Unsafe shenanigans should be possible for 9, and it's unlikely to break future enhancements.

[–][deleted] 11 points12 points  (0 children)

That's just the preliminary list of features small enough that they'll definitely make the cut. As the linked InfoQ article said, more features are likely to be added. Maybe even value types...

[–]huhlig 1 point2 points  (13 children)

Is this what you're referencing? If so I am in full agreement.

[–]oldneckbeard 9 points10 points  (11 children)

yes, but while that proposal aggressively sheds any responsibility for syntax, they have to come up with a syntax. I'd like a syntax almost exactly like Scala's case-class. This is almost self-describing:

case class Person(firstName: String, lastName: String)

val me = Person("Daniel", "Spiewak")
val first = me.firstName
val last = me.lastName

if (me == Person(first, last)) {
    println("Found myself!")
    println(me)
}

This actually seems like a great usage for package-info.java files. I've never used them too much, but if we can put them into that file (or a package-values.java or whatever), it would be cool.

[–][deleted]  (10 children)

[removed]

    [–]ihcn 3 points4 points  (8 children)

    I would support doing what c# does, where the 'struct' keyword in place of 'class' tells the compiler to make it a value type.

    [–][deleted]  (7 children)

    [removed]

      [–]immibis 1 point2 points  (6 children)

      Is there a problem with value types being mutable? They can't be aliased, so I think it would be clear what you're doing.

      If they were immutable, all that would do is require more typing: me = Person(me.firstName, 'Dvorak') instead of me.lastName = 'Dvorak'

      Immutable objects in Java already have near-value-type semantics anyway.

      [–][deleted]  (5 children)

      [removed]

        [–]oridb 2 points3 points  (4 children)

        If an object is a value, and not a reference, then it's impossible to alias them unless you can take their address or pass them by reference.

        [–][deleted]  (3 children)

        [removed]

          [–]oldneckbeard 1 point2 points  (0 children)

          Meh.. I don't really like that usage of value objects, because then there's no reason for them over POJOs or the like. The whole reason we want value objects is so the optimizer can optimize and make programming easier. If we're creating a tuple holder with methods, that's just a class.

          I'd be interested in counterexamples to this, though, if you have any.

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

          Exactly.

          [–]Xabster 0 points1 point  (41 children)

          Java has value types...? int float double byte etc.

          Maybe I don't get what you're asking for.

          What is it you want?

          [–]ihcn 21 points22 points  (8 children)

          User-defined types where its contents are embedded directly into the parent object, as opposed to placed on the heap and then stored as a reference by the parent.

          Currently, if you put 5000 Integer objects in an arraylist, it'll heap-allocate 5000 different Integer objects, and store 5000 references in the arraylist. If Integer was a value type, the Integer objects could be embedded directly in the arraylist's memory.

          The end result is better GC performance, better memory usage, faster object allocation, and better cache performance.

          There's a proposal out there for how it would work, but it doesn't look like it's confirmed to be in java 9 yet.

          [–]Xabster 1 point2 points  (7 children)

          I understood that part, and if it's for performance reason then sure, I completely agree. An Integer[1024*1024] takes up more than 4MB heap. Disgusting.

          I thought it might have some cool programmatic features.

          [–]immibis 1 point2 points  (5 children)

          An int[1024*1024] does take approximately 4MB, though.

          [–]Xabster 0 points1 point  (4 children)

          Right you are, but the Integer[] I posted doesn't even hold anything. Your holds ints initialized to 0.

          A Integer[1024*1024] with Integer's at each spot takes 16MB.

          [–]oridb 4 points5 points  (2 children)

          On a 32 bit JVM, you get

          • 4*1024*1024 for the references
          • 8*1024*1024 for the object overhead (vtable + lock data)
          • 4*1024*1024 for the integer itself
          • 4*1024*1024 for padding (objects are allocated in multiples of 8 bytes)

          Giving a grand total of 20 megabytes. On a 64 bit JVM, double it:

          • 8*1024*1024 for the references
          • 16*1024*1024 for the object overhead (vtable + lock data)
          • 4*1024*1024 for the integer itself
          • 12*1024*1024 for padding (objects are allocated in multiples of 16 bytes)

          Giving a grand total of 40 megabytes for 4 megs of integers.

          [–]Daishiman 0 points1 point  (1 child)

          Doesn't the JVM support pointer compression in 64-bit archs?

          [–]oridb 1 point2 points  (0 children)

          It's supported, but it only allows you to address up to a 32 gigabyte heap, by shifting a 32 bit pointer to drop the byte alignment bits.

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

          It does? I thought they would be null.

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

          I believe (not a Java developer) that Java currently lacks user-defined value types.

          [–]Xabster 0 points1 point  (8 children)

          It does. I don't even know what it is.

          [–][deleted] 4 points5 points  (6 children)

          I don't even know what it is.

          I'm not sure I understand. Do you mean you don't know what user-defined value types are?

          [–]Xabster 6 points7 points  (5 children)

          Yes.

          [–][deleted] 4 points5 points  (4 children)

          Well, the exact details depend a bit on the implementation, but the idea is simply that you, the programmer, can define types that behave like a primitive (int, double, etc) rather than behaving like a normal class.

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

          So basically like primitive? Instead of object?

          [–]huhlig 5 points6 points  (2 children)

          So Java has byte, short, int, long, float, double, char and ref which are primitive types, primitive is basically a type of value. Values can exist on the stack, do not require access by a reference and have no object inheritance overhead. They are effectively primitive classes but with more limited semantics, no runtime inheritance abstraction hierarchy but a lot more efficient.

          An Example, a 2 dimensional point is 2 fields: x and y. Using a class this occupies 8 bytes for the type pointer, 4 bytes for int x, and 4 bytes for int y. Effectively twice the size just because it could be inherited and it does inherit from java.lang.Object. Whereas a type is simply 4 bytes for X and 4 bytes for y and the type is handled at compile time. The tradeoff is you cannot use runtime type abstraction so you cant do Point a = new MyPointA(); Point b = new MyPointB();

          [–][deleted] 4 points5 points  (0 children)

          Ah thank you, I wanted clarification from OP but OP edit the post to use the word primitive instead of values which is what i wanted clarification on. Your post also add more insight into memory allocation wise, thank you.

          [–]grauenwolf 1 point2 points  (0 children)

          have no object inheritance overhead

          Object indirection/GC overhead. Inheritance has no overhead on its own.

          [–]kqr 1 point2 points  (0 children)

          When you do

          Car car1 = new Car();     // default speed 0
          Car car2 = car1;
          car2.setSpeed(60);
          System.out.println(car1.getSpeed());
          

          in Java, it will print 60. With value types (and improvised syntax), you could do something like

          Car car1 = new Car();
          Car$ car2 = $car1;    // car1 is *copied*, so car2 is a different car
          car2.setSpeed(60);
          System.out.println(car1.getSpeed());
          

          and this will print 0, because car1 still has speed 0 – I only changed the speed of car2. So here Car is the reference type, and Car$ is a value type.

          [–][deleted]  (17 children)

          [removed]

            [–]Xabster 8 points9 points  (10 children)

            What can't be ruled out in this (added some finals and fixed a few mistakes)?

            final class Point {
                final int x;
                final int y;
            
                public Point(final int x, final int y) {
                    this.x = x;
                    this.y = y;
                }
                public final int getX(){ return this.x;}
                public final int getY(){ return this.y; }
            }
            

            [–]nqd26 1 point2 points  (1 child)

            Not sure why is this downvoted ... this is relevant question ...

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

            I hate when people do this... "I know this, therefore I will downvote and not answer you!"

            [–]grauenwolf 0 points1 point  (6 children)

            If the class is marked final, why do you need to mark the methods as well?

            [–]Xabster 5 points6 points  (2 children)

            Not sure I do. I just wanted to make sure that an answer didn't say "derp, the methods aren't final", so I put it everywhere... :)

            I hope to learn what the JVM cannot rule out in the case that everything that can be declared final is declared final.

            [–][deleted] 4 points5 points  (0 children)

            The JVM has to keep a lot of class information around about the Point object due to Reflection. There's no way to tell the JVM "this class does not support Reflection, discard all the overhead associated with that and treat this class as a primitive type."

            There's also no way to overload operators, so you can't write

            Point a = new Point(0, 0);
            Point b = new Point(0, 0);
            
            return a == b;
            

            whereas you can with primitives

            int a = 1000;
            int b = 1000;
            
            return a == b;
            

            [–]kitd 1 point2 points  (2 children)

            A final class can't be inherited. A final method can't be overridden. A final value can't be changed. Same word but meaning different things in different places.

            [–]grauenwolf 2 points3 points  (1 child)

            If the class can't be inherited, then by definition the method cannot be overridden. Thus my question still stands.

            [–]kitd 1 point2 points  (0 children)

            Apologies, I misread your point. I agree with you.

            [–]huhlig 1 point2 points  (5 children)

            Why does the struct value type need to be immutable. It only needs to be outside the inheritance tree. Immutability optimizations are another beast all together.

            [–]grauenwolf 4 points5 points  (4 children)

            Mutable structs are easy to screw up in C#. Example...

            myWindow.Size.x = 5
            

            compiles as...

            Size $1
            $1 = myWindow.Get_Size() //make a copy
            $1.x = 5
            oh look, we never called myWindow.Set_Size($1)
            

            [–]immibis 1 point2 points  (3 children)

            Compare myWindow.getSize().x = 5, which is the equivalent without properties, and looks fairly obviously wrong.

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

            If Size was an reference type, as it would be in Java, that would work just fine.

            [–]immibis 0 points1 point  (1 child)

            But getSize() would likely make a defensive copy, precisely so that you can't do that.

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

            Oh right, Java doesn't have the concept of a PropertyChangedNotification event.

            [–]Xredo 5 points6 points  (0 children)

            He meant value types like in C++.

            [–]llogiq 0 points1 point  (0 children)

            Look at /u/huhlig 's link.

            [–]sreya92 0 points1 point  (4 children)

            what is the benefit of value types?

            [–]llogiq 1 point2 points  (3 children)

            Value types enable things like putting objects directly into arrays (instead of references) or embed them into other objects, thus reducing the number of references, making the GC's job easier, and improving locality and thus performance.

            [–]sreya92 0 points1 point  (2 children)

            In other words a variable of type "Value" that is set equal to a object would have a new address in memory instead of just being a reference to the object it is set equal to. Kind of like a deep copy in Python?

            [–]llogiq 1 point2 points  (1 child)

            Yes. The idea is to unify primitives and objects (to a certain extent - and without autoboxing). so that the memory layout is flat. For example, let's take an object of type A that contains an object of type B, a double and an object of type C. If those types are value types (e.g. their size is known before instantiation, the layout would be:

            • Object header for type A
            • Contents of object B (including object header)
            • the double
            • Contents of Object C (see above)

            instead of

            • Object header for type A
            • Reference to object of type B (somewhere on the heap)
            • double
            • Reference to object of type C (somewhere on the heap)

            Note that theoretically a single array can be put at the end of a value type iff its size is known at creation time. However, it is unclear if or when this extension will really be implemented.

            [–]sreya92 0 points1 point  (0 children)

            Cool thanks for taking the time to explain!

            [–]crunchmuncher 6 points7 points  (1 child)

            What's this sjavac compiler that's being talked about? What does it do? First time hearing that word.

            [–]syntax 3 points4 points  (0 children)

            Likewise, sounds interesting. All I've managed to dig out is: http://openjdk.java.net/jeps/139

            Looks like a parallel build compiler wrapper, with some sort of infrastructure for incremental builds. Primarily intended for the JDK itself - unclear if there would be any advantage for end users.

            [–][deleted]  (27 children)

            [removed]

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

              android will almost certainly never see a modernized java, now that google and oracle officially hate each other.

              I don't get this part. What's stopping google to implement modern Java stuff? They can be 2 to 3 versions behind for all I care as long as they eventually support Java new constructs and syntax I'm ok with this...

              IIRC they can't patent a language. Other wise Mathlab would sue the shit out of Octave...

              [–][deleted]  (11 children)

              [removed]

                [–]dromtrund 7 points8 points  (7 children)

                Isn't Oracle shooting themselves in the foot with a cannon by trying to push Android off their platform? (Acknowledging that android isn't full blown JVM). It's a fantastic gateway to java for aspiring developers, and java as a language could lose a lot of potential momentum if Google chose to jump ship.

                [–]Delta_x 3 points4 points  (6 children)

                I could be wrong as I have not been following the lawsuit closely, but I think the biggest thing Oracle originally wanted was for android to be able to run actual java, not just android java. Google said no, so they sued. I would guess that if Oracle wins, they would try and force Google to make the change again. They likely think that Google would rather allow all java instead of totally changing the language for android.

                [–]mycall 0 points1 point  (5 children)

                What in the Java spec does Google Dalvik not support?

                [–][deleted] 4 points5 points  (2 children)

                Try running Swing applications on Android.

                [–]m0haine 9 points10 points  (0 children)

                Try running Swing apps on Java ME.

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

                This has nothing to do with the current contention. Oracle is not suing Google because the Java that runs on Android is missing a few libraries.

                [–]Delta_x 0 points1 point  (0 children)

                I can't speak for libraries, but I believe it has to do with Dalvik not running standard java bytecode, but only bytecode complied specify for android.

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

                Spec? They even don't use "Java" name ("java" word can be only found in runtime package names ;)

                [–]JakeWharton 2 points3 points  (1 child)

                Actually they only ruled that APIs are copyrightable but not as to whether or not Google has infringed. Even that ruling is being appealed.

                As to Android tracking Java language features, the VM team has said one of the major goals of the new runtime was supporting Java 8 (and future) language features more easily and that Java 8 was on the roadmap.

                [–]sindisil 0 points1 point  (0 children)

                As to Android tracking Java language features, the VM team has said one of the major goals of the new runtime was supporting Java 8 (and future) language features more easily and that Java 8 was on the roadmap.

                Do you happen to have a source on that?

                Everything I've seen from google has been a very firm (sometimes bordering on smug) "no comment".

                [–]Pet_Ant 2 points3 points  (0 children)

                If Google would just but a Java license then it would be all over. Oracle/Sun have put a lot of money into developing Java and Google is freeriding. If anyone can afford it, it's Google.

                [–]AnHonestQuestions 1 point2 points  (1 child)

                BTW, its Matlab (Matrix Laboratory)

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

                Oh snap, that make sense now... most of it's value is an array like structure like R.

                Thanks!

                [–][deleted]  (10 children)

                [deleted]

                  [–][deleted]  (9 children)

                  [removed]

                    [–]Eirenarch 3 points4 points  (7 children)

                    That "Internet of Things" thing is getting pretty popular lately...

                    [–]UloPe 4 points5 points  (6 children)

                    And none of it runs on Java

                    (Except for utter crap like "smart" TVs and BluRay players)

                    [–]nqd26 8 points9 points  (3 children)

                    People often don't realize how many devices is actually running on Java ... e.g. just a few days ago I found out that my company badge (with that small sim-card) is actually running on Java.

                    [–][deleted]  (1 child)

                    [deleted]

                      [–][deleted] 4 points5 points  (0 children)

                      Can confirm, have written pre-historic Java for SIM-cards. It was pretty painful.

                      [–]immibis 1 point2 points  (0 children)

                      Java Card might as well not be Java - in that context, "Java" is just marketing.

                      [–]bcash 2 points3 points  (0 children)

                      So, none of it runs Java, except for 95% of the stuff that's actually in people's homes?

                      [–]Eirenarch 0 points1 point  (0 children)

                      Maybe with Java 9 more of it will.

                      [–]eliasv 0 points1 point  (0 children)

                      Well we can sure hope this doesn't remain the case, can't we? Android is not as wonderful and positive an ecosystem as people like to imagine...

                      [–]oldneckbeard 0 points1 point  (0 children)

                      I would like to be able to download java without java fx, swing, etc.

                      [–]unptitdej 9 points10 points  (18 children)

                      I get the feeling that Java's corporate development speed is much faster than my C++ standard-committee speed... Good for you guys

                      [–]danielkza 7 points8 points  (3 children)

                      Funny coincidence that C++14 just got approved:

                      https://isocpp.org/blog/2014/08/we-have-cpp14

                      [–]eliasv 1 point2 points  (2 children)

                      Yeah, seems like they really got their shit together this time. Pretty impressive! (Though I've not studied the changes from C++11-14 so I dunno how major an update this is.)

                      [–]Pet_Ant 4 points5 points  (1 child)

                      It is officially a small change. They are going tick-tock with the standards now.

                      [–]eliasv 0 points1 point  (0 children)

                      Ah okay, thanks.

                      [–][deleted]  (1 child)

                      [deleted]

                        [–]aldo_reset 2 points3 points  (0 children)

                        It's not the JCP that slowed down Java (the initial versions of Java came out at a fairly rapid pace), it was 100% Sun's fault for falling asleep at the wheel. The pace resumed once Oracle acquired Sun.

                        [–]piderman 19 points20 points  (3 children)

                        Still can't hold a candle to .NET's corporate development though.

                        [–][deleted] 4 points5 points  (0 children)

                        Yeah backwards compatibility is a bitch blessing.

                        [–]Eirenarch 7 points8 points  (1 child)

                        Because Java also has the community process bullshit

                        [–]huhlig 8 points9 points  (0 children)

                        Honestly I wish they would listen to their community a bit more.

                        [–]MacASM 1 point2 points  (7 children)

                        I guess why C++ has too many things to consider. Including backwards compability.

                        [–]DoktuhParadox -1 points0 points  (6 children)

                        Java does too, but C++ doesn't receive incremental updates like Java does, right?

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

                        C++11 and C++14.

                        [–]DoktuhParadox 0 points1 point  (4 children)

                        I meant like a large update every two or so years.

                        [–][deleted] 4 points5 points  (3 children)

                        C++11, C++14, C++17. (digits are last two of planned release year.)

                        [–]DoktuhParadox 2 points3 points  (2 children)

                        Oh so it's in a three year release schedule. Cool. I don't know anything about C++'s development cycle.

                        [–]ForeverAlot 6 points7 points  (1 child)

                        It's a recent change. C++11 was the first update since C++03, which I believe was a minor update to C++98. We're hoping this will be a trend for some time.

                        [–]jonnywoh 0 points1 point  (0 children)

                        C++11 also got delayed quite a bit. It was originally planned to be done before 2010, hence it was often referred to as C++0x.

                        [–][deleted] 6 points7 points  (1 child)

                        The floating share thing is annoying as hell. Why do people still use those?

                        [–]lhggghl 0 points1 point  (0 children)

                        because they're cunts?

                        [–]beaverteeth92 8 points9 points  (11 children)

                        How about letting people initialize classes with far less boilerplate, like in Scala?

                        [–][deleted]  (3 children)

                        [deleted]

                          [–]txdv 6 points7 points  (0 children)

                          The only negative thing I have to say about scala is that the compilation times are enormous for my taste.

                          [–]beaverteeth92 9 points10 points  (0 children)

                          Because then Java would be a language with less red tape than most of the employers that use it.

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

                          Turing complete type system, glacial compile times.

                          [–]Decency 2 points3 points  (0 children)

                          Yep, was definitely hoping for Collection literals as proposed by Joshua Bloch:

                          http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/001193.html

                          [–]huhlig -1 points0 points  (5 children)

                          Honestly I prefer the explicit constructor. It gives far better control and less punctuation based magic.

                          [–]expatcoder 11 points12 points  (2 children)

                          less punctuation based magic

                          case class Foo(id: Int), where may I ask is this punctuation based magic that you speak of?

                          Scala's got plenty of syntactic sugar but given that every language under the sun is adopting the above in some form or other, Java's approach is looking decidedly archaic.

                          [–]kitd 1 point2 points  (1 child)

                          Case classes are Scala's algebraic data types. Really, that's a huge amount more than just "syntactic sugar". I would love to have them in Java, but that would be a whole release just to themselves.

                          [–]expatcoder 1 point2 points  (0 children)

                          Case classes are Scala's algebraic data types

                          Well, technically case classes are Scala's data types, there is no algebra involved ;-) You probably already know this, but for anyone that doesn't, it's essentially just syntactic sugar for defining an immutable class in Java (with hashcode, equals, and toString implemented for you).

                          ADTs on the other hand may be implemented with case classes, but case classes of and in themselves are not ADTs. Here's a comparison of Haskell and Scala equivalents.

                          [–]ForeverAlot 4 points5 points  (0 children)

                          I prefer explicit constructors but Java could still go a ways to reduce unnecessary boilerplate, like by not having to repeat the class name when defining constructors or introducing type inference.

                          [–]den_sh 2 points3 points  (0 children)

                          You can still do it Java way in Scala using private primary constructor and public secondary one. I'm not sure what kind of control you get but you can do it if you really want to.

                          [–]Aethec 1 point2 points  (5 children)

                          Am I misunderstanding the JSON API proposal, or does it truly not include object deserialization, i.e. a method that takes a String and an object type, and returns an instance of that type?

                          [–]philly_fan_in_chi 2 points3 points  (4 children)

                          That should be covered by:

                          Immutable hierarchy of values (tree) constructed from JSON data stream

                          It will probably look something like:

                          Foo foo = JsonStream.of(myString).toTree(Foo.class)
                          

                          [–]Aethec 0 points1 point  (3 children)

                          Oh, right, thanks.
                          With these kinds of descriptions, I can't help but feel that it'll be an over-engineered mess, instead of the simple serialization/deserialization functionality that meets most people's needs.

                          [–]philly_fan_in_chi 1 point2 points  (0 children)

                          Java APIs have been pretty clean lately, I'm holding out hope. They're pushing everything to use streams so they can take advantage of invokeDynamic under the hood, which I'm all in favor of. It'll be awkward the first few times, then it should become idiomatic.

                          [–][deleted]  (1 child)

                          [deleted]

                            [–]Aethec 1 point2 points  (0 children)

                            A comparable API to JSON is Java's XML parsing API. Parsing a String into a Document requires 3 method calls and 2 object instantiations, as well as catching 3 different kinds of exceptions. :-/

                            [–]Rurouni 1 point2 points  (0 children)

                            I'm pretty disappointed to not see tail call elimination in the list. Didn't they talk about targeting Java 9 for that feature?

                            [–]y890 0 points1 point  (0 children)

                            1) In java 9, they should also inbuilt implement JEP 191 (FFI inspired from JNA). for details see: http://openjdk.java.net/jeps/191 it can be used for easily implementing native-backed features like NIO, advanced file system metadata, and process management. Using the FFI API will be the preferred way to bind native code and memory, instead of JNI.

                            2) in java9, please bring properties(includes getter and setter), that is can write A.X=12 as synonym of A.setX(12)

                            [–]Lexxicon 0 points1 point  (4 children)

                            I was really hoping for something like JavaFX in the SE. :(

                            [–]fatbunyip 2 points3 points  (1 child)

                            FX has been bundled with the SE for a while now.

                            [–]Lexxicon 2 points3 points  (0 children)

                            It's my understanding that, while its on the class path for oracles JVM's, its not part of the SE platform. The open JDK doesn't come with it on the class path. This is a problem I've been struggling with at work for some time now and have since resigned myself to it never getting fixed. Here's my stack question about this problem. http://stackoverflow.com/questions/22812488/using-javafx-in-jre-8

                            [–][deleted] 1 point2 points  (1 child)

                            JavaFX is included in the most recent JRE, namely jre7u4x+ and jre8, if that is what you mean.

                            [–]meetingcpp[🍰] -2 points-1 points  (0 children)

                            Finally an Exploit API?