为什么 java.util.Map 值可以增加原始数组而不是单个整数
Why java.util.Map value can increment primitive array but not single Integer
这里有三个例子:
示例 1:
int j[] = new int[]{2,2,2,2};
HashMap<Integer, int[]> m = new HashMap<>();
m.put(5,j);
m.get(5)[2]++; // NO COMPAILATION ERROR
System.out.println(Arrays.toString(m.get(5))); // op: [2, 2, 3, 2]
示例 2:
HashMap<Integer, Integer> n = new HashMap<>();
n.put(6, 2);
n.get(6)++; // COMPILATION ERROR - Variable Expected
示例 3:
HashMap n = new HashMap();
n.put(6, 2);
n.get(6)++; // COMPILATION ERROR - Variable Expected
原始 int 数组上的值可以通过仅使用增量运算符来改变,而具有实际整数的 Map (Ex 2,3) 拒绝此操作并出现编译错误,这是为什么?
注意:我预计示例 2 会增加地图中的值。正如以下行为:
Integer x = 2;
x++;
根据JLS §15.1,
When an expression in a program is evaluated (executed), the result denotes one of three things:
A variable (in C, this would be called an lvalue)
A value
Nothing (the expression is said to be void)
并且§15.14.2说:
15.14.2. Postfix Increment Operator ++
A postfix expression followed by a ++
operator is a postfix increment expression.
The result of the postfix expression must be a variable of a type that is convertible to a numeric type, or a compile-time error occurs.
重要的是++
之前的表达式必须是“变量”,而不是“值”或“无”。
方法调用表达式m.get(6)
表示一个“值”,而m.get(5)[2]
表示一个“变量”。这在 §15.10.3:
中指定
The result of an array access expression is a variable of type T
, namely the variable within the array selected by the value of the index expression.
另一方面,在§15.12中,指定方法调用表达式的地方,并没有说方法调用表达式是变量。
如果你考虑一下,方法调用 有 是值而不是变量 想象一下,如果方法调用产生变量并且你有一个像这样的方法:
public static int foo() {
return 1;
}
而你做到了:
foo()++;
你会增加什么?常量1
? 1
现在真的意味着 2
吗?这没有多大意义,是吗?
这里有三个例子:
示例 1:
int j[] = new int[]{2,2,2,2};
HashMap<Integer, int[]> m = new HashMap<>();
m.put(5,j);
m.get(5)[2]++; // NO COMPAILATION ERROR
System.out.println(Arrays.toString(m.get(5))); // op: [2, 2, 3, 2]
示例 2:
HashMap<Integer, Integer> n = new HashMap<>();
n.put(6, 2);
n.get(6)++; // COMPILATION ERROR - Variable Expected
示例 3:
HashMap n = new HashMap();
n.put(6, 2);
n.get(6)++; // COMPILATION ERROR - Variable Expected
原始 int 数组上的值可以通过仅使用增量运算符来改变,而具有实际整数的 Map (Ex 2,3) 拒绝此操作并出现编译错误,这是为什么?
注意:我预计示例 2 会增加地图中的值。正如以下行为:
Integer x = 2;
x++;
根据JLS §15.1,
When an expression in a program is evaluated (executed), the result denotes one of three things:
A variable (in C, this would be called an lvalue)
A value
Nothing (the expression is said to be void)
并且§15.14.2说:
15.14.2. Postfix Increment Operator
++
A postfix expression followed by a
++
operator is a postfix increment expression.The result of the postfix expression must be a variable of a type that is convertible to a numeric type, or a compile-time error occurs.
重要的是++
之前的表达式必须是“变量”,而不是“值”或“无”。
方法调用表达式m.get(6)
表示一个“值”,而m.get(5)[2]
表示一个“变量”。这在 §15.10.3:
The result of an array access expression is a variable of type
T
, namely the variable within the array selected by the value of the index expression.
另一方面,在§15.12中,指定方法调用表达式的地方,并没有说方法调用表达式是变量。
如果你考虑一下,方法调用 有 是值而不是变量 想象一下,如果方法调用产生变量并且你有一个像这样的方法:
public static int foo() {
return 1;
}
而你做到了:
foo()++;
你会增加什么?常量1
? 1
现在真的意味着 2
吗?这没有多大意义,是吗?