长和长的区别?
我正在经历 Android 示例记事本应用程序的第二个练习,我有一个关于用于定义 mRowId 的 Long 和 long 之间的区别的问题。
练习在这里: http://developer.android.com/resources/ tutorials/notepad/notepad-ex2.html
下面是我遇到问题的代码片段:
public class NoteEdit extends Activity {
private Long mRowId;
private EditText mTitleText;
private EditText mBodyText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.note_edit);
setTitle(R.string.edit_note);
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
Button confirmButton = (Button) findViewById(R.id.confirm);
mRowId = null;
当我使用 long
声明 mRowId 时,当我尝试设置 mRowId 时出现错误为 null,错误是“类型不匹配”。但如果我使用Long
,错误就会消失。为什么 long
不起作用?
I am going through the 2nd exercise of the android example the notepad app, I have this a question about the difference between Long and long that was used to define the mRowId.
The exercise is here: http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html
And below is the code piece that I am having problem with:
public class NoteEdit extends Activity {
private Long mRowId;
private EditText mTitleText;
private EditText mBodyText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.note_edit);
setTitle(R.string.edit_note);
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
Button confirmButton = (Button) findViewById(R.id.confirm);
mRowId = null;
When I declared mRowId with long
, I got an error when I tried to set mRowId to null, the error is "type mismatch". But if I use Long
, the error goes away. Why doesn't long
work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Long
是围绕 原语长
。因此Long
是一个对象;对象可以为 null,而基元则不能。请参阅
Long
类文档。Long
is a wrapper class around the primitivelong
. ThereforeLong
is an object; objects can benull
, primitives can not.See the
Long
class documentation.long
是原始类型,Long
是long
的装箱类型。 java中发布自动装箱功能后,原始long
可以自动转换为Long
,这是一个对象。但有时这也会产生问题。例如,下面的代码非常慢:
这是因为由于
sum
声明中的大写 L,程序无意中创建了 2^31 个不必要的对象。long
is primitive type andLong
is boxed type oflong
. After auto-boxing feature is released in java the primitivelong
can be automatically converted toLong
, which is an object.But sometmimes this creates issue also. For example the below code is terribly slow:
This is because the program unintentionally creating 2^31 objects unnecessarily because of capital L in
sum
declaration.