Java 8 released


Recommended Posts

well then. wasn't expecting that for another year or two. 

are there any major improvements from version 7?

 

mostly just use java for minecraft so only slightly interested

Link to comment
Share on other sites

well then. wasn't expecting that for another year or two. 

are there any major improvements from version 7?

 

mostly just use java for minecraft so only slightly interested

 

I found this post on reddit: http://winterbe.com/posts/2014/03/16/java-8-tutorial/

Link to comment
Share on other sites

Lambda expressions! 6 years after C# and 3 years after C++, but better late than never.

 

I think Scala will still make more sense for new development, but for legacy code having lambdas will make for nicer refactoring capabilities.

Having never used lambdas, what is the benefit?  I have read up on their use in C#.. but not sure where the benefit is.  It seems almost like it just replaces a function that could be cleanly written anyways?

Link to comment
Share on other sites

Having never used lambdas, what is the benefit?  I have read up on their use in C#.. but not sure where the benefit is.  It seems almost like it just replaces a function that could be cleanly written anyways?

Lambdas are more than just functions because they capture all the variables in scope, so they're syntactic sugar for classes, not functions - that's actually what they compile to in C#. They're essential for programming in a functional style because they allow for easy creation and composition of functions. Usually classes in these languages occupy an entire file by themselves - reducing it to a single line enables very powerful techniques that would be awkward without!

 

If you haven't tried functional programming, you should check out F# (if you're on .NET) or Scala (for the JVM). http://www.tryfsharp.org/

Link to comment
Share on other sites

Lambdas are more than just functions because they capture all the variables in scope, so they're syntactic sugar for classes, not functions - that's actually what they compile to in C#. They're essential for programming in a functional style because they allow for easy creation and composition of functions. Usually classes in these languages occupy an entire file by themselves - reducing it to a single line enables very powerful techniques that would be awkward without!

 

If you haven't tried functional programming, you should check out F# (if you're on .NET) or Scala (for the JVM). http://www.tryfsharp.org/

 

I think I understand.  I guess I just don't see why you would do like:

 

delegate int del(int i);
static void Main(string[] args)
{
    del myDelegate = x => x * x;
    int j = myDelegate(5); //j = 25
}
 

 

when you could either do:

 

static void Main(string[] args)
{
    int j = 5*5;
}

or

static void Main(string[] args)
{
    int j = square(5)
}

int square(int num)
{
   return num*num;
}

And achieve the same while being a little more readable.

 

Having never programmed functionally maybe I just don't understand it.   I'll take a look at F#.  In your opinion what makes functional programming beneficial / good?

Link to comment
Share on other sites

Does anyone know if Java 8 has better HighDPI support?

 

Unless Java8 includes a new graphical library I believe the answer is no. as that would be down to the graphical libraries like Swing and such. 

Link to comment
Share on other sites

I think I understand.  I guess I just don't see why you would do like:

 

delegate int del(int i);
static void Main(string[] args)
{
    del myDelegate = x => x * x;
    int j = myDelegate(5); //j = 25
}
 

 

when you could either do:

 

static void Main(string[] args)
{
    int j = 5*5;
}

or

static void Main(string[] args)
{
    int j = square(5)
}

int square(int num)
{
   return num*num;
}

And achieve the same while being a little more readable.

 

Having never programmed functionally maybe I just don't understand it.   I'll take a look at F#.  In your opinion what makes functional programming beneficial / good?

If the delegate is doing work in a separate thread, than you can deal with the separate thread as a single line (or two) instead of a separate class.

Link to comment
Share on other sites

Having never programmed functionally maybe I just don't understand it.   I'll take a look at F#.  In your opinion what makes functional programming beneficial / good?

 

Well, let's take a simple example. Say you have a collection of numbers, and you want the sum of all even numbers in that list. With lambdas and higher-order functions, in a language like F#, you would do:

let sum = collection
          |> Seq.filter(fun n -> n % 2 = 0)
          |> Seq.fold (+) 0

In an imperative style, using C# for example, you would do:

int runningTotal = 0;
foreach(var n in collection) {
    if (n % 2 == 0) {
        runningTotal += n;
    }
}

There's a big conceptual difference between the two. The F# code is declarative: it states that we take a collection, take the elements divisible by 2, and "fold" (compute a single value) using the + operator and starting from 0. We are not stating how to perform this computation, we are stating what the computation is.

 

The C# code is all about implementation, state and control flow. We implement sum using a variable that holds the current state of the computation (runningTotal). We explicitely iterate over a collection, and alter control flow using an if statement to skip certain elements. 

 

This might not mean that much, until we run this over 1000000 elements doing a more complicated computation, and suddenly we'd like to run this in parallel to take advantage of our multi-core CPUs. Since the F# code states nothing about how the computation is performed, it's trivial to make it parallel:

 

let sum = collection
          |> PSeq.filter(fun n -> n % 2 = 0)
          |> PSeq.fold (+) 0

That's it, I changed 2 characters to use the Parallel Sequence module rather than the regular Sequence module. 

 

Because the imperative code is all about implementation, state and control flow, it would have to be changed dramatically to work with multiple threads.

 

Multithreading in imperative, object-oriented code is very hard, because it's difficult to reason about the order of state changes with concurrency; functional programming can help greatly because it favors pure (stateless) functions and a more declarative style.

 

Functional programming means functions that are more reusable, more obviously correct, easier to understand, easier to test, easier to parallelize, easier to compose, etc., etc. The advantages are many. http://fpbridge.co.uk/why-fsharp.html

Link to comment
Share on other sites

Unless Java8 includes a new graphical library I believe the answer is no. as that would be down to the graphical libraries like Swing and such. 

That's such a bummer. Thanks

Link to comment
Share on other sites

Lambda expressions, meh... They only did it to stop the Groovy/Scala exodus.

Java is slowly turning into ######ing Python-like programming style hodgepodge with curly braces.

Good show on the new Time and Date API. Still missing the Money and Currency API (JSR 354).

Link to comment
Share on other sites

 

Well, let's take a simple example. Say you have a collection of numbers, and you want the sum of all even numbers in that list. With lambdas and higher-order functions, in a language like F#, you would do:

let sum = collection
          |> Seq.filter(fun n -> n % 2 = 0)
          |> Seq.fold (+) 0

In an imperative style, using C# for example, you would do:

int runningTotal = 0;
foreach(var n in collection) {
    if (n % 2 == 0) {
        runningTotal += n;
    }
}

There's a big conceptual difference between the two. The F# code is declarative: it states that we take a collection, take the elements divisible by 2, and "fold" (compute a single value) using the + operator and starting from 0. We are not stating how to perform this computation, we are stating what the computation is.

 

The C# code is all about implementation, state and control flow. We implement sum using a variable that holds the current state of the computation (runningTotal). We explicitely iterate over a collection, and alter control flow using an if statement to skip certain elements. 

 

This might not mean that much, until we run this over 1000000 elements doing a more complicated computation, and suddenly we'd like to run this in parallel to take advantage of our multi-core CPUs. Since the F# code states nothing about how the computation is performed, it's trivial to make it parallel:

 

let sum = collection
          |> PSeq.filter(fun n -> n % 2 = 0)
          |> PSeq.fold (+) 0

That's it, I changed 2 characters to use the Parallel Sequence module rather than the regular Sequence module. 

 

Because the imperative code is all about implementation, state and control flow, it would have to be changed dramatically to work with multiple threads.

 

Multithreading in imperative, object-oriented code is very hard, because it's difficult to reason about the order of state changes with concurrency; functional programming can help greatly because it favors pure (stateless) functions and a more declarative style.

 

Functional programming means functions that are more reusable, more obviously correct, easier to understand, easier to test, easier to parallelize, easier to compose, etc., etc. The advantages are many. http://fpbridge.co.uk/why-fsharp.html

 

Ah okay.  So doing calculations and such makes a lot of sense to go with the Functional.  But would it be viable for like.. a forms style app, or game? Or is it meant more for computational style applications?   As for the lambdas I think I see the benefit.  A lot of it comes down to flexiblity, using different functions cross thread without issue.  Declaring on the fly functions and using them without needing separate code or redundant code.

Link to comment
Share on other sites

Ah okay.  So doing calculations and such makes a lot of sense to go with the Functional.  But would it be viable for like.. a forms style app, or game? Or is it meant more for computational style applications?   As for the lambdas I think I see the benefit.  A lot of it comes down to flexiblity, using different functions cross thread without issue.  Declaring on the fly functions and using them without needing separate code or redundant code.

If your application consists mainly of boilerplate code like setting up forms and hooking up events, then there's not much that a functional language can do about it. You'll have the same boilerplate in a slightly different syntax. But if you have any sort of complexity beyond that then there's no doubt that at least being able to think functionally, regardless of language, helps tremendously. Few things have been as efficient in terms of improving my coding skills than learning a bit of F#. 

On the topic of functional programming in games, John Carmack has written a good article about how to apply functional techniques in C++: http://www.altdevblogaday.com/2012/04/26/functional-programming-in-c/

Link to comment
Share on other sites

This topic is now closed to further replies.