我知道,如果你将盒装原始Integer与常量进行比较,例如:
Integer a = 4;
if (a < 5)
Run Code Online (Sandbox Code Playgroud)
a 将自动取消装箱,比较将起作用.
但是,当您比较两个盒装Integers并希望比较相等或小于/大于?时会发生什么?
Integer a = 4;
Integer b = 5;
if (a == b)
Run Code Online (Sandbox Code Playgroud)
以上代码是否会导致检查它们是否是同一个对象,还是会在这种情况下自动取消装箱?
关于什么:
Integer a = 4;
Integer b = 5;
if (a < b)
Run Code Online (Sandbox Code Playgroud)
?
I have no idea why these lines of code return different values:
System.out.println(Integer.valueOf("127")==Integer.valueOf("127"));
System.out.println(Integer.valueOf("128")==Integer.valueOf("128"));
System.out.println(Integer.parseInt("128")==Integer.valueOf("128"));
Run Code Online (Sandbox Code Playgroud)
The output is:
true
false
true
Run Code Online (Sandbox Code Playgroud)
Why does the first one return true and the second one return false? Is there something different that I don't know between 127 and 128? (Of course I know that 127 < 128.)
Also, why does the third one return true?
I have read the answer of this question, but I still didn't get …
当有人使用值> 127时,有人能告诉我为什么assertSame()会失败吗?
import static org.junit.Assert.*;
...
@Test
public void StationTest1() {
..
assertSame(4, 4); // OK
assertSame(10, 10); // OK
assertSame(100, 100); // OK
assertSame(127, 127); // OK
assertSame(128, 128); // raises an junit.framework.AssertionFailedError!
assertSame(((int) 128),((int) 128)); // also junit.framework.AssertionFailedError!
}
Run Code Online (Sandbox Code Playgroud)
我正在使用JUnit 4.8.1.
以下是有什么区别的:
Integer in = (Integer)y;
和
Integer in = new Integer(y);
我想将int类型转换为Integer类型,反之亦然.这是我的代码:
public class CompareToDemo {
public static void main(String[] args) {
// Integer x=5;
int y=25;
System.out.println(y+" this is int variable");
Integer in = (Integer)y;
//Integer in = new Integer(y);
if(in instanceof Integer){
System.out.println(in +" this is Integer variable");
}
}
}
Run Code Online (Sandbox Code Playgroud) 编辑:好的,好的,我误读了.我不是将int与Integer进行比较.正好指出.
我的SCJP书说:
当==用于将基元与包装器进行比较时,包装器将被解包,并且比较将是原始的.
所以你认为这段代码会打印出来true:
Integer i1 = 1; //if this were int it'd be correct and behave as the book says.
Integer i2 = new Integer(1);
System.out.println(i1 == i2);
Run Code Online (Sandbox Code Playgroud)
但它打印出来false.
另外,根据我的书,这应该打印true:
Integer i1 = 1000; //it does print `true` with i1 = 1000, but not i1 = 1, and one of the answers explained why.
Integer i2 = 1000;
System.out.println(i1 != i2);
Run Code Online (Sandbox Code Playgroud)
不.是的false.
是什么赋予了?
这是一段代码
public class Classifier {
public static void main(String[] args)
{
Integer x = -127;//this uses bipush
Integer y = 127;//this use bipush
Integer z= -129;//this use sipush
Integer p=32767;//maximum range of short still sipush
Integer a = 128; // use sipush
Integer b = 129786;// invokes virtual method to get Integer class
}
}
Run Code Online (Sandbox Code Playgroud)
这是部分字节代码
stack=1, locals=7, args_size=1
0: bipush -127
2: invokestatic #16 // Method java/lang/Integer.valueO
f:(I)Ljava/lang/Integer;
5: astore_1
6: bipush 127
8: invokestatic #16 // Method java/lang/Integer.valueO …Run Code Online (Sandbox Code Playgroud) 在阅读了很多问题后,我问自己是否可以解决将字符串转换为通用数字而不使用硬编码方法的困境。
例如:我从方法中获取类型为 Class With Number.isAssignableFrom 的参数,或者通过其他方式检查这是否是数字类。但我也从用户那里得到了输入。作为一个字符串。
问题是:我现在可以以某种方式将此字符串转换为请求的数字对象,而无需为每种情况构建 if 语句吗?
示例代码,正常不起作用:
Object ret = null;
for(int i=0;i<method.getParameterTypes().length; i++ ) {
Class<?> param = method.getParameterTypes()[i];
String argString = getUserInput(_in, "Argument ("+param.getSimpleName()+"): ");
if( Number.isAssignableFrom(param) )
ret = ((Class<NumberChild>)param).valueOf(argString);
/// OR
ret = param.cast( Double.valueOf(argString) )
}
Run Code Online (Sandbox Code Playgroud)
甚至提出了这个问题:是否有可能将每个基元转换为与上述方式类似的东西?
注意:这里的方法应该完全关注非硬编码的解决方案。我当前运行的代码使用对每个案例进行硬编码的方法。但在这些情况下,更通用的解决方案会更有趣。
编辑: 我很抱歉造成误解,但我的意思是采用硬编码方法,这种方法可以测试每种可能的情况,例如:
if( integer ); do ...
if( double ); do ...
if( long ); do ...
Run Code Online (Sandbox Code Playgroud)
但这正是我想要解决的问题。澄清一下:这只是一个挑战。我的生活或代码并不依赖于它,我只是想知道它是否可能!
我想知道使用Long.valueOf(0);或的区别0L。他们是一样的吗?
我知道这两个都是Long类型,因此在Java 8中它们的内存消耗为64位长。
那么,考虑到内存消耗和时间复杂性,如何更好地初始化变量呢?
Long a = Long.valueOf(0);
要么
Long b = 0L;
这个问题出现在我用 Java 编写的实习职位面试问题之一中。请注意,布尔函数isSame实际上将 2 个参数声明为Integerclass - not int,所以我认为a和b是对象,对吗?
public class ForLoop{
public static boolean isSame(Integer a, Integer b) {
return a == b;
}
public static void main(String []args){
int i = 0;
for (int j=0; j<500; ++j) {
if (isSame(i,j)) {
System.out.println("Same i = "+i);
System.out.println("Same j = "+j);
++i;
continue;
} else {
System.out.println("Different i = "+i);
System.out.println("Different j = "+j);
++i;
break;
}
}
System.out.println("Final i = " …Run Code Online (Sandbox Code Playgroud) interface New<T> {
boolean func(T n, T v);
}
Run Code Online (Sandbox Code Playgroud)
class MyFunc<T>{
T val;
MyFunc(T val){
this.val = val;
}
void Set(T val){
this.val = val;
}
T Get(){
return val;
}
boolean isEqual(MyFunc<T> o){
if(val == o.val) return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
public class Main {
static <T> boolean check(New<T> n , T a, T b){
return n.func(a,b);
}
public static void main(String args[]){
int a = 321;
MyFunc<Integer> f1 = new MyFunc<Integer>(a);
MyFunc<Integer> f2 = new MyFunc<Integer>(a);
boolean result; …Run Code Online (Sandbox Code Playgroud) 当数值比较运算符用于比较 Java 中的 2 个整数对象时,我试图了解以下代码的行为。
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
System.out.println(i1 == i2);
System.out.println(i1 > i2);
System.out.println(i1 >= i2);
Run Code Online (Sandbox Code Playgroud)
上面代码的输出是:
false
false
true
Run Code Online (Sandbox Code Playgroud)
我理解在第一种情况下发生的事情(对象实例的比较是这样的,这就是它给出错误的原因)。但是为什么第二个和第三个场景不同,它究竟是如何工作的呢?
我有个问题.我不知道这有什么意义,但我真的需要一个答案
当我使用"=="作为
Integer i1 =10;
Integer i2 = 10;
if(i1 == i2) {System.out.println("same object")}
Run Code Online (Sandbox Code Playgroud)
output =同一个对象
但是,如果我使用"=="
String obj1 = new String("xyz");
String obj2 = new String("xyz");
if(obj1 == obj2)
System.out.println("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");
Run Code Online (Sandbox Code Playgroud)
输出= FALSE
我知道"=="运算符会查找对象的内存位置.但是在第一个例子中发生了什么 i1和i2不是不同的对象?
我需要写一些方法来完成一件事,我不知道该怎么做.我很感激任何帮助.
假设我们有一个void m1(int z) {}
这样调用的方法
:
int x; ... m1(x);
基本上我需要确保方法m1正好使用我需要的'x'字段.不是'x'字段的值,而是指向字段'x',而不是指向此字段或任何其他类的其他字段.
更具体地说,我打算写一个if语句意思:
if (z is pointing to the field x) return true;
我不知道如何实现指向条件.有这种评估的标准方法吗?
非常感谢你的答复,你们所有人.
@durbnpoisn,@ Boris the Spider
代码本身尚不存在,但我正在尝试详细说明这些方法.我不是一个经验丰富的程序员,这就是为什么它可能看起来很奇怪.对不起.
但是,如果它有助于我的任务是制作几个"切换"按钮,当按下这些按钮时,从代码的不同部分切换掉某些字段和方法,就好像它们从未使用过一样.
例如,代码行看起来像supCrossVal = x^realOpt/setPower(mine)所有开关关闭,它应该像supCrossVal = x^realOptSWITCH1打开时那样执行.
问题是初始代码非常大(因此相关调用的数量)和开关数量大约为10-15,并且一些swithes可以关闭相同的方法和字段.因此,在我看来,为每个字段或方法调用添加if语句(检查开关是打开还是关闭)将使代码难以阅读.并且每个交换机的重载方法看起来过于简单,代码也乱丢垃圾.所以我在考虑一种基于指向思想的简单方法.
java ×13
autoboxing ×3
integer ×3
comparison ×2
casting ×1
class ×1
compare ×1
for-loop ×1
generics ×1
hardcoded ×1
junit ×1
junit4 ×1
jvm ×1
lambda ×1
object ×1
reflection ×1
types ×1
unboxing ×1
unit-testing ×1
wrapper ×1