1 Simple Java - ProgramCreek.com

[Pages:219]1

Simple Java

X Wang

Version 1.0 Published In The Wild

CONTENTS

i freface 3

ii java questions 5 1 what can we learn from java helloworld? 6 2 how to build your own java library? 13 3 when and how a java class is loaded and initialized? 16 4 how static type checking works in java? 20 5 java double example 23 6 diagram to show java string's immutability 24 7 the substring() method in jdk 6 and jdk 7 27 8 why string is immutable in java ? 31 9 string is passed by "reference" in java 34 10 start from length & length() in java 38 11 what exactly is null in java? 41 12 comparable vs comparator in java 43 13 java equals() and hashcode() contract 48 14 overriding and overloading in java with examples 52 15 what is instance initializer in java? 55 16 why field can't be overridden? 58 17 4 types of java inner classes 60 18 what is inner interface in java? 63 19 constructors of sub and super classes in java? 67 20 java access level for members: public, protected, private 71 21 when to use private constructors in java? 72 22 2 examples to show how java exception handling works 73 23 diagram of exception hierarchy 75 24 java read a file line by line - how many ways? 78 25 java write to a file - code example 81 26 fileoutputstream vs. filewriter 84 27 should .close() be put in finally block or not? 86

2

Contents 3

28 how to use java properties file? 88 29 monitors - the basic idea of java synchronization 90 30 the interface and class hierarchy diagram of java collec-

tions 93

31 a simple treeset example 97

32 deep understanding of arrays.sort() 100 33 arraylist vs. linkedlist vs. vector 106 34 hashset vs. treeset vs. linkedhashset 112 35 hashmap vs. treemap vs. hashtable vs. linkedhashmap 118

36 efficient counter in java 126

37 frequently used methods of java hashmap 133

38 java type erasure mechanism 136

39 why do we need generic types in java? 139 40 set vs. set 143 41 how to convert array to arraylist in java? 146 42 yet another "java passes by reference or by value"? 149

43 java reflection tutorial 152

44 how to design a java framework? - a simple example 45 why do we need java web frameworks like struts 2? 46 jvm run-time data areas 166 47 how does java handle aliasing? 169 48 what does a java array look like in memory? 172

160 163

49 the introduction of memory leaks 175

50 what is servlet container? 178 51 what is aspect-oriented programming? 182 52 library vs. framework? 185

53 java and computer science courses 187

54 how java compiler generate code for overloaded and overridden methods? 189

55 top 10 methods for java arrays 191

56 top 10 questions of java strings 194

57 top 10 questions for java regular expression 197

58 top 10 questions about java exceptions 203

59 top 10 questions about java collections 207

60 top 9 questions about java maps 213

Part I F R E FA C E

Contents 5

The creation of Program Creek was inspired by the belief that every developer should have a blog. The word "creek" was picked because of the beautiful scenes of Arkansas, a central state of America where I studied and worked for three years. The blog has been used as my notes to track what I have done and my learning experience of programming. Unexpectedly, millions of people have visited Program Creek since I wrote the first post five years ago.

The large amount of traffic indicates a more important fact than that my writing skills are good(which is not the case): Developers like to read simple learning materials and quick solutions. By analyzing the traffic data of blog posts, I learned which ways of explaining things developers prefer.

Many people believe in that diagrams are easier to understand things. While visualization is a good way to understand and remember things, there are other ways to enhance a learning experience. One is by comparing different but related concepts. For example, by comparing ArrayList with LinkedList, one can better understand them and use them properly. Another way is to look at the frequently asked questions. For example, by reading "Top 10 methods for Java arrays," one can quickly remember some useful methods and use the methods used by the majority.

There are numerous blogs, books and tutorials available to learn Java. A lot of them receive large traffic by developers with a large variety of different interests. Program Creek is just one of them. This collection might be useful for two kinds of people: first, the regular visitors of Program Creek will find a convenient collection of most popular posts; second, developers who want to read something that is more than words. Repetition is the key of learning any programming language. Hopefully, this contributes another non-boring repetition for you.

Since this collection is 100% from the blog, there is no good reason to keep two versions of it. The PDF book was converted automatically from the original blog posts. Every title in the book is linked back to the original blog. When the title is clicked, it opens the original post in your browser. If you find any problems, please go to the post and leave your comment there. As it is an automatic conversion, there may be some formatting problems. Please leave a comment if you find one. You can also contact me by email: contact@. Thank you for downloading this PDF!

Chrismas Day 2013

Part II J AVA Q U E S T I O N S

1

W H AT C A N W E L E A R N F R O M J AVA H E L L O W O R L D ?

This is the program every Java programmer knows. It is simple, but a simple start can lead to deep understanding of more complex stuff. In this post I will explore what can be learned from this simple program. Please leave your comments if hello world means more to you. HelloWorld.java

public class HelloWorld { / @param args / public s t a t i c void main ( S t r i n g [ ] args ) { / / TODO Auto-g e n e r a t e d method s t u b System . out . p r i n t l n ( " Hello World " ) ; }

}

1.1 why everything starts with a class?

Java programs are built from classes, every method and field has to be in a class. This is due to its object-oriented feature: everything is an object which is an instance of a class. Object-oriented programming languages have a lot of advantages over functional programming languages such as better modularity, extensibility, etc.

7

1.2. WHY THERE IS ALWAYS A "MAIN" METHOD? 8

1.2 why there is always a "main" method?

The "main" method is the program entrance and it is static. "static" means that the method is part of its class, not part of objects. Why is that? Why don't we put a non-static method as program entrance? If a method is not static, then an object needs to be created first to use the method. Because the method has to be invoked on an object. For an entrance, this is not realistic. Therefore, program entrance method is static. The parameter "String[] args" indicates that an array of strings can be sent to the program to help with program initialization.

1.3 bytecode of helloworld

To execute the program, Java file is first compiled to java byte code stored in the .class file. What does the byte code look like? The byte code itself is not readable. If we use a hex editor, it looks like the following:

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download