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);
}
}
答: 这道题考两个方面:
System.out.println(
"
ja
"
+
"
va
"
==
"
java
"
);
final
String str
=
"
java
"
;
System.out.println(str
==
"
java
"
)
//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);
}
}
输出结果:
*****************
child static
code
Parent constructor
Parent a =10
Child constructor
Child var a=20
由此可看出在还没有实例化类的时候(注意*号)已经执行了static代码块。顺序是先父类后子类.然后才调用父类的构造方法,再调用子类的构造方法.就是这个顺序了.
4.内部类的实现方式:
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();
}
}
一.静态内部类可以有静态成员,而非静态内部类则不能有静态成员。
二.静态内部类的非静态成员可以访问外部类的静态变量,而不可访问外部类的非静态变量;
三.非静态内部类的非静态成员可以访问外部类的非静态变量
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的解释吧.
public
static
final
int
SIZE
The number of bits used to represent a
float
value.
意思是说:通常去描述一个float值的位数.
这个是一个常量,来看看源码吧:
public
static
final
int
SIZE
=
32
;
public
class
Test2
{
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
);
}
}
我们来看看Float类的源代码吧:
public
Float(
double
value)
{
this
.value
=
(
float
)value;
}
其中有一个构造方法在方法里已经进行了向下转型。
所以这样写是没有问题的.
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: 不记得了
7.此程序会输出什么?
//3
public
A(){
a1();
}
public
void
a1(){
System.out.println("A-a1"
);
}
}
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是没有机会
被调用的.