关于java内存区域下列说法不正确_2020-2-19 牛客试题复盘

  • Post author:
  • Post category:java


1、下面程序的运行结果是  B

String str1 = “hello”;

String str2 = “he” + new String(“llo”);

System.err.println(str1 == str2);

A、true

B、false

C、exception

D、无输出

解析:

String str1 = “hello”;这里的str1指的是方法区的字符串常量池中的“hello”,编译时期就知道的;

String str2 = “he” + new String(“llo”);这里的str2必须在运行时才知道str2是什么,所以它是指向的是堆里定义的字符串“hello”,所以这两个引用是不一样的。

2、以下程序运行的结果为 ( A  )

publicclassExample extendsThread{

@Override

publicvoidrun(){

try{

Thread.sleep(1000);

} catch(InterruptedException e){

e.printStackTrace();

}

System. out.print( “run” );

}

publicstaticvoidmain(String[] args){

Example example= newExample();

examp



版权声明:本文为weixin_33576058原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。