Java 为什么添加长变量会导致连接?
Java在执行加法时如何处理长变量 错误版本1:Java 为什么添加长变量会导致连接?,java,long-integer,string-concatenation,addition,Java,Long Integer,String Concatenation,Addition,Java在执行加法时如何处理长变量 错误版本1: Vector speeds = ... //whatever, speeds.size() returns 2 long estimated = 1l; long time = speeds.size() + estimated; // time = 21; string concatenation?? 错误版本2: Vector speeds = ... //whatever, speeds.size() returns 2 long est
Vector speeds = ... //whatever, speeds.size() returns 2
long estimated = 1l;
long time = speeds.size() + estimated; // time = 21; string concatenation??
错误版本2:
Vector speeds = ... //whatever, speeds.size() returns 2
long estimated = 1l;
long time = estimated + speeds.size(); // time = 12; string concatenation??
正确版本:
Vector speeds = ... //whatever, speeds.size() returns 2
long estimated = 1l;
long size = speeds.size();
long time = size + estimated; // time = 3; correct
我不明白,为什么Java要连接它们
有谁能帮我一下,为什么两个基本变量连在一起
你好,格尔达,我怀疑你没有看到你认为你看到的东西。Java不能做到这一点 请试着提供一个例子来说明这一点。下面是一个简短但完整的程序,它演示了正确的行为,但使用了错误的代码,即反例
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Vector speeds = new Vector();
speeds.add("x");
speeds.add("y");
long estimated = 1l;
long time = speeds.size() + estimated;
System.out.println(time); // Prints out 3
}
}
我怀疑你没有看到你认为你看到的东西。Java不能做到这一点 请试着提供一个例子来说明这一点。下面是一个简短但完整的程序,它演示了正确的行为,但使用了错误的代码,即反例
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Vector speeds = new Vector();
speeds.add("x");
speeds.add("y");
long estimated = 1l;
long time = speeds.size() + estimated;
System.out.println(time); // Prints out 3
}
}
我猜你实际上在做这样的事情:
System.out.println("" + size + estimated);
此表达式从左到右求值:
"" + size <--- string concatenation, so if size is 3, will produce "3"
"3" + estimated <--- string concatenation, so if estimated is 2, will produce "32"
"" + (expression) <-- string concatenation - need to evaluate expression first
(3 + 2) <-- 5
Hence:
"" + 5 <-- string concatenation - will produce "5"
再次从左到右计算:
"" + size <--- string concatenation, so if size is 3, will produce "3"
"3" + estimated <--- string concatenation, so if estimated is 2, will produce "32"
"" + (expression) <-- string concatenation - need to evaluate expression first
(3 + 2) <-- 5
Hence:
"" + 5 <-- string concatenation - will produce "5"
我猜你实际上在做这样的事情:
System.out.println("" + size + estimated);
此表达式从左到右求值:
"" + size <--- string concatenation, so if size is 3, will produce "3"
"3" + estimated <--- string concatenation, so if estimated is 2, will produce "32"
"" + (expression) <-- string concatenation - need to evaluate expression first
(3 + 2) <-- 5
Hence:
"" + 5 <-- string concatenation - will produce "5"
再次从左到右计算:
"" + size <--- string concatenation, so if size is 3, will produce "3"
"3" + estimated <--- string concatenation, so if estimated is 2, will produce "32"
"" + (expression) <-- string concatenation - need to evaluate expression first
(3 + 2) <-- 5
Hence:
"" + 5 <-- string concatenation - will produce "5"
提示:不要用“l”表示长,因为在某些字体中它与“1”无法区分。使用“L”代替。你已经接受了答案,但这并不能完全解释问题的原因-你能详细说明吗?提示:不要用“L”表示长,因为在某些字体中它与“1”无法区分。用“L”代替。你已经接受了一个答案,但这并不能完全解释问题的原因-你能详细说明吗?那么这个答案的正确之处是什么?作者做错了什么?那么这个答案的正确之处是什么?作者做错了什么?