Java Note 3

Static Constructor
public class Demo{

                static int sint = 100;
                // static block 1
                static{
                        sint = 10;
                } 
                // static block 2
                static{
                        sint  = 1;
                        }
}
This initializing progress created a method named "clinit"
This method will not have a special name to differ from each other, which means this will run in order.
Bytecode:
putstatic #3
bipush 100
putstatic #3
bipush 10
iconst 1
// Means this will be created as one static constructor, one total method named "clinit"(class init)
public class Demo{
            static{
                System.out.println("in static constructor......");
}
            Demo(int j){
                System.out.println("in Demo(int j)......");
}
            public static void main(String[] args) throws ClassNotFound{
                //Class clazz = Class.forName("Demo"); // output: in static constructor.....
                Demo d1 = new Demo(1);
                Demo d2= new Demo(2);
                // output: in static constructor... \n in Demo(int j)... \n in Demo(int j)....
                    }
}
This means static constructor will be called when a class is loaded.
Also, when you use a reference, static constructor will not always be called, for you may use main() of this class.
Inherit will make constructor calling complex, for the son class will definately call father's constructor.
Foreach or say Enhanced forloop
int[] ints = new int[5];
for (int i:ints){
        System.out.println(i);
}
You can switch a String Object now23333333
outer:
for(int i:ints){                        // for 1
            for(int j:ints){               // for 2
                        break;                //  break for 2
                        break outer;   // break for 1
                    }
}
Why build a class?
For creating an object(Mostly)
For creating a namespace(Sometimes, and methods are always static)
Recently, Functional Programming(函数式编程) is quite popular
Von Nuemann Bottleneck: FP as a solve
 1. Imperative         C, Java...      Follow the orders(命令式)
2. Functional       select * from tb_emp  -- This order aims to describe your goal rather than specific move
You don't need to care about the environment settings and the application will do this automatically.
3. Logical
public class Demo{
            static int addOp(int a, int b){
                            if (a>b) return 0;
                            else return a+addOp(a+1, b);
            }

}
pubilc class Demo{
                public static void main(String[] args){
                            f();
                }
                void f(){
                        f();
                }
}
output: Exception in thread "main" java.lang.StackOverflowError
java.lang.StackOverflowError, as its name tells us, the stack overflows 23333
Means that the stack area is not enough for such number of stack frame...
One thread one stack, with a stack area and a lot of stack frame
Bigger stack frame will have less numbers of them
@FunctionalInterface
interface F{
        int f(int a);
}
static int addOp(F f, int a, int b){
        if(a>b) return 0;
        else return f.f(a)+addOp(f,a+1,b);
}
public static void main(String[] args){
        System.out.println(addOp(i->i*i*i, 1,4)); // output: 100
}
static F add(){
        return null;
}
static void f(int... ints){
}
Used as an array, but you can call f(1,3,4,4,3,4,4)
Method-Call Stack and Activation Records(Stack Frame)
In one package will assure the entrance
But if you delete the public, then import this package will not work either.
package java.lang

public class Test{
            System.out.println("hello");
}
java.lang,SecurityException: Prohibited package name: java.lang
runtime package: classes loaded by different class loader will be treated as different class
Using enum to avoid listing possible choices and check input
enum Direction{
        EAST, WEST, NORTH, SOUTH
}
Calling uses Direction.EAST
What does enum do?
A multiton 
class Direction{
            public static final Direction EAST = new Direction();
            public static final Direction WEST = new Direction();
            public static final Direction NORTH = new Direction();
            public static final Direction SOUTH = new Direction();

            private Direction();
}
You can build a constructor to give the objects in enum class types
You can also overload functions like this:
enum Directions{
            EAST(0),WEST(1),NORTH(2),SOUTH(3){
                        @Override
                        public String toString(){
                                        return "23333";            
                                        }
                                };
}
java.lang,Enum
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值