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