Sunday, June 30, 2013

Java :: How To Override toString

How To Override toString
@Override public String toString()
{
return this.get(Field you want to return); // e.g: getName();
}

Why to use this kind override?
Sometimes we need to add for example a collection to a combo box so instead of looping
 over the object list you override the toString in that object to return one of the object variables

Sunday, June 9, 2013

Java :: HotSpot

Java HotSpot

The Java HotSpot Virtual Machine is a core component of the Java SE platform. It implements the Java Virtual Machine Specification, and is delivered as a shared library in the Java Runtime Environment. As the Java bytecode execution engine, it provides Java runtime facilities, such as thread and object synchronization, on a variety of operating systems and architectures. It includes dynamic compilers that adaptively compile Java bytecodes into optimized machine instructions and efficiently manages the Java heap using garbage collectors, optimized for both low pause time and throughput. It provides data and information to profiling, monitoring and debugging tools and applications.
HotSpot is an "ergonomic" JVM. Based upon the platform configuration, it will select a compiler, Java heap configuration, and garbage collector that produce good to excellent performance for most applications. Under special circumstances, however, specific tuning may be required to get the best possible performance. The resources collected here will help the reader understand and tune the Java HotSpot Virtual Machine.

The Java HotSpot Performance Engine Architecture

Wednesday, May 1, 2013

Java :: Why Java extends only one class?


Why Java extends only one class?
From SCJP 6 study guide:
"Some languages (like C++) allow a class to extend more than one other class.
This capability is known as "multiple inheritance." The reason that Java's
creators chose not to allow multiple inheritance is that it can become quite
messy. In a nutshell, the problem is that if a class extended two other classes,
and both superclasses had, say, a doStuff() method, which version of doStuff()
would the subclass inherit? This issue can lead to a scenario known as the
"Deadly Diamond of Death," because of the shape of the class diagram that
can be created in a multiple inheritance design. The diamond is formed when
classes B and C both extend A, and both B and C inherit a method from A. If
class D extends both B and C, and both B and C have overridden the method
in A, class D has, in theory, inherited two different implementations of the
same method. Drawn as a class diagram, the shape of the four classes looks
like a diamond."


Tuesday, April 30, 2013

SQL :: Convert Integer to String

Convert Integer to String
you can convert an integer to string using the following query:

select CAST(ID as varchar2(50)) as ID_STRING from TABLE_NAME.

Have a nice day.

Sunday, April 28, 2013

Java :: Quartz

Steps to use Quartz 

1- Download Quartz
From http://quartz-scheduler.org/downloads/catalog and after free registration.
Extract the archive file and copy quartz-2.1.7.jar (or the name of the downloaded version) to your project libraries.

2- Download slf4j
From http://www.slf4j.org/download.html copy slf4j-api-1.7.5.jar (or the name of the downloaded version) to your project libraries.

3- Create Main Class and Implement Job Interface
Use the bellow example:


public static void main(String[] args) {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched;
try {
sched = sf.getScheduler();
JobDetail job = JobBuilder.newJob(Main.class)
.withIdentity("SimpleJob").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("SimpleJob")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5).repeatForever())
.build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);

} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

or you can download the source from:
https://github.com/firask86/QuartzExample

The example demonstrate simple and cron schedule.




Thursday, April 18, 2013

Java :: Singleton Pattern

Definition
Singleton pattern is one of the most commonly used patterns, it is used to ensure that only one instance of an object is created, and as in the implementation example you can see that a check for a STATIC variable if not exists create one (and it will be only the first time off-course) then the same instance will be returned always.
Focus on that the main constructor for the class should be implemented as private to make sure that no one can create an object for your class using else than getInstance method

Implementation

 public class SingletonClass {
   private static SingletonClass instance;

   private SingletonClass() {}

   public static SingletonClass getInstance() {
    if(instance == null) {
     instance = new SingletonClass();
     return instance;
    } else {
      return instance;
   }
 }

Sunday, March 17, 2013

Java :: HashMap Vs HashTable



 HashMap Vs HashTable

There are several differences between HashMap and Hashtable in Java:
  1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
  2. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
  3. One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.
Since synchronization is not an issue for you, I'd recommend HashMap.
Reference: 
http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable