符号 ~
public static void main(String[] args){
int i = 5;
int j = 10;
System.out.println(i + ~j);
}
输出是什么?
-6
“~” 是位运算符,“非”的意思。平时项目中用到位运算符的时候的确很少:Java 运算符(位运算符)
</br>
</br>
Thread Join
public class MyThread extends Thread {
String myName;
MyThread(String name) {
myName = name;
}
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(myName);
}
}
public static void main(String args[]) throws InterruptedException {
MyThread mtl = new MyThread("mt1");
MyThread mt2 = new MyThread("mt2");
mtl.start();
// XXX
mt2.start();
}
}
//XXX
处替换成什么代码,能先打印100次mt1
再打印100次mt2
答案mtl.join();
</br>
</br>
关于构造函数
public class MyTest {
String s;
public static void main(String[] args) {
MyTest obj = new MyTest();
obj.go();
}
void MyTest() {
s = "this is a test";
}
void go() {
System.out.println(s);
}
}
输出是什么?
null
因为构造函数必须是“无返回值”的,void
也不能加。去掉void就能打印出this is a test
限制符 public
protected
private
可加可不加,看情况。
</br>
</br>
Swap, 引用传递,值传递
public class Swap {
public static void main(String args[]) {
String name[] = { "Killer", "Miller" };
String name0 = "Killer";
String name1 = "Miller";
swap(name0, name1);
System.out.println(name0 + ", " + name1);
swap(name);
System.out.println(name[0] + ", " + name[1]);
}
public static void swap(String name[]) {
String temp;
temp = name[0];
name[0] = name[1];
name[1] = temp;
}
public static void swap(String name0, String name1) {
String temp;
temp = name0;
name0 = name1;
name1 = temp;
}
}
输出?
Killer, Miller
Miller, Killer
</br>
</br>
List 与 Iterator 获取元素
public static void main(String args[]) {
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
Iterator<String> iterator = list.iterator();
String firtElement;
//XXX
firtElement = list.get(1);
firtElement = list[1];
firtElement = iterator.get(1);
firtElement = iterator[1];
firtElement = iterator.next();
}
//XXX
后哪几行是合法的?
firtElement = list.get(1);
firtElement = iterator.next();
</br>
</br>
有顺序的Set
public static void main(String args[]) {
//XXX
set.add("Apple");
set.add("Cricket");
set.add("Banana");
Iterator<String> iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
//XXX
加什么代码,顺序打印出Apple Cricket Banana
答案 Set<String> set = new LinkedHashSet<String>();
</br>
</br>
Do While
public static void main(String args[]) {
int i = 1, j = 10;
do {
if (i > j) {
break;
}
j--;
} while (++i < 5);
System.out.println("i: " + i + ", j: " + j);
}
输出?
i: 5, j: 6
</br>
</br>
类的初始化顺序
前面的题都有点无聊,这道题容易搅晕
public class Hawk extends Raptor {
public static void main(String args[]) {
System.out.println("Start Main Method");
new Hawk();
System.out.println("Leave Main Method");
}
}
class Bird {
{
System.out.println("Bird1");
}
public Bird() {
System.out.println("Bird2");
}
}
class Raptor extends Bird {
static {
System.out.println("Raptor1");
}
public Raptor() {
System.out.println("Raptor2");
}
{
System.out.println("Raptor3");
}
static {
System.out.println("Raptor4");
}
}
关于这题,总结了下:
父类static{} -> static{} -> 父类{} -> 父类Constructor() -> {} -> Constructor()
-
类的初始化(
static{}
) 总是先于 对象的初始化({}
与Constructor()
) -
对象的初始化(
{}
与Constructor()
),{}
总在Constructor()
之前 - 类的初始化 与 对象的初始化 总是父类先执行
- 初始化块(
static{}
与{}
)总是按类中定义的顺序执行 - 类什么时候被初始化?理解Java中的类初始化
输出
Raptor1
Raptor4
Start Main Method
Bird1
Bird2
Raptor3
Raptor2
Leave Main Method
</br>
</br>
无类型的TreeSet
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String args[]) {
Set set = new TreeSet();
set.add("2");
set.add(3);
set.add("1");
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
能编译吗?能运行吗?打印什么?
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at java.lang.Integer.compareTo(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at sample.hibernate4.TreeSetTest.main(TreeSetTest.java:16)
原理不太懂,感觉第一个add的元素即确定了集合的类型,遍历时发现有不同类型时会报错。求大神给个解释的连接
</br>
</br>