Android "1"=="1" 到底是true还是false
高洛峰
高洛峰 2017-04-17 17:36:46
0
7
748

问题:

有个代码我判断是否等于字符串的“1”,因是新手不小心用了==,结果发生了很奇妙的问题。

Util u = new Util();
boolean result = u.getId() == "1";
Toast.makeText(this, String.valueOf(result), Toast.LENGTH_SHORT).show();
  1. 结果在手机上运行会提示true(部分华为Android版本5.0以下手机会提示false)

  2. 在IDE中调试结果是false

看截图,调度过程中代码后面显示的result:true,可是在计算窗口中显示为false。见鬼了,求解。

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(7)
黄舟

There is a heap memory (heap) in Java memory. Simple memory means that everything new comes out is in the heap memory.
There is also an area of ​​Java memory called the constant pool, where string constants, such as string constants, are stored, such as "1".
"1" must be in the constant pool. The key is u.getId The value of () is in the constant pool or in the heap memory. This depends on the source code of Util's construction method. In the screenshot, "1" is returned directly. This means that during debugging, the virtual machine has allocated two pieces of memory in the constant pool, and Android directly shares the same piece of memory. Discrimination depends on the mood of the compiler. Try not to use == in the future, use equals

黄舟

JAVA’s problem depends on the virtual machine. JAVA的问题,看虚拟机的吧。

  • == 比较的内存地址

  • 对于字符串的比较用equal

    • == compared memory address
  • 🎜For string comparison, use equal🎜🎜 🎜
左手右手慢动作

Characters should be compared using equals, and basic types should be compared using "==". Because the basic types are stored in memory, "==" is the storage address in the memory for comparison. If it is a composite type such as characters, even if it looks at first glance, the storage address may be different, so if you use "==" it is possible It may be true, it may be false

伊谢尔伦
  1. == has a higher priority than =;

  2. == compares addresses, equals compares values;

  3. == is to compare whether the addresses of two strings are the same, that is, whether they are references to the same string;

  4. Strings are stored in the constant pool. Only one copy of a literal is stored, so the addresses will be equal, which means "1"=="1" returns true.

巴扎黑

==The comparison is the address. When running directly, the virtual home determines that "1" and "1" are the same constant object, that is, the same address, so it returns true. When debugging, the mechanism may be different (maybe because the debugging mode is used), causing the virtual machine to think that the two '1' strings are not the same object, and return false. So, just ignore this. .

刘奇

According to the arithmetic operator parsing order = before ==
So the result = u.getId() is parsed first, and the assignment operation result is Boolean true
So it is true=="1" You should research the topic of whether true is equal to "1". Theoretically it's fasle.
"1" is a string, not int 1, and int 1 can indeed be expressed as a Boolean type true

洪涛

If the strings you want to compare are all numbers, you can use Integer.parse to convert them.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!