括号中的整数给出编译错误

时间:2015-11-17 08:33:36

标签: java compiler-errors int

我正在初始化两个整数ab

a编译正常,但b存在错误。

public class Main_1 {
    public static void main(String[] args) {
        int a = -2147483648; //Working fine
        int b = -(2147483648); //Compilation error: The literal 2147483648 of type int is out of range 
    }
}

为什么会这样?

6 个答案:

答案 0 :(得分:50)

原因是int数据类型的有效值在[-2147483648, 2147483647]范围内。

当您将2147483648括在括号内时,它将成为一个将被评估为int的表达式。但是,2147483648太大而无法容纳int(太大一个)。

问题不会发生在-2147483648上,因为它是有效的int值。

JLS的相关部分:

  • 添加括号会创建“括号表达式”(section 15.8.5
  • 默认情况下,2147483648等整数文字被视为intsection 3.10.1
      

    如果整数文字的后缀为ASCII字母longL(ell),则其类型为l;否则它是int类型(§4.2.1)。

答案 1 :(得分:11)

int值从2147483647变为-(2147483648)。所以int是OutOfRange,因为括号内的值被评估为Integer.MAX_VALUE //Which is equals to 2147483647 。您可以放入括号中的最大值是

$(function() {
     var chart = new Highcharts.Chart({
         chart: {
             "renderTo": "container",
             "zoomType": "x",
             "panning": true,
             "panKey": "shift"
         },
         plotOptions: {
             "series": {
                 "states": {
                     "hover": {
                         "enabled": false
                     }
                 }
             }
         },
         series: [{
             "name": "First Timeline",
             "type": "line",
             "data": [                                 
                 [1447608100000, 4.256],
                 [1447608070000, 4.471],
                 [1447608045000, 4.916],
                 [1447608042000, 2.946],
                 [1447608029000, 3.517],
                 [1447608003000, 5.801],
                 [1447607954000, 4.162],
                 [1447607924000, 2.415],
                 [1447607908000, 3.314],
                 [1447607898000, 2.765],
                 [1447607888000, 3.581],
                 [1447607861000, 7.016],
                 [1447607740000, 3.485],
                 [1447607719000, 4.264],
                 [1447607700000, 2.855],
                 [1447607694000, 3.142],
                 [1447607676000, 4.92],
                 [1447607613000, 2.899],
                 [1447607603000, 2.729],
                 [1447607594000, 4.054],
                 [1447607538000, 2.61],
                 [1447607526000, 2.43],
                 [1447607515000, 5.984],
                 [1447607467000, 2.529],
                 [1447607445000, 3.398],
                 [1447607391000, 4.9],
                 [1447607365000, 4.829],
                 [1447607310000, 2.935],
                 [1447607287000, 4.811],
                 [1447607242000, 4.897],
                 [1447607219000, 3.354],
                 [1447607157000, 5.929],
                 [1447607156000, 4.444],
                 [1447607128000, 6.542],
                 [1447607125000, 8.854],
                 [1447607121000, 5.693],
                 [1447607118000, 3.906],
                 [1447607092000, 2.843],
                 [1447607085000, 2.615],
                 [1447607076000, 2.651],
                 [1447607060000, 5.015],
                 [1447607057000, 2.441],
                 [1447607045000, 4.417],
                 [1447607015000, 5.198],
                 [1447607012000, 7.056],
                 [1447607006000, 4.014],
                 [1447606988000, 3.363],
                 [1447606965000, 2.329],
                 [1447606925000, 4.209],
                 [1447606922000, 4.095],
                 [1447606892000, 3.021],
                 [1447606879000, 4.54],
                 [1447606815000, 3.73],
                 [1447606783000, 7.097],
                 [1447606781000, 4.336],
                 [1447606732000, 2.786],
                 [1447606714000, 2.774],
                 [1447606675000, 6.027],
                 [1447606626000, 3.349],
                 [1447606576000, 3.147],
                 [1447606565000, 4.873],
                 [1447606518000, 4.064],
                 [1447606500000, 2.458],
                 [1447606484000, 2.601],
                 [1447606480000, 3.839],
                 [1447606429000, 5.348],
                 [1447606390000, 5.665],
                 [1447606287000, 7.904],
                 [1447606079000, 4.976],
                 [1447606055000, 5.577],
                 [1447606016000, 4.474],
                 [1447605931000, 5.43],
                 [1447605893000, 2.979],
                 [1447605889000, 6.101],
                 [1447605758000, 2.608],
                 [1447605757000, 5.77],
                 [1447605732000, 4.056],
                 [1447605701000, 5.834],
                 [1447605668000, 3.143],
                 [1447605665000, 2.924],
                 [1447605647000, 2.941],
                 [1447605631000, 3.544],
                 [1447605589000, 3.78],
                 [1447605566000, 7.102],
                 [1447605564000, 4.404],
                 [1447605563000, 5.95],
                 [1447605536000, 3.158],
                 [1447605499000, 2.618],
                 [1447605470000, 4.327],
                 [1447605456000, 4.384],
                 [1447605434000, 6.955],
                 [1447605307000, 3.632],
                 [1447605289000, 3.25],
                 [1447605276000, 3.711],
                 [1447605266000, 4.151],
                 [1447605224000, 4.985],
                 [1447605186000, 3.37],
                 [1447605163000, 2.553],
                 [1447605136000, 6.708],
                 [1447605042000, 6.076],
                 [1447604944000, 2.934],
                 [1447604923000, 6.721],
                 [1447604660000, 5.824],
                 [1447604608000, 4.777],
                 [1447604572000, 2.702],
                 [1447604560000, 3.34],
                 [1447604512000, 8.953],
                 [1447604509000, 7.409],
                 [1447604399000, 6.201],
                 [1447604327000, 2.912],
                 [1447604317000, 3.708],
                 [1447604280000, 4.575],
                 [1447604251000, 6.961],
                 [1447604248000, 3.878],
                 [1447604237000, 3.699],
                 [1447604232000, 4.066],
                 [1447604194000, 2.265],
                 [1447604182000, 4.943],
                 [1447604135000, 2.764],
                 [1447604132000, 7.494],
                 [1447603986000, 6.042],
                 [1447603983000, 3.071],
                 [1447603948000, 5.029],
                 [1447603887000, 7.517],
                 [1447603762000, 6.409],
                 [1447603758000, 4.375],
                 [1447603729000, 6.743],
                 [1447603584000, 2.78],
                 [1447603561000, 5.06],
                 [1447603536000, 5.109],
                 [1447603499000, 3.178],
                 [1447603433000, 6.729],
                 [1447603368000, 4.771],
                 [1447603278000, 4.775],
                 [1447603256000, 2.522],
                 [1447603242000, 6.729],
                 [1447603065000, 5.433],
                 [1447603016000, 3.842],
                 [1447602983000, 7.156],
                 [1447602780000, 7.057],
                 [1447602609000, 5.594],
                 [1447602566000, 6.967],
                 [1447602420000, 4.171],
                 [1447602345000, 5.094],
                 [1447602311000, 3.869],
                 [1447602293000, 4.59],
                 [1447602290000, 2.944],
                 [1447602271000, 6.146],
                 [1447602174000, 2.749],
                 [1447602161000, 2.713],
                 [1447602116000, 7.074],
                 [1447602065000, 3.175],
                 [1447602053000, 3.326],
                 [1447601987000, 3.973],
                 [1447601984000, 5.376],
                 [1447601933000, 3.106],
                 [1447601899000, 6.304],
                 [1447601822000, 2.749],
                 [1447601773000, 5.92],
                 [1447601650000, 7.701],
                 [1447601547000, 2.893],
                 [1447601532000, 3.036],
                 [1447601523000, 6.992],
                 [1447601392000, 7.213],
                 [1447601061000, 6.555],
                 [1447600809000, 5.283],
                 [1447600771000, 4.829],
                 [1447600748000, 2.75],
                 [1447600719000, 5.896],
                 [1447600632000, 4.905],
                 [1447600607000, 2.821],
                 [1447600583000, 2.98],
                 [1447600582000, 4.549],
                 [1447600568000, 4.821],
                 [1447600527000, 3.244],
                 [1447600521000, 6.498],
                 [1447600404000, 4.902],
                 [1447600380000, 5.754],
                 [1447600287000, 4.958],
                 [1447600250000, 3.106],
                 [1447600235000, 2.655],
                 [1447600231000, 3.266],
                 [1447600194000, 5.529],
                 [1447600152000, 4.377],
                 [1447600086000, 3.799],
                 [1447600083000, 3.085],
                 [1447600072000, 5.024],
                 [1447600036000, 4.372],
                 [1447600024000, 2.653],
                 [1447600024000, 3.836],
                 [1447600015000, 3.173],
                 [1447600007000, 5.187],
                 [1447599920000, 3.063],
                 [1447599914000, 3.986],
                 [1447599898000, 4.572],
                 [1447599817000, 4.282],
                 [1447599807000, 4.841],
                 [1447599802000, 4.515],
                 [1447599764000, 2.617],
                 [1447599749000, 2.932],
                 [1447599724000, 7.049],
                 [1447599720000, 4.841],
                 [1447599695000, 6.434],
                 [1447599692000, 3.248],
                 [1447599687000, 3.738],
                 [1447599680000, 4.174],
                 [1447599625000, 4.691],
                 [1447599575000, 3.657],
                 [1447599561000, 3.93],
                 [1447599506000, 7.645],
                 [1447599243000, 7.372],
                 [1447599242000, 5.109],
                 [1447599212000, 3.965],
                 [1447599188000, 3.358],
                 [1447599177000, 5.133],
                 [1447599177000, 2.737],
                 [1447599168000, 3.591],
                 [1447599104000, 3.627],
                 [1447599094000, 4.185],
                 [1447599079000, 4.75],
                 [1447599040000, 3.388],
                 [1447599029000, 2.608],
                 [1447599020000, 2.721],
                 [1447598998000, 3.548],
                 [1447598983000, 4.117],
                 [1447598967000, 3.311],
                 [1447598953000, 4.011],
                 [1447598911000, 3.208],
                 [1447598886000, 4.261],
                 [1447598878000, 5.554],
                 [1447598845000, 4.951],
                 [1447598783000, 4.79],
                 [1447598728000, 4.677],
                 [1447598665000, 3.013],
                 [1447598648000, 3.199],
                 [1447598630000, 5.953],
                 [1447598556000, 2.935],
                 [1447598549000, 3.082],
                 [1447598521000, 4.991],
                 [1447598493000, 5.14]
             ],
             "tooltip": {
                 "valueSuffix": " secondes"
             }
         }],
         title: {
             "text": "Temps de r\u00e9ponse de RISe"
         },
         xAxis: [{
             "type": "datetime",
             "dateTimeLabelFormats": {
                 "hours": "%H:%M"
             }
         }],
         yAxis: {
             "labels": {
                 "format": "{value}s"
             },
             "title": {
                 "text": null
             },
             "min": 0,
             "max": 9
         },
     });
 });

答案 2 :(得分:7)

编译错误很明显:您正在使用超出范围的int文字。如果您真的想这样做,可以使用long字面值:

int b = (int) -(2147483648L);

double文字:

int b = (int) -(2147483648.0);

答案 3 :(得分:4)

int的最大值为mysqld -u root -p,int的最小值为2147483647。但是当你将-2147483648放入大括号时,它最初会被视为2147483648,并且它对于愤怒无效。

答案 4 :(得分:3)

可视化的一个好方法是将(int) -(2147483648)视为:

(int) -1 * (2147483648)

当编译器评估它时,它说,我必须首先将括号中的数字转换为整数,然后将其乘以负数1.然后继续对数字进行范围检查并发现它大于整数(2147483648)可以容纳的值,这是编译错误。

答案 5 :(得分:1)

function PaymentOnLoad() { setTimeout(attachEventToGrid, 2500); if (crmForm.ObjectId != null) { var PaymentAmount = Xrm.Page.getAttribute("new_accountpaymentamount").getValue(); var OSAmount = CalcOutstandingPaymentAmount(crmForm.ObjectId); Xrm.Page.getAttribute("new_remainingamount").setValue(parseFloat(eval(rounddec(OSAmount)))); if ((PaymentAmount != null) && (OSAmount - PaymentAmount != 0)) { Xrm.Page.ui.controls.get("new_paymentamount").setDisabled(true); } } } function rounddec(value) { return Math.round(value * 100) / 100; } function attachEventToGrid() { // Attach a refresh event to the PaymentAllocations grid var targetgrid = document.getElementById("PaymentAllocations"); if (targetgrid) { if (targetgrid.control.add_onRefresh != undefined) { targetgrid.control.add_onRefresh(ReLoad); } else { targetgrid.attachEvent("onrefresh", ReLoad); } } else { setTimeout(attachEventToGrid, 2500); } } function PaymentAllocationOnLoad() { OnPaymentTypeSelection(); if (Xrm.Page.getAttribute("new_payment").getValue()[0] != null) { var OSPaymentAmount = CalcOutstandingPaymentAmount(Xrm.Page.getAttribute("new_payment").getValue()[0].id); Xrm.Page.getAttribute("new_paymentremainingamount").setValue(parseFloat(eval(OSPaymentAmount))); if (Xrm.Page.getAttribute("new_invoice").getValue() != null) { var OSInvoiceAmount = CalcOutstandingInvoiceAmount(Xrm.Page.getAttribute("new_invoice").getValue()[0].id); Xrm.Page.getAttribute("new_invoiceremainingamount").setValue(parseFloat(eval(OSInvoiceAmount))); Xrm.Page.ui.controls.get("new_paymentamount").setDisabled(true); } } } function CalcOutstandingPaymentAmount(paymentid) { _oService = new FetchUtil(_sOrgName, _sServerUrl); var sFetchPayment = "<fetch mapping='logical'>" + "<entity name='new_payment'>" + "<attribute name='new_paymentamount' />" + "<attribute name='new_accountpaymentamount' />" + "<filter type='and'>" + "<condition attribute = 'new_paymentid' operator='eq' value='" + paymentid + "'/>" + "</filter>" + "</entity>" + "</fetch>"; var fetchResultPayment = _oService.Fetch(sFetchPayment, null); var sFetch = "<fetch mapping='logical'>" + "<entity name='new_paymentinvoiceallocation'>" + "<attribute name='new_allocatedamount' />" + "<filter type='and'>" + "<condition attribute = 'new_payment' operator='eq' value='" + paymentid + "'/>" + "</filter>" + "</entity>" + "</fetch>"; var fetchApplication = _oService.Fetch(sFetch, null); var TotalAmount = 0; if (fetchResultPayment != null) { TotalAmount = fetchResultPayment.results[0].attributes.new_accountpaymentamount.value; } if ((fetchResultPayment != null) && (fetchApplication != null)) { for (var i = 0; i < fetchApplication.results.length; i++) { TotalAmount -= fetchApplication.results[i].attributes.new_allocatedamount.value; } } return TotalAmount; } 数据类型为int

最小值为32-bit signed two's complement integer

最大值为- 2,147,483,648.(-2^31)