What is the JRE?
Answer
The JRE is the Java Runtime Environment. It’s a container for components that allows you to run java files on a machine. It contains the java class libraries and the java virtual machine.
What is the JDK?
Answer
The JDK is the Java Development Kit. The jdk contains jre as well as a compiler to convert the java classes to java byte code.
What is the JVM?
Answer
The JVM is the Java Virtual Machine. It’s a virtual machine that allows a computer to run programs that where compiled in java byte code. The .class files are all written in java byte code.
What is a Singleton Class and whats the best way to implement one?
Answer
It is a class that controls object creation to allow the creation of only one object. The best way to implement a singleton is to use an enum. Java allows only one instance of an enum to be created. I created a quick program to demonstrate implementing an enum. Variables one and two are referenced to the enum. We add the count to only the first variable but its actually only adding to the single enum EnumCount.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | class Singleton{ public static void main(String[] args) { EnumCount one = EnumCount.INSTANCE; EnumCount two = EnumCount.INSTANCE; one.AddCount(); one.AddCount(); one.AddCount(); System.out.println("The count of two is " + two.getCount()); }}enum EnumCount { INSTANCE; private int count = 0; public void AddCount() { count++; } public int getCount() { return count; }} |
The output will be | The count of two is 3
Can a semaphore act as a mutex?
Yes it can. You would have to restrict the semaphore to only allow 1 thread to access the resource. A semaphore restricts the number of threads that can access a resource. A mutex only allows one thread to access the resource at a time.Answer
If you were to use a set, how would you determine between a HashSet and a TreeSet?
Answer
A HashSet is implemented using a hashtable where the elements are not ordered. A TreeSet is implemented using a tree structure where the elements are sorted using a tree algorithm.
A HashSet is faster then a TreeSet. If your elements do not require sorting a HashSet is good.
If you have elements that will be null then choose a HashSet. A TreeSet uses compareTo( ) which will throw a NullPointerException if the object is null.
Why should you avoid the finalize() method in the Object class? What are some alternatives?
Answer
The finalize method is not called until the garbage collector is ran. Knowing when that will happen is uncertain. That could result in finalize never being ran.
Two things you could do as an alternative to using finalize:
– Implement a close method and document that it should be called.
– Produce a cleaner class that performs cleanup actions.