相关疑难解决方法(0)

整数类对象

我有一个代码片段:

public class Test{
    public static void main(String args[]){
        Integer a = 100;
        Integer b = 100;
        Integer c = 5000;
        Integer d = 5000;

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        if(a == b)
            System.out.println("a & b Both are Equal");
        else
            System.out.println("a & b are Not Equal");
        if(c == d)
            System.out.println("c & d Both are Equal");
        else
            System.out.println("c & d are Not Equal");
    }
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么输出是这样的?的Output是:
a & b Both are equal
c & d are not equal
我使用jdk1.7

java

5
推荐指数
1
解决办法
195
查看次数

java中奇怪的等式行为

看看下面的代码:

Long minima =          -9223372036854775808L;
        Long anotherminima =   -9223372036854775808L;
        System.out.println(minima==anotherminima); //evaluates to false
        System.out.println(Long.MIN_VALUE); 
        Long another= 1L;
        Long one = 1L;
        System.out.println(another == one); //evaluates to true
Run Code Online (Sandbox Code Playgroud)

我无法理解这种行为..?我希望第一个eval也是真的..这就是我所期待的......

java

5
推荐指数
1
解决办法
114
查看次数

从getter返回之前检查null是否是不好的做法?

我想防止getter返回null值,例如下面的示例。这是不好的做法吗?

例子1

public Integer getMinutes() {
   if (minutes == null)
       minutes = 0;
   return minutes;
}
Run Code Online (Sandbox Code Playgroud)

例子2

public List getTasks() {
   if (tasks == null)
       tasks = new ArrayList();
   return tasks;
}
Run Code Online (Sandbox Code Playgroud)

java oop null

5
推荐指数
1
解决办法
1313
查看次数

Java Integer Wrapper类相关

public class TestMe{
 public static void main(String[] args)  {
   Integer i = 128;
   Integer j = 128;

   Integer k = 12;
   Integer l = 12;

   System.out.println(i == j);  
   System.out.println(k == l);
  }
}
Run Code Online (Sandbox Code Playgroud)

我得到输出:false true

为什么第一个是假的而第二个是真的?

java

4
推荐指数
2
解决办法
56
查看次数

带有==和!=的Strange Wrapper Classes行为

可能重复:
奇怪的Java拳击

最近,当我阅读有关包装类时,我遇到了这个奇怪的案例:

Integer i1 = 1000;
Integer i2 = 1000;

if(i1 != i2) System.out.println("different objects");

if(i1 == i2) System.out.println("same object");
Run Code Online (Sandbox Code Playgroud)

哪个印刷品:

different objects
Run Code Online (Sandbox Code Playgroud)

Integer i1 = 10;
Integer i2 = 10;

if(i1 != i2) System.out.println("different objects");

if(i1 == i2) System.out.println("same object");
Run Code Online (Sandbox Code Playgroud)

哪个印刷品:

same object
Run Code Online (Sandbox Code Playgroud)

这个案子有合理的解释吗?

谢谢

java wrapper

3
推荐指数
1
解决办法
772
查看次数

Java整数自动自动装箱

我在Java(JDK 1.7)中偶然发现了这个:

    Integer a = 100;
    Integer b = 100;
    Integer c = 1000;
    Integer d = 1000;

    System.out.println(a == b); //true
    System.out.println(c == d); //false
    System.out.println(new Integer(100) == new Integer(100)); //false
    System.out.println(new Integer(1000) == new Integer(1000)); //false
Run Code Online (Sandbox Code Playgroud)

输出为:true false false false

为什么== b评估为真?这是什么原因?这类似于String内化吗?

java autoboxing integer

3
推荐指数
1
解决办法
2116
查看次数

java:class Integer == operator奇怪的行为

public class IntegerVsInt {

    public static void main(String args[])
    {
        int a = 1;
        int b = 1;
        int c = a + b;

        System.out.println(c);
        System.out.println(a == b);

        Integer x = 1;
        Integer y = 1;
        Integer z = x + y;

        System.out.println(z);
        System.out.println(x == y);
    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我将比较两个int和两个integer类型的对象.

比较两个int时

a == b
Run Code Online (Sandbox Code Playgroud)

我希望他们的价值观能够进行比较.

但是当你比较两个Integer时

x == y
Run Code Online (Sandbox Code Playgroud)

我希望比较两个对象的地址,然后返回false.

我在两种情况下都是真的吗?为什么会这样?

java eclipse

3
推荐指数
1
解决办法
218
查看次数

关于整数比较的澄清?

class Demo{
public static void main(String[] args) {  
     Integer i = Integer.valueOf(127);  
     Integer j = Integer.valueOf(127);        

     System.out.println(i==j);  

     Integer k = Integer.valueOf(128);  
     Integer l = Integer.valueOf(128);        

     System.out.println(k==l);  
  }  
}
Run Code Online (Sandbox Code Playgroud)

第一个print语句打印为true,而第二个打印语句打印为false.为什么?请详细解释.

java autoboxing integer value-of

2
推荐指数
1
解决办法
148
查看次数

为什么我得到奇怪的输出?

该程序找到素数因子并将其打印出来:factor(order)factor(order)

import java.util.Scanner;
import java.util.ArrayList;

public class Factorise2
{
    public static ArrayList<Integer> get_prime_factors(int number)
    {
//Get the absolute value so that the algorithm works for negative numbers
        int absoluteNumber = Math.abs(number);
        ArrayList<Integer> primefactors = new ArrayList<Integer>();
        ArrayList<Integer> newprimes = new ArrayList<Integer>();
        int b;
        int c = (int)Math.sqrt(absoluteNumber);
//Get the square root so that we can break earlier if it's prime

        for (int j = 2; j <= absoluteNumber;)
        {
//Test for divisibility by j, and add to the list of …
Run Code Online (Sandbox Code Playgroud)

java

1
推荐指数
1
解决办法
153
查看次数

为什么First if if打印Equal和Second Not Equal?

可能重复:
奇怪的Java拳击

public class example {
    public static void main(String[] args) {

        Integer a=127,b=127;
        if(a==b)
            System.out.println("Equal");
        else
            System.out.println("Not Equal");
        Integer c=128,d=128;
        if(c==d)
            System.out.println("Equal");
        else
            System.out.println("Not Equal");
    }

}
Run Code Online (Sandbox Code Playgroud)

输出:

Equal
Not Equal
Run Code Online (Sandbox Code Playgroud)

java

1
推荐指数
1
解决办法
90
查看次数

标签 统计

java ×10

autoboxing ×2

integer ×2

eclipse ×1

null ×1

oop ×1

value-of ×1

wrapper ×1