Today, When I was tidying up the disk space of my hard driver, I found something interesting. It is the notes I made when I started learning Java. In fact, I have already used Java for almost 4 years. However, when I am coding, It seems that many things have already became my habits, so that I don’t think about the reason why I do it like that. When reading these notes again, I have a feeling of getting an insight into the problem. Therefore, I suggest all the programmers, not just Java programmer, not always keeping coding, sometimes please stop to think and recall.
In the following, there are some concepts of Java. They are pretty easy, but do you still remember them?
Default values for primitive members: When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it. Notice that this guarantee doesn’t apply to local variables (for example, you declare it in a method), since that are not fields of a class.
The static keyword: When you say something is static, it means that data or method is not tied to any particular object instance of that class, and it directly belongs to the class. Bear in mind that the static method cannot directly access non-static members or methods. There are two ways to refer to a static variable and method. You can name it via an object or refer to it directly through its class name. Usually, we always prefer to the later way.
Distinguishing overloaded methods: there is a siimple rule. Each overloaded method must take a unique list of argument types. Even differences in the ordering of arguments are sufficient to distinguish two methods. And only the differences of the type of return value cannot separate two methods.
String s = new String(“a String”);
this sentence contains four tasks. The first one is creating a String object in the heap; the second one is initializing this String object as “a String”; the third one is create a reference named s; then make this reference point to the String object.
what is the sequence of initializing the members of a java class?
Let’s assume that there is a class named Dog.
- when we create an object of Dog or the first time to access the static members or methods of Dog, JVM need to find Dog.class firstly
- all the static memebers of the Dog class will be initialized
- arranging space for the object created by new Dog() in the heap. This space will be cleared and all the memebers of the Dog class will get their default value
- run the definition initialization
- run the consturctor