Category Archives for Java API

Java Random Number Generator

random_number_generationWe will cover Java Random Number Generator here. When it comes to programming, what you are generating is not really random. What we will have is pseudo randomness. So in essence you cannot exactly mimic the way the dice is rolled. But for practical purposes, these numbers will be random.

 

Here are different needs to create random numbers in Java Continue reading

Java class java.util.Timer

Purpose of java.util.Timer:

This class is used to schedule Tasks. Each of the Timer object will have a thread attached to it and uses wait() mechanism for scheduling. There can be more than one task scheduled with each of the timer object, and the programmer must be aware of the delay each of the task can take so that other tasks are not adversely affected.

Specialty:

This class closely work with TimerTask class. The scheduled task could be a daemon thread. Daemon thread is a thread that is usually run for a very long duration in the back end. You cannot expect a real time behavior with the thread attached to it.

Code Sample:

Constructors:

Constructor Details
Timer() The default constructor.
Timer(boolean isDaemon) Constructor that defines is the thread that runs the task has to be a daemon or not.
Timer(String nameOfTheThread) Constructor that names the thread that runs the tasks.
Timer(boolean isDaemon, String nameOfTheThread) Constructor that defines if the thread has to be a daemon and names it.

Frequently used Methods:

Methods here are mainly for scheduling, and cancelling

Methods Details
public void cancel Terminates the scheduler. If there is any currently running task, it will be completed before stopping.
public in purge() Removes the cancelled tasks from the scheduler queue. Normally this doesn’t have to be used. If there are high number of tasks that has been cancelled, it could be used to free them up.
public void schedule(TimerTask task, Date time)  Schedules the task for a specified time
public void schedule(TimerTask task, Date firstTime, long period) Schedules the task for a specified time. The task will be first executed on the time give by firstTime. After the first run, there will be a gap in the running as specified by period.
public void schedule(TimerTask task, long delay) Schedules the specified task for execution after the specified delay.
public void schedule(TimerTask task, long delay, long period) delay is for the initial execution delay. period is for interval between each run.
public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
scheduleAtFixedRate(TimerTask task, long delay, long period) Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.

Java Class java.util.Dictionary

Purpose  of java.util.Dictionary:

Dictionary is an abstract class. This can act as  parent class for the purpose of storing key-value pairs. Examples of the child class is HashTable.

Specialty

Dictionary class is obsolete and one should stay away from using it in the newer implementations. Instead Map interface is to be used.

Code Samples

 

Constructors

Constructor Details
Dictionary() Being an abstract class, you cannot create class directly and should be used for deriving other classes from it. Dictionary has only one constructor.

Frequently used Methods

Method Details
 size()  Size of the dictionary which is number of unique keys
isEmpty() Checks if the dictionary is empty or not
 Enumeration<K> keys()  Returns and Enumeration of keys
 Enumeration<V> elements()  As opposed to keys(), this returns Enumeration of values
 V get(K)  Given the key K, it will return the corresponding value V
 put(K,V)  inserts Key -value pair for K and V
 remove(Object key)  Removes from the dictionary, the entry corresponding to the key

 

 

 

Java InputStream

InputStream is an abstract class based on which all other InputStreams are derived.

InputStreams are used to read bytes of data from a data source.

It differs from a Reader, in that , a Reader deals with characters, while Stream is for bytes.

Major functions

This is abstract function that will be implemented by child classes. Used for reading next byte from the data source.
returns the byte that is read. If there is nothing to read, returns -1

This function reads array of bytes up to the size of the array. If there is no enough data up to the size of the array, reads maximum from the data source.
returns the number of bytes read. If there is nothing to read, returns -1

Very similar to the function read(byte[] bArray). However, here you can specify the offset from which it should be start reading.
return value is same as read(byte[] bArray).