java

 

User defined Exception subclass

You can also create your own exception sub class simply by extending java Exception class. You can define a constructor for your Exception sub class (not compulsory) and you can override the toString() function to display your customized message on catch. class MyException extends Exception { private int ex; MyException(int a) { ex=a; } public …

User defined Exception subclass Read More »

Method Overriding with Exception Handling

There are few things to remember when overriding a method with exception handling. If super class method does not declare any exception, then sub class overriden method cannot declare checked exception but it can declare unchecked exceptions. Example of Subclass overriden Method declaring Checked Exception import java.io.*; class Super { void show() { System.out.println(“parent class”); …

Method Overriding with Exception Handling Read More »

Chained Exception

Chained Exception was added to Java in JDK 1.4. This feature allow you to relate one exception with another exception, i.e one exception describes cause of another exception. For example, consider a situation in which a method throws an ArithmeticException because of an attempt to divide by zero but the actual cause of exception was …

Chained Exception Read More »

Exception Handling

Chained Exception was added to Java in JDK 1.4. This feature allow you to relate one exception with another exception, i.e one exception describes cause of another exception. For example, consider a situation in which a method throws an ArithmeticException because of an attempt to divide by zero but the actual cause of exception was …

Exception Handling Read More »

What is Multithreading in java?

What is Multithreading in java? Multithreading A program can be divided into a number of small processes. Each small process can be addressed as a single thread (a lightweight process). Multithreaded programs contain two or more threads that can run concurrently. This means that a single program can perform two or more tasks simultaneously. For …

What is Multithreading in java? Read More »

Thread Class in java

Thread class is the main class on which Java’s Multithreading system is based. Thread class, along with its companion interface Runnable will be used to create and run threads for utilizing Multithreading feature of Java. Constructors of Thread class Thread ( ) Thread ( String str ) Thread ( Runnable r ) Thread ( Runnable …

Thread Class in java Read More »

Creating a thread in java

Java defines two ways by which a thread can be created. By implementing the Runnable interface. By extending the Thread class. Implementing the Runnable Interface The easiest way to create a thread is to create a class that implements the runnable interface. After implementing runnable interface , the class needs to implement the run() method, …

Creating a thread in java Read More »

Joining threads in java

Sometimes one thread needs to know when another thread is ending. In java, isAlive() and join() are two different methods to check whether a thread has finished its execution. The isAlive() method returns true if the thread upon which it is called is still running otherwise it returns false. final boolean isAlive() But, join() method …

Joining threads in java Read More »

Synchronization in java

At times when more than one thread try to access a shared resource, we need to ensure that resource will be used by only one thread at a time. The process by which this is achieved is called synchronization. The synchronization keyword in java creates a block of code referred to as critical section. Every …

Synchronization in java Read More »

Interthread Communication in java

Java provide benefit of avoiding thread pooling using interthread communication. The wait(), notify(),notifyAll() of Object class. These method are implemented as final in Object. All three method can be called only from within a synchronized context. wait() tells calling thread to give up monitor and go to sleep until some other thread enters the same …

Interthread Communication in java Read More »

Enumerations in java

Enumerations was added to Java language in JDK5. Enumeration means a list of named constant. In Java, enumeration defines a class type. An Enumeration can have constructors, methods and instance variables. It is created using enum keyword. Each enumeration constant is public, static and final by default. Even though enumeration defines a class type and …

Enumerations in java Read More »

Autoboxing and Unboxing In Java

Autoboxing and Unboxing Autoboxing and Unboxing features was added in Java5. Autoboxing is a process by which primitive type is automatically encapsulated(boxed) into its equivalent type wrapper Auto-Unboxing is a process by which the value of object is automatically extracted from a type wrapper. Example of Autoboxing and Unboxing class Test { public static void …

Autoboxing and Unboxing In Java Read More »

IO Stream

Java performs I/O through Streams. A Stream is linked to a physical layer by java I/O system to make input and output operation in java. In general, a stream means continuous flow of data. Streams are clean way to deal with input/output without having every part of your code understand the physical. Java encapsulates Stream …

IO Stream Read More »

Networking in Java

Java is a premier language for network programming. java.net package encapsulate large number of classes and interface that provides an easy-to use means to access network resources. Here are some important classes and interfaces of java.net package. Some Important Classes CLASSES CacheRequest CookieHandler CookieManager Datagrampacket Inet Address ServerSocket Socket DatagramSocket Proxy URL URLConnection   Some …

Networking in Java Read More »

Generics In JAVA

A class or interface that operates on parameterized type is called Generic. Generics was first introduced in Java5. Now it is one of the most profound feature of java programming language. It provides facility to write algorithm independent of any specific type of data. Generics also provide type safety. Using Generics, it becomes possible to …

Generics In JAVA Read More »

Collection Framework in java

Collection framework was not part of original Java release. Collections was added to J2SE 1.2. Prior to Java 2, Java provided adhoc classes such as Dictionary, Vector, Stack and Properties to store and manipulate groups of objects. Collection framework provides many important classes and interfaces to collect and organize group of alike objects. Important Interfaces …

Collection Framework in java Read More »

The Collection classes in java

Java provides a set of Collection classes that implements Collection interface. Some of these classes provide full implementations that can be used as it is and other abstract classes provides skeletal implementations that can be used as starting points for creating concrete collections. ArrayList class ArrayList class extends AbstractList class and implements the List interface. …

The Collection classes in java Read More »

Accessing a Collection in java

To access, modify or remove any element from any collection we need to first find the element, for which we have to cycle throught the elements of the collection. There are three possible ways to cycle through the elements of any collection. Using Iterator interface Using ListIterator interface Using for-each loop Accessing elements using Iterator …

Accessing a Collection in java Read More »