- Exciting New Features For Laptops
- Exciting New Features For Kids
- Exciting New Features & New Plans Including
- Exciting New Features For Iphone 11
- Exciting New Features For Iphone 8
Last week at Microsoft Build, there have been a lot of exciting annoucements!.NET 5, Blazor WebAssembly,.NET MAUI, WinUI But the thing I’m most eager to get my hands on is C# 9, which introduces many interesting new features, so let’s take a quick tour! There’s a long list, so I won’t cover all of them here, but I will highlight the ones I find the most interesting. So, if you’re in need of a little novelty and suspense right now, read on for 5 exciting new features Instagram is working on right now. Instagram is sharing revenue with creators for the first time.
Last week at Microsoft Build, there have been a lot of exciting annoucements! .NET 5, Blazor WebAssembly, .NET MAUI, WinUI… But the thing I’m most eager to get my hands on is C# 9, which introduces many interesting new features, so let’s take a quick tour! There’s a long list, so I won’t cover all of them here, but I will highlight the ones I find the most interesting.
Note: Unfortunately the new C# features aren’t supported yet in the latest SDK preview, so we can’t test them in actual projects. Some features can be tried in SharpLab, but things are moving fast, so the bits available in SharpLab don’t always reflect what has been announced at Build.
Update 2020/06/17: A few of the features mentioned in this post are now available in the .NET 5 preview 5 SDK.
Exciting New Features For Laptops
In C# 9, it will be possible to omit the type in object creation expressions, if the type can be inferred from the context, making code terser and less repetitive:
This feature introduces a simple syntax to automate null checks on method parameters. For instance, this code :
Can be simplified to this:
! after the parameter name automatically inserts a null check for that parameter.
Pattern matching improvements
Exciting New Features For Kids
C# 9 comes with a few improvements to pattern matching. The most useful, in my opinion, is the
not pattern, which lets you write code like this:
>=, etc.) and logical operators (
or) can also be used in pattern matching:
This is the big one, in my opinion. Creating simple data types in C# have always been more painful than it should be; you have to create a class, declare properties, add a constructor if you want your type to be immutable, override
GetHashCode, maybe add a deconstructor, etc. C# 7 tuples made this a little easier, but still not ideal since a tuple is anonymous. The new Record feature in C# 9 makes things much easier!
For instance, a simple class representing a point might look like this, if you implement equality, deconstructor, etc.
In C# 9, using the Records feature, the above class can be reduced to this:
Yup, just one line, and not even a long one! How great is that? Note that it also works with structs, if you need a value type.
Note that records are immutable: you can’t change the values of their properties. So if you want to modify an instance of a record type, you need to create a new one (this should be familiar, since the same is true of dates and strings, for instance). The current approach would be to do something like this:
Basically, copy all properties from the original instance, except the ones you want to change. In this case, it’s OK because there are only 2 properties, but it can quickly become annoying when there are many properties.
C# 9 introduces
with expressions, which let you do this instead:
with expression makes a clone of the original object, with the modified properties specified between curly brackets.
There are several sub-features related to records (e.g. init-only properties), that I won’t cover here. Check out Mads Torgersen’s article for a more in-depth description.
There’s a small thing that has been annoying C# developers for years: when using the conditional operator (also known as “ternary”), there must be a type conversion from one side to the other. For instance, this code doesn’t compile:
Because there’s no conversion between
FileStream. To fix it, one side has to be explicitly cast to
In C# 9, the code above will be allowed, if both sides are convertible to the target type (in this case,
Currently, when you override a method from a base class, the overriding method must return the same type as the base class method. In some situations, it would be more practical to return a more specific type. C# 9 makes this possible by allowing overriding methods to return a type that derives from the base method’s return type:
This feature aims to reduce boilerplate code for simple programs. Currently, even the simplest program needs a class with a
This just adds noise, and make things confusing for beginners. C# 9 will make it possible to omit the
Program class and
Main method, so that the code above can be simplified to this:
Most of the features introduced by C# 9 are relatively small ones, designed to make code simpler, less cluttered and more readable; they’re very convenient, but probably won’t change how we write code in a very fundamental way. Records are another story, though; they make it much easier and less painful to write immutable types, which I hope will encourage developers to take advantage of immutability whenever possible.
Note that the release of C# 9 is still a few months away, and things are still moving, so some of the features I mentioned in this post could be modified, postponed to a later version, or even abandoned completely.
Perhaps the most cited reason for not using Java for development is that it’s too verbose. Developers complain that there’s too much boilerplate, and that no new features for Java developers have been released in a long time. The idea that Java is not growing and changing is flatly false. Although it’s true that other languages built on the JVM have gained significant traction, especially Kotlin, Java is certainly fighting back. These new Java features are coming… and fast.
Since Oracle has split JDK into a Commercial Oracle JDK and OpenJDK, the development and release cycle for the JDK has increased at an alarming rate. On OpenJDK’s website, you can see the last 5 versions of the OpenJDK have all been released since 2018. The first 9 versions of Java SE were released over the course of 18 years.
The goal of Project Amber is to add productivity-oriented features to the Java language. Project Amber is taking huge steps forward in the developer experience in Java. As someone with a habit of furtively eyeing other languages, I felt a sense of relief and excitement knowing the direction OpenJDK was taking Java. Here are the main goals of project Amber, per the OpenJDK Website.
- JEP 286 Local-Variable Type Inference (
var) (JDK 10)
- JEP 323 Local-Variable Syntax for Lambda Parameters (JDK 11)
- JEP 361 Switch Expressions (JDK 14)
Currently in progress:
- JEP 360 Sealed Types (Preview)
- JEP 359 Records (Second preview)
- JEP 368 Text Blocks (Second Preview)
- JEP 375 Pattern Matching for
Quite a few features, and each and every one represents a significant change to the way you will write Java code in the future.
var Keyword (JDK 10)
var keyword is a new way to declare variables using type inference.
var allows you to declare a variable without specifying it’s type. In Java, this goes against the grain of always having to define the type of a variable. There is some dissent among professionals if
var should be embraced or avoided due to conventional Java’s insistence on type specification.
In the right environment, I think a style guide which includes the use of
var, where appropriate, would allow for significant readability improvements. The
var keyword performs best in small, self-explanatory methods where types can be omitted without losing clarity of what a variable is.
OpenJDK provides principles and Style Guidelines to help you determine when to use
- Reading code is more important than writing code.
- Code should be clear from local reasoning.
- Code readability shouldn’t depend on IDEs.
- Explicit types are a tradeoff.
If your code is aligned with these principles when
var is used, then you can safely use it. If not, you should default back to explicitly typing your variables. Keep in mind that
var is only available for local variables.
JEP 323, Local-Variable Syntax for Lambda Parameters (JDK 11) is an extension of the
var keyword to also be usable in Lambda parameters.
varkeyword in lambda parameters
This simply enables you to use annotations with lambda parameters without requiring you to explicitly define the type of the lambda parameter.
switch Expressions (JDK 14)
switch expression is a new type of
switch which allows you to easily assign the result of the switch computation to a variable.
Exciting New Features & New Plans Including
This alleviates the requirement of having to use
break to exit a switch statement. This also allows for simpler variable assignment, without having to repeat
result = x + y; break;,
result = x * y; break;, etc.
switch expressions make
switches shorter, easier to read, and less error prone.
Pattern Matching for
instanceof (JDK 15, Second Preview)
JEP 375 will give Java’s
instanceof test some much-needed love. Every Java programmer is familiar with the following for checking the type of an object, and then retrieving the value of a field from it.
instanceofcheck, cast, and use of an unknown
The above is verbose and unnecessary. Most of the time you do an
instanceof test, you will be casting the object to that type and performing some type-specific operation on it. Pattern matching will make this more concise and remove the cast entirely.
This becomes especially annoying when you need to test for multiple different types, as in the below example by Gavin Bierman and Brian Goetz.
Using pattern matching and
switch expressions together, the above 17 lines of code, which is 66% boilerplate, could be simplified to just 8 lines.
The ability to use pattern matching with
switch is not planned for the same release as the regular pattern matching, but I couldn’t help but include it. The clarity and brevity of the above code is impressive – especially for Java!
Text Blocks (JDK 15, Second Preview)
Text blocks are a simple quality of life enhancement for writing longer strings in Java. Whether you want to embed a block of code from another language, an HTML snippet, or a JSON payload, text blocks will simplify this:
Text blocks support standard escape sequences and are
Records (JDK 15, Second Preview)
Exciting New Features For Iphone 11
Java Records are immutable classes that can be specified using special, compact syntax. If you are familiar with Lombok, records are similar to classes annotated with
@Value, they are data holders that have the following properties (modified for readability from JEP 359):
- A private final field for each component of the state description;
- A public getter for each field in the state description;
- A public constructor which initializes each field from the corresponding argument;
- Implementations of
hashCodethat say two records are equal if they are of the same type and contain the same state; and
- An implementation of
toStringthat includes the string representation of all the record components, with their names.
Exciting New Features For Iphone 8
records are created using the keyword
record and a straightforward constructor-like syntax called a “state description”. Once the state description is defined, we get all 5 features above for free. It is possible to add validation in the constructor and still not be required to specify the constructor arguments or fields they will be assigned to. Instance methods are also supported. Setters are not provided, the generated fields
y below are
recordusing state description.
Records are by far the most exciting new Java feature of the bunch. Records will change the way we interact with data holder classes on a day-to-day basis. I suspect that Java programmers, once beginning to use
record, will wonder how we ever did this before.
Sealed Types (JDK 15, Preview)
A sealed type is a type with restrictions placed on what can subclass it. The restrictions are defined in the type definition. My first question with this feature was, why? Why not always allow for a type to be subclassed? This was explained best in JEP 360:
In Java, a class hierarchy enables the reuse of code via inheritance: The methods of a superclass can be inherited (and thus reused) by many subclasses. However, the purpose of a class hierarchy is not always to reuse code. Sometimes, its purpose is to model the various possibilities that exist in a domain, such as the kinds of shapes supported by a graphics library or the kinds of loans supported by a financial application. When the class hierarchy is used in this way, restricting the set of subclasses can streamline the modeling.Brian Goetz, JEP 360, 2020
Sealed types allow you to specify not just what the supertype is, but also what the subtypes can be. As mentioned above, sealing allows for a special kind of semantic to be defined in a supertype. Another benefit is that the compiler is subsequently allowed to make assumptions about the subtypes, such as in our pattern matching switch expression example above – if the supertype is a sealed type, then the compiler could allow you to omit the
default clause in a
switch, as in the below example from Data Classes and Sealed Types for Java, by Brian Goetz (2019).
switchexpression example from Data Classes and Sealed Types for Java, by Brian Goetz (2019)
Contribute to OpenJDK
With all the new Java features coming to the JDK in the near future, I am excited to see what the OpenJDK team dreams up next. Clearly, the developer experience is at the forefront of the great minds working on the JDK. As Java developers, we are all part of the ecosystem that sustains and improves Java. If you have an interest in contributing or providing feedback on the preview features, become a contributor to OpenJDK here.