In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the syntax of the new features of JDK7, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
JDK7 has a few updates to the Java syntax, focusing on improvements in ease of use and convenience.
1. Binary literal quantity
At the beginning of JDK7, you can finally represent integers (byte,short,int and long) in binary. The advantage of using binary literals is that the code can be understood more easily. The syntax is very simple, as long as the binary value is preceded by 0b or 0B
Java code
Byte nByte = (byte) 0b0001; short nShort = (short) 0B0010; int nInt = 0b0011; long nLong = 0b0100L
two。 The literal amount of numbers can be underlined
For some larger numbers, we always have no aspect to define them, and often lack or increase the number of digits. JDK7 provides us with a solution where underscores can appear in digital literals.
Java code
Int a = 1000000000000; long b = 0xffffffffl.byte c = 0b0001_1000
Note: you can only put underscores between numbers. The following methods are incorrect.
1. The beginning or end of a number
two。 Before and after the decimal point
3. The suffix of'F' or'f'
4. You can only use the position of a number
Java code
Int err1 = _ 11, err2, 11; float err3=3._4,err4=3_.4; long err5=0x888_f
The 3.switch statement can use a string.
This function has been called for for a long time, and it has finally come out.
Java code
Private static void switchString (String str) {switch (str) {case "one": System.err.println ("1"); break; case "two": System.out.println ("2"); break Default: System.out.println ("err");}}
4. The creation of generic instances can be simplified by type inference
In the future, when you create a generic instance, you don't need to specify the type, just use it, and the compiler will automatically match it for you.
Java code
/ / for example, Map myMap = new HashMap (); / / can be simplified to Map myMap = new HashMap ()
5. Pass non-materialized parameters (Non-Reifiable Formal Parameters) in variable parameter methods to improve compilation warnings and errors
Some parameter types, such as ArrayList and List, are non-reifiable. During the compilation phase, the compiler erases the type information.
Heap pollution means that one variable is pointed to another variable of a different type. For example
Java code
List l = new ArrayList (); List ls = 1; / / unchecked warning l.add (0, new Integer (42)); / / another unchecked warning String s = ls.get (0); / / ClassCastException is thrown
Back to our topic, in jdk7, when you define the following function
Java code
Public static void addToList (List listArg, T... Elements) {for (T x: elements) {listArg.add (x);}}
You'll get a warning.
Warning: [varargs] Possible heap pollution from parameterized vararg type
Before jdk7, when you called a variable parameter method with non-materialized parameters, you had to guarantee yourself that "heap pollution" would not occur. There is a problem. If the caller is not familiar with the method, he cannot judge at all. JDK7 improved this by issuing warnings long after the method was defined
There are three ways to eliminate warnings
1. Add annotation @ SafeVarargs
two。 Add annotation @ SuppressWarnings ({"unchecked", "varargs"})
3. Use the compiler parameter-Xlint:varargs
6.try-with-resources statement
Jdk7 provides try-with-resources, which automatically shuts down related resources (as long as the resource implements the AutoCloseable interface, jdk7 implements this interface for most resource objects)
Java code
Static String readFirstLineFromFile (String path) throws IOException {try (BufferedReader br = new BufferedReader (new FileReader (path) {return br.readLine ();}}
Try statement blocks can also handle multiple resources at the same time, like ordinary try statements can be catch exceptions, there are finally statement blocks
Java code
Try (java.util.zip.ZipFile zf = new java.util.zip.ZipFile (zipFileName); java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter (outputFilePath, charset)) {} catch (…) {} finally {}
7.Catch improved type detection with multiple Exception,rethrow exception
A lot of times, we catch multiple exceptions and do the same thing, such as logging, wrapping them as new exceptions, and then rethrow. At this point, the code is less elegant, such as
Java code
Catch (IOException ex) {logger.log (ex); throw ex; catch (SQLException ex) {logger.log (ex); throw ex;}
Jdk7 allows multiple exceptions to be caught
Java code
Catch (IOException | SQLException ex) {logger.log (ex); throw ex;}
Note that the exception parameter after catch is final and cannot be copied again.
More inclusive type checking in Rethrow Exception
When you re-throw multiple exceptions, you no longer need to define the exception type in detail, and the compiler already knows which exception you are throwing. You only need to declare the exception that needs to be thrown when the method is defined.
Java code
Public void call () throws ReflectiveOperationException, IOException {try {callWithReflection (arg);} catch (final Exception e) {logger.trace ("Exception in reflection", e); throw e;}} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.