首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果条件未正确评估

如果条件未正确评估
EN

Stack Overflow用户
提问于 2015-05-24 20:17:07
回答 2查看 110关注 0票数 0

下面的Java测试代码发生了一些奇怪的事情。这是用Spring编写的。

代码语言:javascript
运行
复制
ResourceType resourceType1 = new ResourceType(RESOURCE_TYPE_NAME);
resourceType1 = resourceTypeService.addResourceType(resourceType1);

ResourceType resourceType2 = new ResourceType(RESOURCE_TYPE_NAME_2);
resourceType2 = resourceTypeService.addResourceType(resourceType2);

List<ResourceType> all = resourceTypeService.getAll();
List<ResourceType> myObjects = new ArrayList<>();
for (ResourceType resourceType : all) {
    if(resourceType.getId() == resourceType1.getId()
            || resourceType.getId() == resourceType2.getId()){
        myObjects.add(resourceType);
    }
}

assertTrue(myObjects.get(0).getId() == resourceType1.getId());

当我运行它时,它会在IndexOutOfBounds调用上返回.get(0)

这很奇怪,因为resourceType1resourceType2all列表中有is。

这是调试代码的结果:

插入后的资源类型1:

代码语言:javascript
运行
复制
resourceType1 = {ResourceType@5986} 
 id = 179

插入后的资源类型1:

代码语言:javascript
运行
复制
resourceType2 = {ResourceType@5987} 
 id = 180

全在getAll()之后

代码语言:javascript
运行
复制
all = {ArrayList@5988}  size = 37
 ..........
 35 = {ResourceType@5986} 
  id = 179
 36 = {ResourceType@5987} 
  id = 180

这是调试数据。

为什么没有满足if条件?

更新#1

错误堆栈跟踪:

代码语言:javascript
运行
复制
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at com.test.ihbs.service.ResourceTypeServiceTest.test_getAll(xxx.java:107)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-24 20:55:18

正如我在备注中所说的,使用==比较器不能进行很长时间的比较。它只适用于(-128,127)之间的值,并且只有当它们不使用new关键字创建时才有效。它之所以有效,是因为JVM缓存了这些值,因此比较引用返回true。

看看这里这里

1)

代码语言:javascript
运行
复制
Integer i = 100;
Integer p = 100;
if (i == p)  System.out.println("i and p are the same.");
if(i.equals(p))  System.out.println("i and p contain the same value.");

产出如下:

代码语言:javascript
运行
复制
i and p are the same.
i and p contain the same value.

2)

代码语言:javascript
运行
复制
Integer i = 200;
Integer p = 200;
if (i == p)  System.out.println("i and p are the same.");
if(i.equals(p))  System.out.println("i and p contain the same value.");

产出如下:

代码语言:javascript
运行
复制
i and p contain the same value.

3)

代码语言:javascript
运行
复制
Integer i = new Integer (100);
Integer p = new Integer(100);
if(i==p) System.out.println(“i and p are the same object”);
if(i.equals(p)) System.out.println(“ i and p contain the same value”);

在这种情况下,输出仅为:

代码语言:javascript
运行
复制
i and p contain the same value
票数 3
EN

Stack Overflow用户

发布于 2016-07-28 06:35:41

"==“总是比较值的内存位置或对象引用。方法总是比较values.but =,也间接使用"==“运算符来比较值。整数使用整数缓存存储从-128到+ 127之间的值,如果使用==操作符检查-128到127之间的任何值,则返回true。对于这些值以外的值,则返回false。

参考链接获取更多信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30427899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档