簡體   English   中英

長期在Java中被視為數組中的int

[英]Long being treated as int in an array in Java

我已經將變量定義為long,當我嘗試將其用作數組中的一個變量時,它會繼續拋出一個錯誤,說我的值超出了int范圍。 嗯,不開玩笑,這很長,我把它定義為一個。

以下是我的代碼。 在第二課,LoanOfficer,你會發現第二個申請人比爾蓋茨,其年收入為3,710,000,000,這就是錯誤。

public class Applicant {
    private String name;
    private int creditScore;
    private long annualIncome;
    private int downPayment;
    private boolean status;

    public Applicant(String name, int creditScore, long annualIncome,
            int downPayment) {
        this.name = name;
        this.creditScore = creditScore;
        this.annualIncome = annualIncome;
        this.downPayment = downPayment;
        this.status = false;
    }

    public String getName() {
        return name;
    }

    public int getCreditScore() {
        return creditScore;
    }

    public long getAnnualIncome() {
        return annualIncome;
    }

    public int getDownPayment() {
        return downPayment;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public boolean isStatus() {
        return status;
    }
}

public class LoanOfficer {
    public static void main(String[] args) {
        Applicant[] applicants = {
                new Applicant("MC Hammer", 400, 25000, 5000),
                new Applicant("Bill Gates", 850, 3710000000, 500000),
                new Applicant("MC Hammer", 400, 25000, 5000),
                new Applicant("MC Hammer", 400, 25000, 5000), };
    }
}

對於被視為long的數字,您需要L后綴:

new Applicant("Bill Gates", 850, 3710000000L, 500000)

如果缺少L后綴,編譯器會將文字視為int

您需要通過附加L指定3710000000作為長文字:

new Applicant("Bill Gates", 850, 3710000000L, 500000),

來自JLS

如果整數文字后綴為ASCII字母L或l(ell),則整數文字的長度為long; 否則它是int類型

將3710000000更改為3710000000L。 問題是沒有L ,Java會把它當作int

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM