Friday, November 30, 2012

Uncategorized questions

Which two method you need to implement for key Object in HashMap?
In order to use any object as Key in HashMap, it must implements equals and hashcode method in Java. Read How HashMap works in Java for detailed explanation on how equals and hashcode method is used to put and get object from HashMap. You can also see my post 5 tips to correctly override equals in Java to learn more about equals.

What are the ways in Java for indication an error in a method?
Return value;
Throwing an exception.
Changing the object state in a parameter of the method. The method has t o be passed by reference;


Other sources of Java information


http://javarevisited.blogspot.com

Strings in Java

Differences between creating String as new() and literal assigning?
Described as code, the question transforms in this:
String a = "abc";
String c = new String("abc");


Just a note, which is basic for all comments below – String in Java is immutable.
When we create string with new() Operator, it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap.

Lets look some functionality differences:

Thursday, November 29, 2012

Immutable objects

Immutable objects
Immutable objects are objects whose state cannot change after construction. Examples of immutable objects from the JDK include String and Integer. (String methods, such as String.replace do not change the instance, but create new Strings)

Requirements for immutable classes:
•    ensure the class cannot be overridden - make the class final, or use static factories and keep constructors private;
•    make fields private and final;
•    force callers to construct an object completely in a single step, instead of using a no-argument constructor combined with subsequent calls to setter methods
•    do not provide any methods which can change the state of the object in any way;

Threads in java

Problem 1:
You have thread T1, T2 and T3, how will you ensure that thread T2 start after the end of T1 and thread T3 will start after T2 end?
Solution:
By using Thread.join();
     t1.start(); 
     t1.join(); 
     t2.start(); 
     t2.join(); 
     t3.start(); 
//     t3.join();  - not needed


Problem 2:
"You have T1, T2, and T3. T1 and T2 can execute concurrently, but T3 has to wait for both of them before it can run."
Solution:
     t1.start(); 
     t2.start(); 
     t1.join(); 
     t2.join(); 
     t3.start();
 

Compare wait and sleep methods in java
Wait release the lock or monitor while sleep doesn't release any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution.

Wait is defined in Object class, where sleep methods are defined in Thread class.

Both could throw InterruptedException.


Collections questions

Differences between Iterator and Enumeration?
Iterator duplicate functionality of Enumeration with one addition of remove() method and both provide navigation functionally on objects of Collection.

Difference between Set and List in Java?
In Set the order of the elements in not saved.Set doesn't allowed duplicate while List does and List maintains insertion order while Set does not. Set uses the method equals() internally, not ==.

How do you Sort objects on collection?
The class Collections has functionality for sorting collections. There are two most used methods for this:
Collections.sort() - called without Comparator. It uses Comparable interface in Java. The order is the default , which is specified by CompareTo method;
Collections.sort(Comparator) will sort objects based on compare() method of Comparator. See Sorting in Java using Comparator and Comparable for more details.


Tuesday, November 27, 2012

Similarities and differences between HashMap, Hashtable, ConcurrentHashMap and synchronizedMap

There are several similarities and differences between HashMap, Hashtable, ConcurrentHashMap and synchronizedMap  in Java. (I name synchronizedMap the object retrieved by Collections.synchronizedMap(map) method.)

Monday, November 26, 2012

Fail-fast vs fail-safe iterators

Fail-fast iterators
"Fail-fast iterator" means that if the collection class is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException.

 Throwing ConcurrentModificationException is not granted, so you have NOT to relay on it. ConcurrentModificationException is used to show the developer that his/her classes are not synced.

Most ifiterators in Java are fail-fast, these found in classes:
- LinkedList
- ArrayList
- Vector

Fail-safe iterators
Fail-safe iterator doesn't throw any Exception if Collection is modified structurally.

Advanced view over Concurrent collections

A nice article for java syncronization from IBM developers:
http://www.ibm.com/developerworks/java/library/j-jtp07233/index.html

Friday, November 23, 2012

Waterfall design model

The waterfall model is a sequential design process, often used in software development processes, in which progress is seen as flowing steadily downwards (like a waterfall) through the phases of Conception, Initiation, Analysis, Design, Construction, Testing, Production/Implementation, and Maintenance.

Thursday, November 22, 2012

LinkedList, ArrayList & Vector classes - differences

LinkedList, ArrayList & Vector are tree different implementations of the List interface. LinkedList implements it with a doubly-linked list. ArrayList & Vector classes implement it with a dynamically resizing array.

Fat and thin client architecture

Fat client
A fat client (sometimes called heavy) is a client in client–server architecture that typically provides rich functionality independent of the central server.

MVC or Model–View–Controller

Model–View–Controller (MVC) is an architecture that separates the representation of information from the user's interaction with it.

The model consists is the core of the existing system, where are placed application data and business rules.

The controller mediates input, converting it to commands for the model or view.

A view can be any output representation of data, such as a chart or a diagram. Multiple views of the same data are possible

Wednesday, November 21, 2012

Java "final" modification

In the Java programming language, the final keyword is used in several different contexts to define an entity which cannot later be changed (or reassigned).

Tuesday, November 13, 2012

wait and notify methods in Java

Java includes an elegant inter-process communication mechanism via the wait( ), notify( ), and notifyAll( ) methods. All three methods can be called only from within a synchronized method. Although conceptually advanced from a computer science perspective, the rules for using these methods are actually quite simple:

Monday, November 12, 2012

Software Team Leader Responsibilities

Here are the main responsibilities for a team leader, no matter of language: Java, C++, C#, PHP or whatever.

  • responsible for delivery of working software;
  • prioritize the tasks of team members;
  • to ensure the team is self-organising so that we take collective responsibility for the work we do. It is not my job to tell people what to do but to enable them to do it themselves;
  • responsible for all problems in team;
  • no single responsible for a task. To ensure no one person in my team is solely responsible for any task or activity so that we are able to continue working effectively when any of our team is away
  • monthly one-to-one meetings with team members;


Good practices:
  • buddy assignment to new members