360 Online written---reflection on two questions

Source: Internet
Author: User

1.

  HashMap map=new HashMap ();           Map.put ("name", null);           Map.put ("name", "Cindy");           System.out.println (Map.size ());
Q, what is the size of the output?

Analysis: This obvious test is the implementation of the Map interface class HashMap details. And I ignored it, or I didn't notice it at ordinary times.

The correct answer is 1.

Next, let's get this straight.

I made up such a program.

public static void Main (string[] args) {           HashMap map=new HashMap ();           Map.put ("name", null);           Map.put ("name", "Cindy");           Map.put ("name", "Tom");           System.out.println (Map.get ("name"));           System.out.println (Map.containskey ("name"));           System.out.println (Map.size ());    }

The output is:

Tom
True
1

This means that as long as the key of the element in the HashMap is the same, then the entry of the same key added later will overwrite the previous one. and count a project. The original one was gone, equivalent to a replacement.

Let me check again: I changed the test procedure,

     HashMap map=new HashMap ();           Map.put ("name", null);           Map.put ("name", "Cindy");           Map.put ("name", "Tom");           Map.put (null, "Hello");           System.out.println (Map.get ("name"));           System.out.println (Map.containskey ("name"));           System.out.println (Map.size ());


Null
True
2

Description HashMap allows value to be null, and also allows key to be null.

Final Comprehensive test:

  HashMap map=new HashMap ();           Map.put ("name", null);           Map.put ("name", "Cindy");           Map.put ("name", "Tom");           Map.put (null, "Hello");           System.out.println (Map.get ("name"));           System.out.println (Map.containskey ("name"));           System.out.println (Map.size ());

Output:

Tom
True
2





2. Questions about the number addition of byte types:

byte b1=3;           byte b2=4;           byte B3,b4;           B3=B1+B2;           B4=B3+B2;

Q, B4 equals how much or which sentence will be reported error?

The result is b3=b1+b2; it will be an error.

Why is that? The compiler displays the error message as follows:

Incompatible types: converting from int to Byte can be a loss ... Because, although b1,b2 is of type Byte, it is an integer value, and the sum is calculated as an integer int type

。 When declaring B3 with the INT type declaration, the error will not be made at this time.





























Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

360 Online written---reflection on two questions

Related Article

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.