为什么我们不能将包装类对象作为常量?

时间:2015-03-20 18:34:39

标签: java

在Java 7中,对于给定的代码:

final Integer i=9;
final int x=5;

switch(x){
case 1:
case i://compilation error is thrown here 
}

这背后的原因是什么?

4 个答案:

答案 0 :(得分:3)

Integer i = 9;

这个i现在是对Integer对象的引用,它不是在Java中打开的有效类型。

以下是有效的变量类型,您可以启用

  1. Convertible ints - int,byte,short,char
  2. 枚举
  3. 字符串常量 - Java 7中添加的支持
  4. 除了这些有效值之外,您不能只打开任何其他对象

答案 1 :(得分:0)

在许多语言中, switch语句表示对一组编译时常量的持续时间查找

javac编译器将switch语句转换为非常有效的字节码表示,对于该优化,编译时常量是必要的。

答案 2 :(得分:0)

这就是我寻找的原因:

诸如Integer之类的包装器对象不能在case语句中使用,因为它们不是编译时常量(因为装箱和装箱在运行时发生)。因此,您只能使用编译时常量的原语  也必须是最终的。

答案 3 :(得分:0)

1)switch case不支持任何Object类型,更多支持原始变量作为常量和文字。

2)从Java 7开始,它支持字符串。

<?
    $con = mysql_connect('localhost','root','');
    mysql_select_db('social_net', $con) or die('error in(application/view/chatting.php) at line 3');
?>
<!DOCTYPE html>
<html lang="en">
  <head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="facivon.ico">

<title>Facebook like chat</title>
<link href="style.css" rel="stylesheet">
<style>

.chat_box{
    position:fixed;
    right:20px;
    bottom:0px;
    width:250px;
}
.chat_body{
    background:white;
    height:400px;
    overflow-y:scroll;
    border-right: 1px solid rgba(0, 0, 0, 0.28);
    border-left: 1px solid rgba(0, 0, 0, 0.28);
    padding:5px 0px;
}

.chat_head,.msg_head{
    background:#f39c12;
    color:white;
    padding:15px;
    -moz-user-select:none;
    user-select:none;
    font-weight:bold;
    cursor:pointer;
    border-radius:5px 5px 0px 0px;
}

.msg_box{
    position:fixed;
    bottom:-5px;
    display:inline-block;
    border-right: 1px solid rgba(0, 0, 0, 0.28);
    border-left: 1px solid rgba(0, 0, 0, 0.28);
    width:250px;
    background:white;
    border-radius:5px 5px 0px 0px;
}

.msg_head{
    background:#3498db;
    -moz-user-select:none;
    user-select:none;
}

.msg_body{
    background:white;
    height:200px;
    font-size:12px;
    padding:15px;
    overflow:auto;
    overflow-x: hidden;
}
.msg_input{
    width:100%;
    border: 1px solid white;
    border-top:1px solid #DDDDDD;
    -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
    -moz-box-sizing: border-box;    /* Firefox, other Gecko */
    box-sizing: border-box; 
    resize:none;
}

.close{
    float:right;
    margin:-15px;
    border-radius:0px 5px 0px 0px;
    padding:15px;
    -moz-user-select:none;
    user-select:none;
    cursor:pointer;
    background:rgba(71, 62, 48, 0.63) none repeat scroll 0% 0%;
}
.minimize{
    float:right;
    cursor:pointer;
    padding-right:5px;

}

.user{
    position:relative;
    padding:10px 30px;
}
.user:hover{
    background:#f8f8f8;
    cursor:pointer;

}
.user-0{
    content:'';
    position:absolute;
    background:#535a56;
    height:7px;
    width:7px;
    left:10px;
    top:18px;
    border-radius:4px;
}
.user-1{
    content:'';
    position:absolute;
    background:#2ecc71;
    height:7px;
    width:7px;
    left:10px;
    top:18px;
    border-radius:4px;
}

.msg_a{
    position:relative;
    background:#FDE4CE;
    padding:10px;
    min-height:10px;
    margin-bottom:5px;
    margin-right:10px;
    border-radius:5px;
}
.msg_a:before{
    content:"";
    position:absolute;
    width:0px;
    height:0px;
    border: 10px solid;
    border-color: transparent #FDE4CE transparent transparent;
    left:-20px;
    top:7px;
}


.msg_b{
    background:#EEF2E7;
    padding:10px;
    min-height:15px;
    margin-bottom:5px;
    position:relative;
    margin-left:10px;
    border-radius:5px;
    word-wrap: break-word;
}
.msg_b:after{
    content:"";
    position:absolute;
    width:0px;
    height:0px;
    border: 10px solid;
    border-color: transparent transparent transparent #EEF2E7;
    right:-20px;
    top:7px;
}
#finfrndslink
{
    display:block;
    padding:.6em;
    margin:0px auto;
    width:94%;
    margin-top: 10em;
    background: rgb(54, 42, 42) none repeat scroll 0% 0%;
    text-align: center;
    color: white;
    border-radius: 5px;
}
    </style>
       <script src="<?=base_url().'assets/js/jquery-1.10.1.min.js'?>"></script>
    <script>
    $(document).ready(function(){
        $('.msg_box').hide();
        $('.chat_head').click(function(){
            $('.chat_body').slideToggle('slow');
        });
    });
    </script>
  </head>
  <body>
  <?
    $user_name = $this->session->userdata('user');
    $sql1 = "SELECT * FROM users WHERE user_name='$user_name'";
    $query1 = mysql_query($sql1);
    $row1 = mysql_fetch_assoc($query1);
    $u_id = $row1['u_id'];

    $sql = "SELECT * FROM friends T1 JOIN users T2 ON T1.frnd_id=T2.u_id WHERE T1.u_id='$u_id' && T1.status='confirm'";
    $query3 = mysql_query($sql);
    $query = mysql_query($sql);
    $count = mysql_num_rows($query);
  ?>
  <div class="chat_box">
    <div class="chat_head">Chat</div>
    <div style="display: none;" class="chat_body">  
    <?
    if($count!=0)
    {
        $i=1;
        while($row = mysql_fetch_assoc($query))
        {
            ?>
                <div class="user" id="u<?=$i?>" style="text-transform:capitalize"><span class="user-<?=$row['logged_in'];?>"></span><?=$row['f_name'].' '.$row['l_name']?></div>
            <?
            $i++;
        }
    }
    else
    {
        ?>
        <a href='<?=base_url()?>find_friends' id='finfrndslink'>Find Friends</a>
        <?
    }
    ?>
    </div>
    <?
    $i=1;
        while($row1 = mysql_fetch_assoc($query3))
        {
            ?>
                <script>
                $(document).ready(function(){
                    $('#mh<?=$i?>').click(function(){
                        $('#mw<?=$i?>').slideToggle('slow');
                    });

                    $('#cl<?=$i?>').click(function(){
                        $('#mb<?=$i?>').hide();
                    });

                    $('#u<?=$i?>').click(function(){
                        $('#mw<?=$i?>').show();
                        $('#mb<?=$i?>').show();
                    });

                    $('#ti<?=$i?>').keypress(
                    function(e){
                        if (e.keyCode == 13) {
                            e.preventDefault();
                            var msg = $(this).val();
                            $(this).val('');
                            if(msg!='')
                            $('<div class="msg_b">'+msg+'</div>').insertBefore('#msg_push<?=$i?>');
                            $('#msg_body<?=$i?>').scrollTop($('#msg_body<?=$i?>')[0].scrollHeight);
                        }
                    });
                });
                </script>
                <div class="msg_box" id="mb<?=$i?>" style="right:290px">
                    <div class="msg_head" id="mh<?=$i?>">
                    <font style="text-transform:capitalize"><?=$row1['f_name'].' '.$row1['l_name']?></font>
                    <div class="close" id="cl<?=$i?>" title="Close">x</div>
                    </div>
                    <div class="msg_wrap" id="mw<?=$i?>">
                        <div class="msg_body" id="msg_body<?=$i?>">
                            <div class="msg_a">This is from A   </div>
                            <div class="msg_b">This is from B, and its amazingly kool nah... i know it even i liked it :)</div>
                            <div class="msg_a">Wow, Thats great to hear from you man </div> 
                            <div class="msg_push" id="msg_push<?=$i?>"></div>
                        </div>
                    <div class="msg_footer"><textarea class="msg_input" id="ti<?=$i?>" placeholder="write your message here" rows="2"></textarea></div>
                    </div>
                </div>
            <?
            $i++;
        }
    ?>
  </div>
</body>
</html>