在整数位之间允许下划线的理由是什么?

时间:2017-03-02 19:18:09

标签: java language-lawyer literals

今天我在JLS的3.10.1 (integer literals)部分偶然发现了这个看似奇怪的陈述:

  

允许下划线作为表示整数的数字之间的分隔符。

请注意以下内容完全有效(example):

int x = 1_2_3_4_5____6;

下划线只能在数字之间显示,前导/尾随下划线无效。

这是什么理由?是否有一些其他语言的保留?有些风格在当时很常见吗?允许这似乎是一件很奇怪的事情。

1 个答案:

答案 0 :(得分:3)

这背后的想法是能够显示一个易于阅读的大数字。

示例: <?php header("Access-Control-Allow-Origin: *"); ?> <!DOCTYPE html> <html> <head> <title>IO Chat</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js"></script> <script src="jquery.js"></script> <script> $.ajax({ url: "sampleclickinggame.esy.es", type: method, // This is the important part xhrFields: { withCredentials: true }, // This is the important part data: data, success: function (response) { // handle the response }, error: function (xhr, status) { // handle errors } }); </script> <style> body{ margin-top: 30px; } #messageArea{ display: none; } </style> </head> <body> <div class="container"> <div id="userFormArea" class="row"> <div class="col-md-12"> <form id="userForm"> <div class="form-group"> <label>Enter UserName</label> <input class="form-control" id="username" /> <br /> <input type="submit" class="btn btn-primary" value="Login" /> </div> </form> </div> </div> <div id="messageArea" class="row"> <div class="col-md-4"> <div class="well"> <h3>Online Users</h3> <ul class="list-group" id="users"></ul> </div> </div> <div class="col-md-8"> <div class="chat" id="chat"></div> <form id="messageForm"> <div class="form-group"> <label>Enter Message</label> <textarea class="form-control" id="message"></textarea> <br /> <input type="submit" class="btn btn-primary" value="Send Message" /> </div> </form> </div> </div> </div> <script> $(function(){ var socket = io.connect(); var $messageForm = $('#messageForm'); var $message = $('#message'); var $chat = $('#chat'); var $messageArea = $('#messageArea'); var $userFormArea = $('#userFormArea'); var $userForm = $('#userForm'); var $users = $('#users'); var $username = $('#username'); $messageForm.submit(function(e){ e.preventDefault(); socket.emit('send message', $message.val()); $message.val(''); }); socket.on('new message', function(data){ $chat.append('<div class="well"><strong>'+data.user+'</strong>: '+data.msg+'</div>'); }); $userForm.submit(function(e){ e.preventDefault(); socket.emit('new user', $username.val(), function(data){ if(data){ $userFormArea.hide(); $messageArea.show(); } }); $username.val(''); }); socket.on('get users', function(data){ var html = ''; for(var i = 0;i < data.length;i++){ html += '<li class="list-group-item">'+data[i]+'</li>'; } $users.html(html); }); }); </script> </body> </html>

还有其他用例,但这是我认为最常用的。

有关详细信息,请参阅the documentation