// 计数器
final int start = 2000000000;
int count = 0;
// 陷阱 float 最大精确长度为6~7 所以 2000000000 ==2000000050 相等
for (float f = start; f < start + 50; f++)
count++;
System.out.println("计数器-->" + count);
// 分分钟
int minutes = 0;
for (int ms = 0; ms < 60 * 60 * 1000; ms++)
// 陷阱 % * 优先级问题
if (ms % 60 * 1000 == 0)
minutes++;
System.out.println("分分钟-->" + minutes);
static boolean decision() {
try {
return true;
} finally {
//陷阱 不管try任何异常还是正常 都会执行finally 代码 所以会返回 false;
return false;
}
}
System.out.println(decision());
public class Arcane1 {
public static void main(String[] args) {
try {
//坑 println 没有在任何地方说明要抛出IOException 异常 所以不能编译、、、
System.err.println("Hello wirld");
} catch (IOException e) {
System.err.println("naver seen println fail!!!");
}
}
}
//能正常运行
public class Arcane2 {
public static void main(String[] args) {
try {
//
} catch (Exception e) {
System.err.println("handle exception");
}
}
}
public class Arrays {
interface Type1 {
//语法错误 throws
void t() throws CloneNotSupportedException();
}
interface Type2 {
//语法错误 throws
void t2() throws InterruptedException();
}
interface Type3 extends Type1, Type2 {
}
public class Arcane3 implements Type3 {
public void f() {
System.out.println("Hello world");
}
@Override
public void t() throws CloneNotSupportedException {
// TODO Auto-generated method stub
}
@Override
public void t2() throws InterruptedException {
// TODO Auto-generated method stub
}
}
public static void main(String[] args) {
Type3 a = new Arcane3();
a.t();
}
}
private static final long USER_ID;
private static final long GUEST_USER_ID = -1;
static {
try {
USER_ID = getUserId();
} catch (IdUnavailableException e) {
// final 字段 赋值问题 final 只能赋值一次
// The final field USER_ID may already have been assigned
USER_ID = GUEST_USER_ID;
System.out.println("in a guset");
}
}
private static long getUserId() throws IdUnavailableException {
throw new IdUnavailableException();
}
class IdUnavailableException extends Exception {
IdUnavailableException() {
}
}
public static void main(String[] args) {
}
// 当 执行 System.exit(0) 的时候 系统会执行 addShutdownHook 方法可以在这里出息 其他情况
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
super.run();
System.err.println("good bay");
}
});
try {
System.err.println("Hello world ");
// 讲停止当前线程及其所有其他当场死亡的线程
System.exit(0);
} finally {
System.err.println("good bay");
}
//
private Arrays tReluctant = new Arrays();
public static void main(String[] args) {
try {
// 递归异常 StackOverflowError 无法捕获
Arrays tReluctant = new Arrays();
System.out.println("surprise");
} catch (Exception e) {
System.out.println("i told you so");
}
}
Arrays() throws Exception {
throw new Exception();
}
static void copy(String file, String oldFile) {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(file);
out = new FileOutputStream(oldFile);
byte[] buf = new byte[1024];
int n;
while ((n = in.read(buf)) >= 0) {
out.write(buf, 0, n);
}
} catch (IOException e) {
// close 也会抛出异常
/*if (null != in) {
throw IOException
in.close();
}
if (null != out) {
throw IOException
out.close();
}
*/
//改进办法
close(in);
close(out);
}
}
//改进办法
public static void close(Closeable closeable) {
if(null!=closeable) {
try {
closeable.close();
} catch (IOException e) {
// TODO: handle exception
}
}
}
public static void main(String[] args) {
Type type = new Type(null);
}
/*
* 重载解析 分为二个阶段;
*
* 一. 选取所有可或得并可应用的构造器
*
* 二.选择构造器中最精准的哪一个
*
* 三.如何判断是否精准 《如果一个方法或者构造器可以接受传递给另外一个方法或构造器的任何参数,
* 那么我们就说第一个方法比第二个方法缺乏精准性》
*
*/
public static class Type {
public Type(Object jObject) {
System.out.println("Object");
}
// 会被调用
public Type(double[] doubles) {
System.out.println("doubles");
}
}
/**
*
* 静态域公用 导致 会出现 5-5
*
* 如果出现记录单独计数器 应该独立而不是共有静态域
*
*/
static class Counter {
private static int count;
public static void increment() {
count++;
}
public static int getCount() {
return count;
}
}
static class Dog extends Counter {
Dog() {
}
public void woof() {
increment();
}
}
static class Cat extends Counter {
Cat() {
}
public void woof() {
increment();
}
}
public static void main(String[] args) {
Dog dogs[] = { new Dog(), new Dog() };
for (int i = 0; i < dogs.length; i++) {
dogs[i].woof();
}
Cat cats[] = { new Cat(), new Cat(), new Cat() };
for (int i = 0; i < cats.length; i++) {
cats[i].woof();
}
System.out.println(Dog.getCount());
System.out.println(Cat.getCount());
}