Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Features that stand out in Java 9  RSS feed

 
Ranch Hand
Posts: 183
1
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Author,

As far as the new features are concerned ,Java 8 was a Mile Stone after Java5 ..
What would you pick as some of the things that stand out in Java 9 which are comparable to Java 8?

Thanks
Sundar
 
gunslinger & author
Ranch Hand
Posts: 130
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The changes in Java 9 are not nearly as significant as the ones made in Java 8, other than possibly the adoption of the module system. Java 9 includes private methods in interfaces, as well as many new methods in Stream, Optional, and other classes, but those are much more incremental changes.
 
Marshal
Posts: 59131
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can they fit more methods into Stream? Yes, there are four new methods that I can see. The two xxxWhile methods look interesting.
 
Kenneth A. Kousen
gunslinger & author
Ranch Hand
Posts: 130
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, it's the dropWhile and takeWhile methods that existing functional programming people are excited about. You can also now an iterate method that takes a third argument, which makes it look more like a for loop.
 
Saloon Keeper
Posts: 1609
56
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did find this interesting:

has now be deprecated in favor of

This, I believe is the same for Byte, Short, Long, Float and Double. This may also be the same Boolean and Character classes.

It is very possible that I find this interesting because I'm preparing for the OCA 1Z0-808 exam where there could very well be questions on the objects listed above.
 
Kenneth A. Kousen
gunslinger & author
Ranch Hand
Posts: 130
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. The wrapper class constructors are not considered thread safe. Instead, the static valueOf method are preferred, as you found.
 
Campbell Ritchie
Marshal
Posts: 59131
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know why the Integer(int) constructor was deprecated?
 
Pete Letkeman
Saloon Keeper
Posts: 1609
56
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Do you know why the Integer(int) constructor was deprecated?


If you are asking me, then the answer is maybe. I did not know what Kenneth mentioned before it was posted, so that could be one reason.

However in the official documentation,
http://download.java.net/java/jdk9/docs/api/java/lang/Integer.html#constructor.summary
They say that it's slow and it was rarely appropriate to use that constructor.

I suspect that there could be other reasons, which I'm not aware of.
 
Campbell Ritchie
Marshal
Posts: 59131
180
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Old hands know lots more than that API note tells you.
When I started, generics and boxing hadn't yet been introduced. I think there were lots of complaints about generics because older languages like Eiffel and C++ supported generics when Java® didn't. The basis for introducing generics was worked out about 1999 but it wan't until Java5 in 2004 that generics were implemented. Before that we had to write things like:-Old Eiffel users will of course remember that Bertrand Meyer was scathing about the absence of generics in Java®. Anyway, 2004 came and 2004 went and it became possible to say that List would only contain Integers. It has never been possible to implement a List<int>.But they introduced boxing at the same time, so we no longer had to write the constructor explicitly:-Now, doesn't that look better
So, let's look at the Java® Language Specification (=JLS). It tells you about boxing, and also that a certain range of values must be cached. So if I box 123 1,000,000,000×, my fingers will fall off and I shall have one Integer instance representing 123. Had I used new Integer(123), I shou‍ld have 1,000,000,000 different instances. I believe that boxing uses the Integer#valueOf(int) method but I am not certain; you can verify that with the javap tool. If you look up the Integer#valueOf(int) method, you will find it caches values just as does boxing, which means that method is better to use than the constructor. Somewhere on this forum, Rob Spoor said that Cay Horstmann's suggestion for calculating hash codes was horrible because it uses new Integer(int). So there was a widespread feeling that that particular constructor was bad practice. And in more recent editions of his book, Horstmann has used Integer.valueOf(int).
As well as boxing, unboxing was introduced in Java5. I believe that unboxing uses the intValue() method inherited from Number (not certain), which has been available since the first days of Java® for unboxing (that valueOf method was introduced in Java5).

So, we now have a situation where there is a factory method which works better than the constructor. It caches certain values (the range can be increased by the user) and is therefore better than that constructor. If you look in Effective Java by Joshua Bloch or other sources, you will find that factory methods can often be better than public constructors. Look at the classes in the java.time package. Most of them don't have a public constructor. Obviously the developers at Oracle have decided to be assertive and they have now deprecated that constructor in Java9.
 
Kenneth A. Kousen
gunslinger & author
Ranch Hand
Posts: 130
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a great answer. I remember hearing that the idea was to use caching rather than creating new objects, but for some reason I keep forgetting it. As you say, it's a case where a factory method is preferred over a constructor.
 
Campbell Ritchie
Marshal
Posts: 59131
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth A. Kousen wrote:That's a great answer. . . .

Thank you
 
Campbell Ritchie
Marshal
Posts: 59131
180
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yesterday, I wrote:. . . you can verify that with the javap tool. . . .

And now I have a few minutes to try it out:-

javac BoxingDemo.java
javap -c BoxingDemo

The lines marked 7: and 18: shou‍ld verify which methods are used; this might change in future versions if new methods are introduced.
 
Bartender
Posts: 2166
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interestingly, the Java Language Specification does not mandate that Integer.valueOf(int) is used. It only says in 5.1.7. Boxing Conversion that:

If p is a value of type int, then boxing conversion converts p into a reference r of class and type Integer, such that r.intValue() == p


I googled JLS unboxing and I didn't get what I expected.
 
Campbell Ritchie
Marshal
Posts: 59131
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:. . . the Java Language Specification does not mandate that Integer.valueOf(int) is used. . . .

No, otherwise that would mandate an implementation detail. If ever a more efficient analogue of valueOf is designed, it would be possible to change to use that instead. Let's try with what they had before valueOf was introduced. You can't use boxing but you can use something different.

javac -source 1.4 StringConcatenation.java
warning: [options] bootstrap class path not set in conjunction with -source 1.4
warning: [options] source value 1.4 is obsolete and will be removed in a future release
warning: [options] target value 1.4 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:‍-options.
4 warnings
javap -c StringConcatenation

Without the source flag it defaults to Java8. I must remember to keep an old JDK in case I ever need to go back to JDK1.4.

javac StringConcatenation.java
javap -c StringConcatenation

You shou‍ld be able to guess the differences without my having to tell you.
 
Saloon Keeper
Posts: 8910
167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really happy with the takeWhile() and dropWhile() methods. Still a little peeved they didn't add a zip() method, but I assume that it's hard to consolidate with the implicitly concurrent nature of streams (or lack thereof!).
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!