6: Java syntax sugar

Source: Internet
Author: User


Virtual machines do not support these syntaxes,

Map<int,String> map = new HashMap<int,String>();map.put(1,"No.1");map.put(2,"No.2");System.out.println(map.get(1));System.out.println(map.get(2));

Map map = new HashMap();map.put(1,"No.1");map.put(2,"No.2");System.out.println((String)map.get(1));System.out.println((String)map.get(2));

import java.util.List;public class FanxingTest{public void method(List<String> list){System.out.println("List String");}public void method(List<Integer> list){System.out.println("List Int");}}

When I use

This is because of generics.

Modify the above Code as follows:

import java.util.List;public class FanxingTest{public int method(List<String> list){System.out.println("List String");return 1;}public boolean method(List<Integer> list){System.out.println("List Int");return true;}}

It is found that the compilation can be passed at this time (the language is not correlated with each other, and the two are of different types. They cannot be converted to each other, and there is no such situation as a non-zero integer in the language ). Adding two different types of return values makes the method reload successful. Why?

We know that the return value of a method is not involved in the selection of the method to be overloaded. It seems redundant to add the return value to the method to be overloaded. This is indeed redundant for the selection of overload methods, but the problem we need to solve now is that the above Code can be compiled, so that the two overload methods can reasonably coexist after adding different return values to the two overload methods, they can coexist in one

The syntax sugar, such as the automatic disassembly box and variable length parameters, are also restored to the native syntax structure during the compilation phase. Therefore, only the corresponding native type exists in the file, I will not describe them here.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.