长和长的区别?

发布于 2024-12-04 04:43:38 字数 990 浏览 2 评论 0原文

我正在经历 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望笑 2024-12-11 04:43:39

Long is a wrapper class around the primitive long. Therefore Long is an object; objects can be null, primitives can not.

See the Long class documentation.

节枝 2024-12-11 04:43:39

long 是原始类型,Longlong 的装箱类型。 java中发布自动装箱功能后,原始long可以自动转换为Long,这是一个对象。

但有时这也会产生问题。例如,下面的代码非常慢:

public static void main(String[] args)
{
    Long sum = 0L;
    for(long i=0; i < Integer.MAX_VAL; i++){
        sum+=i;
    }
}

这是因为由于 sum 声明中的大写 L,程序无意中创建了 2^31 个不必要的对象。

long is primitive type and Long is boxed type of long. After auto-boxing feature is released in java the primitive long can be automatically converted to Long, which is an object.

But sometmimes this creates issue also. For example the below code is terribly slow:

public static void main(String[] args)
{
    Long sum = 0L;
    for(long i=0; i < Integer.MAX_VAL; i++){
        sum+=i;
    }
}

This is because the program unintentionally creating 2^31 objects unnecessarily because of capital L in sum declaration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文