Java 7 (codename Dolphin) was a major update to the Java programming language announced on July 7, 2011[1]. Java 7 reached General Availability on 28 July 2011[2]. The update brought a few small additions to the language under Project Coin, a number of new concurrency utilities, RIA enhancements, and various other additions.
Contents
Project Coin
- Main article: Project Coin
Project Coin brought a small set of changes to the language itself which include:
- binary literals
- strings in switch statements
- Underscores in numeric literals
- try-with-resources statements
- The ability to catch multiple exception types
- Generic instances type inference
- Better compiler warnings and errors with varargs methods
Binary literals
Java SE 7 added the ability to express the values of integral types such as byte, short, int, and long using the binary number system. Binary literals can be expressed by prefixing the number with 0b or 0B. For example:
//an octet value
byte b = (byte)0b00010101;
//a 16-bit integer value
shot s = (short)0b1011010001000001;
//a 32-bit integer value
int i = 0b010001010100111010001101011101;
int i2 = 0b010;
//a 64-bit integer value
long l = 0b0101011111000100101001010111101100011110010101011110010000110010L; // note the 'L' suffix
Strings in switch statements
Java SE 7 added the ability to use String object in the expression of a switch case. The switch statement compares the String object form the expression to each of the String objects in the cases using the String.equals() method. The comparison is case sensitive.
public String getDayAbbr(String day)
{
switch (day)
{
case "Monday": return "Mon";
case "Tuesday": return "Tue";
case "Wednesday": return "Wed";
case "Thursday": return "Thur";
case "Friday": return "Fri";
case "Saturday": return "Sat";
case "Sunday": return "Sun";
default:
return "";
}
}
Underscores in numeric literals
Java 7 added the ability to delimit numeric literals using the underscore characters (_) anywhere between digits. This feature was added to make it easier for programmers to read long numbers:
long l = 111_111_111_111_111_111L;
float pi = 3.1_415F;
byte b = 0b0011_1011;