public class Main {
public void test(Object o) {
System.out.println("Object");
}
public void test(String s) {
System.out.println("String");
}
public static void main(String[] args) {
Main that = new Main();
that.test(null);
}
}

请写出运行输出

String

这个考的是Java的method overload resolution。根据Java语言规范的规定:
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error.
这里的that.test(null)可以匹配上两个重载版本的test()方法,但是String版本比Object版本更具体,所以匹配上String版。
 如果此处要强制选择Object版,则可以写:that.test((Object) null);
 

1. 以下程序运行时是否会抛出异常, 以及程序输出.

 public class a {
static String s0, s1;
public static void main(String args[]) {
s0 = s0 + s1;
System.out.println(s0);
}
}
这个考察的是静态变量的默认初始化,以及String的连接(+)。
 
静态变量会在类加载过程中的linking阶段得到默认初始化。引用类型的静态变量会被默认初始化为null。
 
然后是String对象的连接。根据Java语言规范:
15.18.1. String Concatenation Operator +
If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.
The result of string concatenation is a reference to a String object that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string.
The String object is newly created (§12.5) unless the expression is a constant expression (§15.28).
然后对null的情况规范也做了规定:
The operators on references to objects are:
 
  • ...
  • The string concatenation operator + (§15.18.1), which, when given a String operand and a reference, will convert the reference to a String by invoking the toString method of the referenced object (using "null" if either the reference or the result of toString is a null reference), and then will produce a newly created String that is the concatenation of the two strings
(抱歉之前我写这个回答的时候看漏了这规范的这部分。对null的规定居然跟string concat运算符的规定没写在一起…)
 
如果考虑Sun JDK 1.0 - 1.4.2的实现的话,s0 = s0 + s1;是一个语法糖,会被解糖为:
s0 = new StringBuffer().append(s0).append(s1).toString();
考虑Oracle/Sun JDK 5、6、7、8的实现的话,则会被解糖为:
s0 = new StringBuilder().append(s0).append(s1).toString();
根据规范,此处调用的StringBuffer / StringBuilder.append(String)方法会对null做特殊处理,把它当作"null"字符串。相关实现:jdk8u/jdk8u/jdk: 3dc438e0c8e1 src/share/classes/java/lang/AbstractStringBuilder.java
所以最后答案是"nullnull"。
(注意:Oracle/Sun JDK 9改变了String +的实现,解除语法糖后不再是对StringBuilder的调用了。可以参考JEP 280: Indify String Concatenation。但用户代码能观察到的行为不变,结果仍然是"nullnull"。)

2.写出 `WindowAdapter.windowClosing, ActionListener.actionPerformed` 方法的参数类型.

3.下列程序的输出:

 // ...
public static void main(String args[]) {
String a = "abc";
String b = "ab" + "c";
System.out.println(a == b);
// ...
}
 
public class Test { public static void main(String args[]) { String a = "abc"; String b = "ab" + "c"; System.out.println(a == b); } }
 
这个考察的是Java的编译时常量、编译时常量折叠,以及String interning的知识。
"abc"、"ab"、"c"在Java里都是String类型的编译时常量。当+运算符的左右两个操作数都是编译时常量时,这个+表达式也会被认为是编译时常量表达式。
 
 
再看Java语言规范:
15.28. Constant Expressions
A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
Constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.
以及:
A string literal is a reference to an instance of class String (§4.3.1, §4.3.3).
Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.
String.intern的文档:
public String intern()
Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
All literal strings and string-valued constant expressions are interned. String literals are defined in section 3.10.5 of the The Java™ Language Specification.
 
Returns:a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
 
换句话说,上述代码等价于:
// ... public static void main(String args[]) { String a = "abc"; String b = "abc"; System.out.println(a == b); // ... }
结果就显而易见了。内容相同的String类型编译时常量会被intern为同一个对象,所以a与b都引用了这个对象,要检查它们是否引用相等,自然得到true。
作者:RednaxelaFX
链接:https://www.zhihu.com/question/50111592/answer/119694222
来源:知乎
 
 
 
 
附语,不会查询API,好像查了也得靠有道来看。。呜呜~加油~要看懂!!

清华大学计算机系大二 java 小学期考试题(摘自知乎)的更多相关文章

  1. 大数据学习--day04(选择结构、循环结构、大数据java基础面试题)

    选择结构.循环结构.大数据java基础面试题 switch: 注意: byte short int char String(jdk1.7支持) 不能是 long float double boolea ...

  2. 大二 Java上学期总结

    一学期的Java学习结束了,这学期对程序语言的理解更深了,首先感谢李津老师的教导,这学期收获挺多的,不像上学期,这学期没有任何缺课表现,希望之后的语言程序学习会更加努力. 突然感觉Java的学习如此之 ...

  3. 各大公司java后端开发面试题

    各大公司Java后端开发面试题总结 ThreadLocal(线程变量副本)Synchronized实现内存共享,ThreadLocal为每个线程维护一个本地变量.采用空间换时间,它用于线程间的数据隔离 ...

  4. Java,面试题,简历,Linux,大数据,常用开发工具类,API文档,电子书,各种思维导图资源,百度网盘资源,BBS论坛系统 ERP管理系统 OA办公自动化管理系统 车辆管理系统 各种后台管理系统

    Java,面试题,简历,Linux,大数据,常用开发工具类,API文档,电子书,各种思维导图资源,百度网盘资源BBS论坛系统 ERP管理系统 OA办公自动化管理系统 车辆管理系统 家庭理财系统 各种后 ...

  5. 大公司的PHP面试题

    1. 禁用COOKIE 后 SEESION 还能用吗? 2. 抓取远程图片到本地,你会用什么函数? 4. 你觉得在pV10W的时候, 同等配置下,LUNIX 比WIN快多少? 5. 简述pOST 和G ...

  6. JAVA 综合面试题

    JAVA 综合面试题 2007-08-12 目录 TOC \o "1-3" \h \z \u Java面试题整理 9 Java面向对象 9 1. super()与this()的区别 ...

  7. Java 200+ 面试题补充② Netty 模块

    让我们每天都能看到自己的进步.老王带你打造最全的 Java 面试清单,认真把一件事做到最好. 本文是前文<Java 最常见的 200+ 面试题>的第二个补充模块,第一模块为:<Jav ...

  8. Java高级面试题解析(一)

    最近,在看一些java高级面试题,我发现我在认真研究一个面试题的时候,我自己的收获是很大的,我们在看看面试题的时候,不仅仅要看这个问题本身,还要看这个问题的衍生问题,一个问题有些时候可能是一个问题群( ...

  9. Java笔试面试题整理第六波(修正版)

    转载至:http://blog.csdn.net/shakespeare001/article/details/51330745 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

随机推荐

  1. lesson 16 Mary had a little lamb

    lesson 16 Mary had a little lamb a little + 可数 小的;+ 不可数 少量的 对于动物在幼时都有不同的称呼: calf 小牛 lamb 羊羔 piglet 小 ...

  2. 【Python 开发】第一篇:计算机基础

    一.计算机基础 首先Python是一门编程语言 语言: 那什么是语言? 语言就是一种事物与另一种事物沟通的介质.所以说编程语言是程序员跟计算机沟通的介质. 什么是编程: 准确来说就是程序员用计算机所能 ...

  3. 随机森林random forest及python实现

    引言想通过随机森林来获取数据的主要特征 1.理论根据个体学习器的生成方式,目前的集成学习方法大致可分为两大类,即个体学习器之间存在强依赖关系,必须串行生成的序列化方法,以及个体学习器间不存在强依赖关系 ...

  4. 机器学习-线性回归LinearRegression

    概述 今天要说一下机器学习中大多数书籍第一个讲的(有的可能是KNN)模型-线性回归.说起线性回归,首先要介绍一下机器学习中的两个常见的问题:回归任务和分类任务.那什么是回归任务和分类任务呢?简单的来说 ...

  5. vuejs学习之 项目打包之后的首屏加载优化

    vuejs学习之 项目打包之后的首屏加载优化 一:使用CDN资源 我们在打包时,会将package.json里,dependencies对象里插件打包起来,我们可以将其中的一些使用cdn的方式加载,例 ...

  6. 学霸系统PipeLine功能规格说明书

    学霸系统PipeLine功能规格说明书共分为以下三部分: 1.产品面向用户群体 2.用户使用说明 3.产品功能具体实现 1.产品面向用户群体 我们这组的项目并不是传统意义上能发布并进行展示的项目,因此 ...

  7. 20145214 《Java程序设计》第5周学习总结

    20145214 <Java程序设计>第5周学习总结 教材学习内容总结 try和catch Java中所有错误都会被包装为对象,可以尝试try执行程序并捕捉catch代表错误的对象后做一些 ...

  8. GPS定位,根据经纬度查询附近地点的经纬度-sql方法实现

    根据当前所在的坐标点也即经纬度,查找数据库中附近5公里或10公里附近的所有信息的实现,经过查找资料,原来是我高二学的,就是求弦长,数学忘完了,没想到数学还这么有用,数学啊 真是用途太大了. 用到的什么 ...

  9. 移动端调试和fiddler移动端抓包使用

    这里介绍一款移动端的调试工具以及抓包工具fiddler的使用.也是初次接触,算是初次接触的总结. 1,移动端调试工具.手机截图如下 代码实现 <!DOCTYPE html> <htm ...

  10. 第一章 持续集成jenkins工具使用之部署

    1.1 硬件要求 内存:至少512MB 磁盘空间:10G JDK8 最好同时安装jre 从官网https://jenkins.io/download/下载最新的war包(Generic Java Pa ...