“This is the 19th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”.

The vast sea of millions, thank you for this second you see here. I hope my article is helpful to you!

May you keep your love and go to the mountains and seas in the coming days!

Note: yesterday we learned a little about JDK1.7 exception support, so let’s take a look at some of the new features in JDK1.7. Let’s have a look!

😛JDK1.7 about new features

Catch multiple exceptions and throw new ones

There is also a new exception related feature to check out.

  • Before JDK1.7, catch multiple exceptions looked like this:

    Catch (exception type A e){When an exception of type A occurs in A try, the catch is used. }catch(exception type B e){When an exception of type B occurs in a try, use this catch. Code to handle exceptions}Copy the code
  • JDK1.7 and later can do this:

    Try the abnormal code} {write may catch (exception type A | exception type B e) {exception handling code}Copy the code

    But, you can only define it this way if it’s of the same type.

Strings are now available in switch

In previous versions, parentheses in switch statements could only support byte, short, int, char, or their corresponding wrapper classes. You can now add the String class to that.

Let’s take a look!

package com.gt; Public class SwitchTest {public static void main(String[] args) {// Switch new feature String status = "complete"; Switch (status) {case "cancel": system.out.println (" cancel successfully "); break; Case "success": system.out.println (" success"); break; Case "complete": system.out.println (" order complete"); break; Default: system.out.println (" input error, please re-enter "); }}}Copy the code

You can see the output:

The order finishedCopy the code

Type inference when generics are created

The creation of generic instances can be simplified by type inference, removing the generic type following the new part and using only <>.

As long as the compiler can infer type arguments from the context, you can replace generic arguments with a pair of empty Angle brackets <>.

Now check it out:

package com.gt; import java.util.ArrayList; import java.util.List; Public class GenericsTest {// Test generic automatic type inference public static void main(String[] args) { arrayList = new ArrayList<Integer>(); // Since the compiler can infer type parameters from the previous (List), it is not necessary to write generic parameters after the ArrayList. Java SE7 only supports limited type inference: You can use type inference only if the parameterized type of the constructor is explicitly declared in the context, otherwise you can't. List<Integer> arrayList2 = new ArrayList<>(); arrayList.add(11); arrayList.add(22); arrayList2.add(66); arrayList2.add(88); For (Integer item: arrayList) {system.out.println (" current number = "+ item); } for (Integer item2: arrayList2) {system.out.println (" current number = "+ Item2); }}}Copy the code

Now look at the result:

Current number = 11 Current number = 22 Current number = 66 Current number = 88Copy the code

There are small details, of course

  1. Values can be underlined

    int one_million = 1_000_000;
    Copy the code
  1. Binary text support

    int binary = 0b1001_1001;
    Copy the code
  1. Added some tool methods for fetching environment information

    File system.getUserHomedir () // Current user directory File system.getUserdir () // Directory where the Java process is started 5 File system.getJavaiotempdir () // IO temporary folder File System.getJavaHomedir () // Installation directory of the JRECopy the code

🌸 summary

I believe that you are on JDK1.7 new features have some understanding of it, of course, there are a lot of JDK1.7 new features waiting for you to unlock, here only a little bit! Look forward to more in the next chapter! Welcome to the next chapter!

Let’s refuel together, too! I am not just, if there is any missing, wrong place, also welcome you to criticize in the comments of talent leaders! Of course, if this article is sure to help you a little, please kindly and lovely talent leaders to give a thumb-up, favorites, one key three even, thank you very much!

Here, the world is closed for today, good night! Although this article is over, I am still here, never finished. I will try to keep writing articles. The coming days are long, why fear the car yao ma slow!

Thank you all for seeing this! May you live up to your youth and have no regrets!

\