in Java, Reading Notes

Thinking in Java Chapter 1&2: Object

After one year of programing with Java, I am gradually familiar with OOP. Learning java for me, whose first programming language is C, was much more than change the name system, like field, method, reference, etc. It is almost a renewal of concept. Although so, my knowledge of Java was founded from all kinds of sources, and a book like Thinking in Java seems useful for me to introduce systematically introduce the thought behind Java.

It is important to design classes appropriately. For people who is not familiar with OOP, it can easily happen that use classes as structs and designs methods as “functions” in the outer classes. Abusing inheritance may also be a popular mistake. When the relationship between classes is “have-a”, one should use composition(or aggregation when dynamically). We should also think about it whether the relationship is “is-a” or “is-like-a”. For the former one, we may only override methods of base class, while the latter one may involves implicating new methods. When we upcasting objects to their base classes types, only methods we use substitution can be called. Otherwise, we will have to downcast the reference to call the unique methods. Although so, both the two models are useful, and the choice will be obvious for certain problems.

Fortunately, Java have implemented parameterized types, which is called genetics, to make the downcasting a little bit easier, although some people critic that the genetics make java even more stupid.

Java has a famous system of garbage collector, which I have heard for many times. As a C-programmer, I was used to create pointers to handle complicated data structures. However, in Java, there is no such things anymore. Basically, except some basic data type like int, double, and Boolean, all the objects are reference. It is something like pointer, but its principle is not the same as pointer’s, so it has a different name. When people use new to create an object, java arrange it in heap – like malloc in C. I like to use malloc in C to increase the agility of my codes, but I will have to free the space after use. If I don’t use things like malloc, realloc, calloc, the new variance will be arrange in stack. In Java, you can’t put object other than basic data types in stacks. When the objects are no longer useful, the garbage collector will destroy them automatically, which may cost some time. But anymore, in this way, our life might be easier as developers.

Under this frame, the scope and lifetime in Java may be a little bit different. All the methods and variances(as field) has to be in a class. The class who contains the main method will automatically become something like “main class”. The file name has to be the same as the class name-a interesting system, at least the division of files is more natural than C. A strange thing is, in Java, to “hide” a variable in a larger scope is not allowed, which is legal in C, and, if I remembered, Python.

Only the variance as a field in a class has been initialized automatically, those in method didn’t. Well, I think initializing field manually is necessary, unless those fields are not important in certain cases.

Javadoc is also a useful tool to build manuals for other developers. In the past, I have read a lot of documents in form of Javadoc htmls. I have to admit that they are convenient, either read or build.

I will continue explore in this book, although I prefer the idea behind Python. Anyway, Java is very popular, and learning more doesn’t hurt, does it? Moreover, I found myself begin to love it, at least in some aspects:)

Write a Comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.