1.写出程序运行结果
public class TestString {
public static void link(String a){
a+= "World" ;
}
public static void main(String []args){
String a = "Hello" ;
link(a);
System.out.println(a);
}
}
答: 这道题考两个方面:
1). String对象的内容是不能改变的,a+="World"并不是把a所指对象改变,
而是先生成一个临时String对象,其值为a+"World",然后在把这个临时
对象赋给a.
2). Java中函数参数传递方式为Call by value,link方法中会生产a的一个
拷贝,同样指向a所指的对象,综合以上两点,得出结果为 Hello
2. 写出下面代码的结果
System.out.println(
"
ja
"
+
"
va
"
==
"
java
"
);
答:"=="用来比较对象的引用,而equals()用来比较对象的内容,但是如果是字符串常量,用"=="也可以比较内容
是否相等,"ja"+"va"和"java"都是字符串常量,因此结果为true
同理,下面代码结果也是true
final
String str
=
"
java
"
;
System.out.println(str == " java " )
System.out.println(str == " java " )
3.继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么?
public
class Parent {
//1
static int a = 1;
//2
static
{
a=10;
System.out.println( "parent static code" );
}
//4
public Parent(){
System.out.println( "Parent constructor" );
System.out.println( "Parent a =" +a);
}
public static void main(String []args){
System.out.println( "*****************" );
Parent c = new Child();
}
}
public class Child extends Parent{
static int a = 2;
//3
static {
a=20;
System.out.println( "child static code" );
}
public Child(){
System.out.println( "Child constructor" );
System.out.println( "Child var a=" +a);
}
}
//1
static int a = 1;
//2
static
{
a=10;
System.out.println( "parent static code" );
}
//4
public Parent(){
System.out.println( "Parent constructor" );
System.out.println( "Parent a =" +a);
}
public static void main(String []args){
System.out.println( "*****************" );
Parent c = new Child();
}
}
public class Child extends Parent{
static int a = 2;
//3
static {
a=20;
System.out.println( "child static code" );
}
public Child(){
System.out.println( "Child constructor" );
System.out.println( "Child var a=" +a);
}
}
输出结果:
parent
static code
*****************
child static code
Parent constructor
Parent a =10
Child constructor
Child var a=20
*****************
child static code
Parent constructor
Parent a =10
Child constructor
Child var a=20
由此可看出在还没有实例化类的时候(注意*号)已经执行了static代码块。顺序是先父类后子类.然后才调用父类的构造方法,再调用子类的构造方法.就是这个顺序了.
4.内部类的实现方式:
public
class OuterClass {
static int a;
int b;
private class InnerClass{
public InnerClass(){
System.out.println( "InnerClass create:" +a);
}
}
public OuterClass(){
InnerClass ic = new InnerClass();
System.out.println( "OuterClass create" );
}
public static void main(String []args){
OuterClass oc = new OuterClass();
}
}
static int a;
int b;
private class InnerClass{
public InnerClass(){
System.out.println( "InnerClass create:" +a);
}
}
public OuterClass(){
InnerClass ic = new InnerClass();
System.out.println( "OuterClass create" );
}
public static void main(String []args){
OuterClass oc = new OuterClass();
}
}
总结:
一.静态内部类可以有静态成员,而非静态内部类则不能有静态成员。
二.静态内部类的非静态成员可以访问外部类的静态变量,而不可访问外部类的非静态变量;
三.非静态内部类的非静态成员可以访问外部类的非静态变量
一.静态内部类可以有静态成员,而非静态内部类则不能有静态成员。
二.静态内部类的非静态成员可以访问外部类的静态变量,而不可访问外部类的非静态变量;
三.非静态内部类的非静态成员可以访问外部类的非静态变量
5. Float类型
public
class TestFloat {
public static void main(String []args){
Float a = new Float(3.4);
System.out.println(a.SIZE);
a = new Float(34000);
System.out.println(a.SIZE);
}
}
public static void main(String []args){
Float a = new Float(3.4);
System.out.println(a.SIZE);
a = new Float(34000);
System.out.println(a.SIZE);
}
}
让我们来看看此程序会输出什么呢?
我们先来看看JDK的解释吧.
我们先来看看JDK的解释吧.
public static final int SIZE
The number of bits used to represent a float value.
意思是说:通常去描述一个float值的位数.
这个是一个常量,来看看源码吧:
public static final int SIZE = 32 ;
final int 变量一旦被定义就不能被改变~
第二个Float类型题:
public class
Test2
{
public static void main(String[] args)
{
float a = 3.4 ;
}
}
这个简单的程序能不能编译通过呢?{
public static void main(String[] args)
{
float a = 3.4 ;
}
}
其实是不能的。
原因是精度问题,应该用强制类型转换.
float a = ( float ) 3.4 ;
再来看看这个能不能编译通过
public class
Test2
{
public static void main(String[] args)
{
Float a = new Float( 3.4 );
}
}
其实是没问题的。{
public static void main(String[] args)
{
Float a = new Float( 3.4 );
}
}
我们来看看Float类的源代码吧:
public Float( double value)
{
this .value = ( float )value;
}
this .value = ( float )value;
}
其中有一个构造方法在方法里已经进行了向下转型。
所以这样写是没有问题的.
6.选出用法错误的:
a: Stirng a
=
"
Gone With Wind
"
;
String t = " Wind " ;
String m;
m = a - t;
B: Stirng a = " Gone With Wind " ;
String m ;
m = a[ 3 ] + " one " ;
C: Stirng a = " Gone With Wind " ;
Sting m;
m = a.toUpperCase();
D: 不记得了
String t = " Wind " ;
String m;
m = a - t;
B: Stirng a = " Gone With Wind " ;
String m ;
m = a[ 3 ] + " one " ;
C: Stirng a = " Gone With Wind " ;
Sting m;
m = a.toUpperCase();
D: 不记得了
7.此程序会输出什么?
public
class A {
//3
public A(){
a1();
}
public void a1(){
System.out.println( "A-a1" );
}
}
//3
public A(){
a1();
}
public void a1(){
System.out.println( "A-a1" );
}
}
public
class B
extends A{
int bb = 0;
//2
public B(){
//5
bb=1000;
}
//4
public void a1(){
System.out.println( "bb is" +bb);
System.out.println( "B-a1" );
}
public static void main(String []args){
new B();
}
}
int bb = 0;
//2
public B(){
//5
bb=1000;
}
//4
public void a1(){
System.out.println( "bb is" +bb);
System.out.println( "B-a1" );
}
public static void main(String []args){
new B();
}
}
答案:
bb is 0
B-a1
看看执行顺序就明白了.
在方法被a1()被重写的情况下,父类的a1是没有机会
被调用的.
bb is 0
B-a1
看看执行顺序就明白了.
在方法被a1()被重写的情况下,父类的a1是没有机会
被调用的.