整理一些java基础面试题

1、请说出下面程序的输出。

<code class="language-Java hljs cs has-numbering" style="font-family: 'Source Code Pro', monospace; line-height: 1.45; display: block; padding: 0px; color: inherit; box-sizing: border-box;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">class StringEqualTest {

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>(String[] args) {
        String s1 = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Programming"</span>;
        String s2 = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> String(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Programming"</span>);
        String s3 = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Program"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ming"</span>;
        System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(s1 == s2);
        System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(s1 == s3);
        System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(s1 == s1.intern());
    }
}</code><code class="language-Java hljs cs has-numbering" style="font-family: 'Source Code Pro', monospace; line-height: 1.45; display: block; padding: 0px; color: inherit; box-sizing: border-box;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">
</code><code class="language-Java hljs cs has-numbering" style="font-family: 'Source Code Pro', monospace; line-height: 1.45; display: block; padding: 0px; color: inherit; box-sizing: border-box;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">运行结果:false</code><code class="language-Java hljs cs has-numbering" style="font-family: 'Source Code Pro', monospace; line-height: 1.45; display: block; padding: 0px; color: inherit; box-sizing: border-box;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">          true</code><code class="language-Java hljs cs has-numbering" style="font-family: 'Source Code Pro', monospace; line-height: 1.45; display: block; padding: 0px; color: inherit; box-sizing: border-box;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">          true</code><code class="language-Java hljs cs has-numbering" style="font-family: 'Source Code Pro', monospace; line-height: 1.45; display: block; padding: 0px; color: inherit; box-sizing: border-box;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="color: rgb(51, 51, 51); font-family: 'microsoft yahei'; font-size: 14px; line-height: 17.5px; background-color: rgba(128, 128, 128, 0.0470588);">补充:String对象的intern方法会得到字符串对象在常量池中对应的版本的引用(如果常量池中有一个字符串与String对象的equals结果是true),如果常量池中没有对应的字符串,则该字符串将被添加到常量池中,然后返回常量池中字符串的引用。</span></code><code class="language-Java hljs cs has-numbering" style="font-family: 'Source Code Pro', monospace; line-height: 1.45; display: block; padding: 0px; color: inherit; box-sizing: border-box;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><strong style="color: inherit;font-size:undefined; line-height: 1.45; background-color: transparent;">
</strong></code><code class="language-Java hljs cs has-numbering" style="font-family: 'Source Code Pro', monospace; line-height: 1.45; display: block; padding: 0px; color: inherit; box-sizing: border-box;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><strong style="color: inherit;font-size:undefined; line-height: 1.45; background-color: transparent;">
</strong></code><code class="language-Java hljs cs has-numbering" style="font-family: 'Source Code Pro', monospace; line-height: 1.45; display: block; padding: 0px; color: inherit; box-sizing: border-box;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><strong style="color: inherit;font-size:undefined; line-height: 1.45; background-color: transparent;">2、<span style="font-family: 'microsoft yahei'; line-height: 17.5px;">为什么不能根据返回类型来区分重载?</span></strong></code><code class="language-Java hljs cs has-numbering" style="font-family: 'Source Code Pro', monospace; line-height: 1.45; display: block; padding: 0px; color: inherit; box-sizing: border-box;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="color: rgb(51, 51, 51); font-family: 'microsoft yahei'; font-size: 14px; line-height: 17.5px; background-color: rgba(128, 128, 128, 0.0470588);">
</span></code><code class="language-Java hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="font-family:microsoft yahei;"><span style="line-height: 17.5px;">答案:<span style="color: rgb(51, 51, 51); font-family: 'microsoft yahei'; font-size: 14px; line-height: 26px;">重载发生在一个类中,同名的方法如果有不同的参数列表(参数类型不同、参数个数不同或者二者都不同)则视为重载。</span></span></span></code><code class="language-Java hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="font-family:microsoft yahei;"><span style="line-height: 17.5px;"><span style="color: rgb(51, 51, 51); font-family: 'microsoft yahei'; font-size: 14px; line-height: 26px;"></span></span></span></code><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei'; font-size: 14px; line-height: 20px;">我们现在来讨论一下用返回值来区分重载方法的可行性。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei'; font-size: 14px; line-height: 20px;">比如下面两个方法:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei'; font-size: 14px; line-height: 20px;">void fun();</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei'; font-size: 14px; line-height: 20px;">int fun();</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei'; font-size: 14px; line-height: 20px;">long fun();</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei'; font-size: 14px; line-height: 20px;"> </p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei'; font-size: 14px; line-height: 20px;">如果编译器可以根据语境来判断执行哪一个方法的话,那么当  执行  int i = fun(); 的时候,我们很容易判断出来,应该调用第二个重载方法。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Microsoft Yahei'; font-size: 14px; line-height: 20px;">但是,当我们 执行单独调用fun(),而不把返回值赋值给任何变量的时候呢?就像:  直接执行 fun(); 这个时候,我们就不知道调用哪个方法了,所以根据返回值来区分重载方法是行不通的。</p>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值