- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
下面的 (Java) 代码是否合法?
class Test {
Object foo() {return "";}
boolean bar() {return foo() == true;}
}
它不会针对 JDK 6 进行编译,但在 7+ 上似乎没问题。规范有变化吗?是否修复了错误?我一直在 http://bugs.eclipse.org/bugs/show_bug.cgi?id=416950 讨论并且可以在这一个上走任何一条路。
最佳答案
关于引用相等性的 JLS 在 Java 6 和 7 之间没有改变:
Chapter 15.21.3: Reference Equality Operators == and != :
If the operands of an equality operator are both of either reference type or the null type, then the operation is object equality.
It is a compile-time error if it is impossible to convert the type of either operand to the type of the other by a casting conversion (§5.5). The run-time values of the two operands would necessarily be unequal.
但是我注意到 Chapter 5.5: Casting Conversion 上有一些变化.将 boolean 值转换为对象似乎被归类为 Java 7 上的装箱约定:
An expression of a primitive type may undergo casting conversion to a reference type without error, by boxing conversion.
⊡表示装箱转换
因此,由于原语 true
可以转换为 Object
,因此您的相等表达式在 Java 7 上可以归类为引用相等,并且不会产生编译器错误
关于java - 什么时候用 '==' 运算符比较对象和基元是合法的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18731497/
(注意:这个问题的动机是试图提出预处理器黑客来生成无操作分配来回答另一个问题: Macro that accept new object ...请记住这一点!) 这是一个人为的类: class foo
所以我尝试用java编写一个井字棋游戏。大部分已经完成,但是,如果有人选择了已被占用的空间,我无法返回无效的移动。 这是我试图弄清楚的代码。我认为由于空间是由数字 0 表示的(我的教授告诉我们这一点)
我想使用 Java 代码获取某些 Google 搜索引擎查询(在整个网络上)的估计结果数。 我每天只需要做很少的查询,所以一开始 Google Web Search API ,虽然已被弃用,但似乎已经
C++ Primer, 5e 的练习 4.20 询问表达式 iter++->empty(); 是否成立是合法的。假设iter是 vector::iterator . 这个表达式是合法的。我用gcc编译
C++ Primer, 5e 的练习 4.20 询问表达式 iter++->empty(); 是否成立是合法的。假设iter是 vector::iterator . 这个表达式是合法的。我用gcc编译
在 C++ 中,动态内存(取消)分配(malloc-free/new-delete)显然可以重复获得相同的内存范围,该内存范围被释放并按顺序再次分配。在多线程 C++ 中,这可能发生在多个线程中。 显
AesCryptoServiceProvider.LegalKeySizes 字段显示允许的位大小。 但是我不明白的是,如果这些都是真的,我如何才能成功利用 2048 位 key 长度(256 字节)
我有一个结构: typedef struct _n { int type; union { char *s; int i; }; } n; 当我
这个问题在这里已经有了答案: 关闭10年前. Possible Duplicate: int var = 1; void main() { int i = i; } 以下代码在g++和Visual C
根据 C++03 标准 (5.3.4/7): When the value of the expression in a direct-new-declarator is zero, the allo
我正在尝试创建一个生成器,使用 scalacheck 1.6.6 和规范 1.7 (scala 2.8.1) 生成(非零长度)合法的 unicode 字符串。 我希望我可以创建像这样的生成器: obj
这确实是一个关于 C++ 的问题,而不是 OpenGL。我正在关注 this OpenGL 教程(我刚开始),作者使用 C++(不是 C)。我的问题是 glVertexAttribPointer ta
由于 C++ 别名规则,您不能随意将 (一个 int*) 指向 char 数组,这似乎达成了一些共识。 从另一个问题——Generic char[] based storage and avoidin
我正在寻找一个通过 HTTP 接口(interface)发送文本消息的 SMS 网关。 但是,有些短信网关不允许设置发件人ID,允许的要么要求用户证明发件人ID是自己的手机号码,要么手动检查每个发件人
我们注意到您的应用要求用户注册个人信息才能访问非基于帐户的功能。在允许访问与用户无关的应用内容和功能之前,应用不能要求用户注册。 我们在注册时采用电子邮件 ID,因为所有数据都代表电子邮件 ID 在后
我一直在阅读 Bloch 和 Gafter 的 Java Puzzlers,并读到了第 10 个谜题 (Tweedledee)。这个谜题的本质是 provide declarations for th
这个问题确实符合标题:我很想知道造成这种差异的技术原因是什么,以及基本原理? std::shared_ptr sharedToVoid; // legal; std::unique_ptr uniqu
我试图做的是将数据框数据附加到现有的合法 excel 文件中。我使用了 openpyxl 中的 load_workbook() 函数,但它系统地返回了一个错误。这是一些在我的机器上崩溃的代码: fro
App 多次被 Apple 拒绝 “经过进一步审查,我们仍然发现您的应用使用了后台定位服务,但并未按照 iOS 人机界面指南的要求在位置模式警报中阐明其使用目的。” 在“NSLocationAlway
据我所知,像这样在 C++ 中实例化一个整数是合法的: int x = int(5); 作为一名 Java 程序员,我会假设这行代码调用传递“5”作为参数的整数的构造函数。我读到 int 不是一个类,
我是一名优秀的程序员,十分优秀!