Posts

Showing posts from November, 2017

Generics : Bounded Types

In generics, the type parameters could be replaced by any class type. But in some scenarios, we would like to set a boundary on the class types. Lets consider one such scenario. Suppose we want to concatenate a string to the object. Lets see the generic class defined below : public class BoundedTypeClass< T > { T obj ; BoundedTypeClass ( T o) { obj = o ; } T getObj () { return obj ; } public void concate () { obj .concat( "Done" ) ; } public void show () { System. out .println( "The type of obj is : " + obj .getClass().getName()) ; } } Now concat() is a method only available to Strings. This results into a compilation error : cannot find concat() for type T. Another scenarios where errors can occur are when we pass Strings in a method that computes the average of integers. For this example, to only accept strings, we need to bind the type. i.e. we need to extend T with a supercl

Defining your own Generic Class

Apart from using Generics with classes in the Java framework, we can create our own generic classes. Lets create a generic class named MyGenericClass as follows : public class MyGenericClass< T > { T obj ; MyGenericClass ( T o) { obj = o ; } T getObj () { return obj ; } public void show () { System. out .println( "The type of obj is : " + obj .getClass().getName()) ; } } Here whatever the object we receive in the constructor and the value of T used while declaring the object tells what type  is going to be used. Lets use this class in our test class to see its usage : MyGenericClass<String> myG1 = new MyGenericClass<String>( "Sandeep" ) ; System. out .println(myG1.getObj()) ; myG1.show() ; MyGenericClass<Integer> myG2 = new MyGenericClass<Integer>( 100 ) ; System. out .println(myG2.getObj()) ; myG2.show() ; The above declarations change the value of T to java.lang.St

How Generics works?

To understand how generics work, lets take a look at how things used to work before introduction of generics. Lets take example of ArrayList. Prior to Generics, we used to declare ArrayList as follows : ArrayList objList = new ArrayList(); and use it like : objList.add(new Employee(1, "Sandeep", "IT"); objList.add("Sandeep") objList.get(0) //objList.get(index) i.e. The add() method used to accept Object references so it is not type-safe and the get() method used to return Object reference which we used to type-cast. With the introduction of Generics, a new definition of ArrayList class looks like : class ArrayList<T> { ... add(T t) T get(int index) ... } You can check the code for ArrayList in jdk  here  or any other standard implementations. Whenever we use Generics, at the compilation time, the type parameter is replaced by the reference type that we use. E.g. ArrayList<T> becomes ArrayList<Employee> add(T t) bec

Generics Continued

There are different ways to use generics. Before seeing that lets revisit the syntax for creating an instance of a generic class : class-name<parameter-type> objectName = new class-name<parameter-type>() ; As we saw in the previous post, we can use the following syntax for creating an ArrayList with Generics : ArrayList<String> s1 = new ArrayList<String>() ; s1.add( "Sandeep" ) ; s1.add( "Ravi" ) ; for (String s:s1) { System. out .println(s) ; } Apart from using ArrayList as the reference type, we can also use List as the reference type as follows : List<String> s2 = new ArrayList<String>() ; s2.add( "Sandeep" ) ; s2.add( "Ravi" ) ; for (String s:s2) { System. out .println(s) ; } What this means is that we can use the reference of a parent class to hold the child object. Generics supports that. Lets try to use generics with primitive types: List< int > i1 = new ArrayList< in

Generics

The main objective of generics is to resolve type casting problems i.e. to provide type-safety in the code. What is type safety? Suppose you want to store a list of a particular type of objects, say Employee. Now lets consider 2 options : Arrays and ArrayLists. You would store it in an array as follows : Employee[] employees = new Employee[100]; employees[0] = new Employee(1, "Sandeep"); employees[1] = new Employee(1, "Ravi"); ....... and likewise. Now if you try to use employees[10] = "Sandeep" i.e. try to store a string instead of an employee object; it should give a compile time error. Something similar to - Incompatible types : Expected Employee Got String. Try this. The point made here is that Arrays are type safe . Whenever we try to store anything except the type that the array was intended, it would result in a compilation error. Now lets take the same example with ArrayList. Store the list of employees in an arraylist as follo

The First Program

Lets create our first program. It will just print to the console. Copy the following code to a file and save the file as "HelloWorld.java" public class HelloWorld {     public static void main(String[] args) {         System.out.println("Hello World!! Welcome to Java.");     } } Now go to console and compile the program with the following command : javac HelloWorld.java If the above command doesnt work, you need to configure the path and the classpath in your environment. Instructions for the same can be found  here . Once the program compiles successfully, it generates a .class file. In this case, it will be HelloWorld.class. This is actually the bytecode that JVM will execute. This is not an executable. To run the program, use the following command : java HelloWorld This should print  "Hello World!! Welcome to Java."  on the console. Understanding the program 1. The class name and the file name are identical i.e. HelloWorld. Thi

Evolution of Java

In 1996, the first version of Java was released. It was nothing short of revolutionary and Java has continued to evolve at tremendous speed. While writing this, the latest version of Java is 9. Soon after release of Java 1.0, Java 1.1 was released. It added new library elements as well as deprecated several features from 1.0. The next major release was Java 2. It had internal version number 1.2 due to the version of libraries being packed as 1.2.  With Java 2, Sun repackage Java Product as J2SE (Java 2 Platform Standard Edition), and the version numbers started to get applied to the entire product. J2SE 1.3 added more functionality to the existing package. Source-code compatibility with previous versions was major highlight. J2SE 1.4 contained several important upgrades, enhancements and additions. For example, it added chained exceptions, channel based IO system, changes to networking classes and Collection framework. With this version also, source-code compatibility with pri

What is Java?

Image
Java is a programming language and a platform . Java is a high-level, robust, secured and object-oriented programming language. Platfom : Any hardware or software environment in which a program runs, is known as a platform. Since Java has its own runtime environment (JRE) and API,  it is called platform. ByteCode : The output of a Java compiler is not a executable but it is a bytecode . ByteCode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is Java virtual machine (JVM). Translating a Java program into byte code makes it much easier to port it to different environments since we need JVM to be implemented for whatever platform we intend the program to run. Although the details of JVM may differ from environment to environment, all understand the same byte code. If a Java program was not compiled to byte code but was instead compiled to native executable code, then we would have to create different executable versions