JAVA TIPS?
JAVA is the first programming language I learn, by my limited experience on JAVA, here are some tips u might need to know in an interview.
If there is any error in this page, please contact me for correction. Thanks.
Content // TODO
JAVA is the first programming language I learn, by my limited experience on JAVA, here are some tips u might need to know in an interview.
If there is any error in this page, please contact me for correction. Thanks.
Content // TODO
1. JDK and JRE ?
JDK is Java Development Kit and JRE is Java Runtime Environment.
JDK offers both development environment and runtime envrionment. Thus, in some way, JDK >= JRE, JDK = JRE + javac, where javac is the compiler.
In summary, if u want to write codes in JAVA, u need to install JDK, if u just need to run Java code, JRE will be enough.
2. FINAL ?
Final variable must be initiated and cannot be changed.
Final methods cannot be overrided.
Final classes cannot be extended, all the methods in a final class are also final. Adding "final" for methods in a final class is permitted but meaningless.
3. Primary type ?
Java has 8 primary types:
Note: string is NOT a primary type
3.1 int and Integer ?
int is a primary type while Integer is wrapped class.
int can only store the binary value of an integer, Integer has much wider muliputation access than int: storing, converting, casting.
3.2 Float nad double ?
Both float and double are floating point type which means they are used for storing "fractional numbers" like 3.1415, 1/3, 10.0000. The difference is that float takes 4 bytes and double takes 8 bytes, which leads them to differece precision.
4. Primary type and wrapped type?
a )
Wrapped type can be used for random type.
List<int> intList = new ArrayList<>();
// above line: syntax error: insert "Dimensions" to compete ReferenceType
List<Integer> integerList = new ArrayList<>();
// okay
5. Equals?
There are two types of equal in JAVA: == and equals().
For ==, for primary types, == will just conpare value:
int a = 1;
int b = 2;
sout(a==b); // true
for references types, == will compare the referece:
String a = "123";
String b = "123";
sout(a==b); // true
String c = new string("123");
String d = new string("123");
sout(a==c); // true
sout(a==d); // false, a will be allocated to Constant Pool, while c and d will be allocated to heap memory, thus
sout(c==d); // false
however:
sout(c.equals(d)); // true w? h? y?
for THIS equals():
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
By the source code of equals on string, it's actually rewriten the equals to make it comparing the value rather than the reference.
However, for other types:
TypeA a = new TypeA("12");
TypeA b = new TypeA("12");
sout(a.equals(b)); // false why??
Because for types other than string, Integer, Java did not override the equals() method to make them comparing value;
public boolean equals(Object obj) {
return (this == obj);
}
6. Thread safety, and unsafe?
Definition from wikipedia: Thread safety is a computer programming concept applicable to multi-threaded code. Thread-safe code only manipulates shared data structures in a manner that ensures that all threads behave properly and fulfill their design specifications without unintended interaction. There are various strategies for making thread-safe data structures.
It did took me some time to understand, here is an example:
public class ThreadSafeExample {
public static boolean condition = true;
public static void main(string[] args) {
run();
}
static void run(){
if (condition) {
sout("should print only once!");
this.boolean = false;
}
}
}
in the first time call to run(), the code will print the message, but for the second time, it will not. For the case that run() is called in parallel by multiple threads, assume the first thread has executed just passed the sout and before change value of boolean, the second thread comes in and go through the if-statement. The message will be printed twice, which makes the code thread unsafe.
In Java, thread safety is reflected on different types:
Thread safe Thread unsafe
Vector ArrayList
HashTable HashMap
StringBuffer StringBuilder
6.1 String and StringBuffer, StringBuilder
A string statement will initiate new object and unchangable object, while the later two will only operate on exiting objects. Thus string is not recommanded when lots of modification string content exists.
As StringBuffer is thread safe and StringBuilder is thread unsafe, thus StringBuffer is pro multi thread environment and StringBuilder is pro single thread environment.
7. Common string methods?
indexOf(int ch) // return the index of the first occurance of specific charactor, -1 if not occurred
indexOf(int ch, int fromIndex) // return the first occurance of specific charactor after given index position
indexOf(String str) // return the index of str
indexOf(String str, int fromIndex) // return the index of str after given index position
charAt(int index) // return the charactor at specific index position
replace(string oldStr, String newStr) // replace ALL occurance of oldStr to newStr
trim() // delete whitespace at either ends of the string
split(String divider) // eg: ("1/2/3/4/5").split("/") returns ["1", "2", "3", "4", "5"]
8. Abstract class?
Only abstract class can have abstract methods, however, abstract method is not mandantory for abstract class.
Abstract class cannot be instanciated.
Abstract class cannot be "final"-ed. // referes to 2, final class cannot be extended, while abstract class must have normal classes to extend
8.1 Abstract and interface?
a) extends AbstractClass / implements Interface
b) constructor method: Abstract(√) / Interface (×)
c) one class can implement multiple interface but can extend only one abstract class
d) access field: Interface: public by default / Abstract: can be any
9. File system
Files.exists() // check if file directory exists
Files.createFile() // create file...
Files.createDirectory() //
Files.delete() // delete a file or directory
Files.copy()
Files.move()
Files.size()
Files.read()
Files.write()
10. Containers
11. Collections and collection
12. HashSet and HashTable
13. ArrayList and LinkedList ?
13.1 Array ?
Jav offers the implementation of array to store objects of the same type of fixed size. Instantiation:
exampleArray[] arrayOne; // this is the apprexiated way of instantiation
exampleArray arrayTwo[]; // this is also accpeted but now recommanded. The main purpose of this syntax is to allow / //C/C++ developers to get used to Java
13.2 Back to the original question, ArrayList is based on Array structure, supports random access, while LinkedList is actually douuble-linked-list, does not support random access. Thus access for an element in ArrayList has complexity of O(1) while LinkedList is O(n).
13.3 Array and List?
The big differece between Array and List is that Array allows both direct access and sequencial access, while list only allow sequencial access.
???????????????????????????????????? // todo
13.3.1 Sequencial and direct access: sequencial access has to start from the very beginning, while direct access allow access by index or array.
13.3.2 Transformation?
ArrayList.toArray()
Array.asList()