View RSS Feed

Java Basic

Basic Java programming tutorials for learning Java fundamentals.

  1. How to use properties file in Java

    by , 02-29-2012 at 10:57 AM
    Software programs may need to store its settings or configuration in a file on disk, such as database connection settings or user preferences. In Java, it is possible to use some classes in the package such as FileReader and FileWriter to read/write the configuration file. However, that approach is tedious and error-prone, because it requires writing a lot of code from scratch. Fortunately, the Java platform provides an out-of-the-box utility class which is designed specifically for that ...
  2. Final, finally and finalize

    by , 01-08-2012 at 12:38 PM
    final – It’s a constant declaration.

    finally – It handles exception. It is an optional block to write at the end of your exception handling block. It is usually used to close files, close database connections and statements. Also release all other resources in this block.

    finalize – This method is called by the garbage collector. Garbage collector always called this method before it performs garbage collection on that object. It should not be used to release ...
    Access Modifier
  3. Shallow cloning Vs Deep cloning

    by , 01-08-2012 at 12:36 PM
    By default an object’s clone() yields a shallow copy. You must edit your class to get a deep copy of the cloned object.

    • Shallow copy: As shown in figure, when an object is shallow copied, it does not copy its internal objects. Shallow cloning is supported by default in Java. Implement the interface java.lang.Cloneable to implement shallow cloning.
    • Deep copy: In deep cloning, all the internal objects of the class are also copied as shown in figure below. Serialization supports ...
  4. Serialization

    by , 01-08-2012 at 12:33 PM
    Reading or writing an object is called serialization. It saves an object sate which can be restored at a later stage. Implement the interface to make a class serializable. This interface is also called marker interface. It supports object persistence to a file.

    Name:  56.JPG
Views: 995
Size:  19.3 KB


    If you do not want to serialize an object then mark it as transient. Serialization can affect performance in following ways:
  5. Pass-by-reference vs Pass-by-value

    by , 01-08-2012 at 12:31 PM
    In other languages like C++ you can pass a value by reference or by pointer. In Java when you pass a parameter, the receiving object always gets a copy of the passed parameter. It is also known as pass by value concept. To pass a reference of an object in java as an argument, the passed in object is first copied and then passed to other method. In such cases both will be pointing to same object in memory. Also in this case, you will be modifying the original object for which a reference is passed. ...
  6. Abstract class vs Interface

    by , 01-08-2012 at 12:15 PM
    Base classes are used to present interface to its child classes in design. It supports that base class cannot be instantiated. It can be only up casted and its interface is used only. To achieve this, mark the base class as abstract. Compiler gives an error, if you try to create an object of abstract class.

    The interface helps and improves the concept of abstract class. In interfaces, you can only declare methods and cannot provide implementation. The child class which is implementing ...
  7. Encapsulation

    by , 01-08-2012 at 12:14 PM
    To store all the variables and methods in an object is called encapsulation. Variables are declared private to hide them and getter methods are provided to access those variables. It is concept of hiding internal functionality. It helps in improving code modularity and re usability which supports refactoring. It is also important for integrity of application to encapsulate it internal variables and methods. Below code explains process of encapsulation in depth where different objects are kept hidden ...

    Updated 01-14-2012 at 07:39 PM by Java Basic

  8. Inheritance

    by , 01-08-2012 at 12:11 PM
    Process of including methods and variables of base class into its child class is known as Inheritance. The core benefit of Inheritance is code reusability. In case of shared code, move it to base class to refractor and improve the code. It is also a process in which characteristics of one object are transferred into another object.
    Two main forms of inheritance are as following:

    • Implementation inheritance: Functionality of the parent class is reused in the child or derived
    Tags: inheritance Add / Edit Tags
  9. Polymorphism

    by , 01-08-2012 at 12:07 PM
    It allows using objects of different types by using or calling object of only one type. This concept is called polymorphism. It will automatically call the method of desired object. It is also referred as “bottom up” method call. It helps you to add new classes of derived objects without major changes in code and breaking the code call sequence.

    Below image explains the concept of polymorphism. Send a message to a method without knowing its type. It is also known an dynamic binding ...
    Tags: polymorphism Add / Edit Tags
  10. Inheritance vs. Composition

    by , 01-08-2012 at 11:55 AM
    The concept of inheritance is based on “is a” relationship. Composition is based on “has a” relationship. Both of these two concepts allow placing sub objects into your class. Below image explains this key relationship.

    Name:  48.JPG
Views: 1215
Size:  25.3 KB

    Inheritance Vs Composition

    Inheritance is based on one direction concept. You can not apply it in reverse. Extends keyword is used to specify inheritance.

    Composition is used in cases in which ...
  11. How does the Object Oriented approach improve software development?

    by , 01-08-2012 at 11:52 AM
    There are many key benefits of the object oriented approach. Some of those benefits are:

    • Reusability: Object oriented approach support reusability of code with the help of inheritance and composition.
    • Mapping to real world problems: Object oriented approach can map real world objects with encapsulation.
    • Modular Architecture: Object oriented approach supports modular architectures where these modules can be reused.
  12. Static initialize

    by , 01-08-2012 at 11:50 AM
    All the code blocks that do not have a function name and are declared as static are executed before the constructor is called. They are usually used to initialize static fields as shown in below code.

    Java Code: This is an example of static initialize
    public class StaticInitilaizer {
    public static final int A = 5;
    public static final int B;
    //Static initializer block, which is executed only once when the class is loaded.
    static {
    if(A == 5)
    B = 10;
    B = 5;

    Updated 01-14-2012 at 07:37 PM by Java Basic

  13. Explain java class loaders

    by , 01-08-2012 at 11:47 AM
    Classes are loaded into JVM when there are referenced by already running classes in JVM. It’s a hierarchical process. First class that is loaded contains a static main method. All the remaining classes are loaded when they are referenced by main class or any other class that is running in JVM. A namespace is created by the class loaded. One class loader is embedded into JVM by default and it is called bootstrap class loader. Following figure explains the class loading process.

    Name:  45.JPG
Views: 693
Size:  29.8 KB
    Class Loader
  14. Difference between java and C++

    by , 01-08-2012 at 11:44 AM
    Java and C++ are almost similar in syntax. Following are the major differences between these:

    • Java has no support to C++ pointers.
    • Java has no support for multiple inheritances. Java has replaced multiple inheritances with multiple interface inheritance. Interface inheritance allows methods to be inherited from different interfaces. It is also referred to as polymorphism.
    • Java has replaced destructors with finalize methods. These methods are called by the garbage collector
    Java and C++
  15. Difference between java and other platforms

    by , 01-08-2012 at 11:41 AM
    Java is only software based platform. It runs on some specific hardware based operating systems like UNIX, Windows etc. Below diagram explains this process.

    Name:  44.JPG
Views: 1685
Size:  17.1 KB

    Java Compile and JVM

    The Java platform contains the following 2 components.

    • Java Virtual Machine (JVM): It can be deployed on any hardware specific platform. It generates byte codes for the java program.
    • Java Application Programming Interface
  16. Using Imports to Change Behaviour

    by , 11-30-2011 at 03:36 AM
    One thing that would be great to have is a feature that I know well from my previous experience coding in C. That is the possibility to conditional compile in order to modify the behavior of the application. A feature that is missing from Java is C’s conditional compilation, which allows you to change a switch and get different behavior without changing any other code. I think is was left out of Java is that I remember using this feature often to address issues related to compiling a program on ...
    Tutorial , Import
  17. How to do Overloading with Generics

    by , 11-29-2011 at 05:12 PM
    Overloading is a technique often used in object orientated programming languages such as java. In the context of generics unfortunately, classic type of overloading will produce a compile time error because of erasure.

    Java Code:
    import java.util.List;
    public class OverloadingExample<W, T> {
    	void f(List<T> v) {
    	void f(List<W> v) {

    What you must do to overcome this problem is to provide ...