Monday 17 September 2012

What is a Class and Object in Java?

What is a Class and Object in Java?


A class is a blue print to create an object.
Let us try to understand this by an example:
Let us say, we have a feedback form which needs to be filled by all the workers regarding a feedback of a product or application of their organization.
The feedback form may contain the below details
 So, the admin of the organization prepares this form and takes 100 photo copies of the form and distribute to all the 100 workers present in the organization. Once the employees/workers get the form, they fill in the details and return the forms to the admin.

Now the admin have the original form (master copy) from which he/she took the other 100 photo copies and also the 100 forms which are filled by the workers.

If we relate this example with Java, we can term the master copy(blue print) as a Class.
The other 100 forms which are replicated from the master copy are called as objects.



The name, id or any field will not be filled in the master copy so as to use that to take more photo copies in future. So, the master copy won’t be bearing any value to itself. But the photo copies that were taken and filled by the workers have values assigned to them.(name, id, address, feedback of diff workers)
In the same way in Java, a class is a blue print from which the objects can be created and each object have its own value.
If we want to create 1000 employees in java, we create an Employee class and then create 1000 employee objects and each object will have its own value. The object just uses the blue print/structure of the class.

As the Name, id and other fields in the form differ from employee to employee, they are called as variables in java.
Let us say, we need the company name to be printed on the form. Instead of asking the workers to fill the company name, the master copy can be edited such that it has the company name. So, once the photo copies are taken, all the copies will have the same value.




As we want this value to be same across multiple objects, we made the change at the class level and so these variables (like company name) are called as class variables.

The way the admin created the form, let us try to create a class in Java which represents the feedback form.
Class name can be ‘FeedBackForm’
-       As Name, Address, Feedback  are  alphanumeric, they should be String variables in Java
-       As ID is  numeric field, it should be  a Int variable in Java
-       A class is defined in a curly brace with the key word class.
The class of the FeedbackForm can be :



And this should be saved in a file with the name FeedBackForm.java (should be the name of the public class)

Now, as the admin of the organization took photo copies of the master copy and made objects, we will see how to do that photo copy in java.
We create a copy of the class by the following command.
FeedBackForm f1 = new FeedBackForm();
Where FeedBackForm is the name of the class, f1 is the name of the object.If we want to create 100 objects:




The FeedBackForm() you see in the object creation is called as constructor. A constructor is used to create a object in Java. We will know more about the constructors in other sections.



How to compile and run a java file command prompt.

How to compile and run a java file command prompt.? 
What is PATH? What is CLASSPATH?


Compiling:
Create a file LearnSoftOnline.java .



The above program just prints a message on the console.
Place this file anywhere in the local system. Let us say, we place the file in E:/
Let’s assume Java is installed in the location C:\Program Files\Java\jdk1.6.0_33

1)    Open the command prompt and go to the location C:\Program Files\Java\jdk1.6.0_33\bin

2)    Type in java and press enter. You will get the below output. This is just to test that the java command is working fine.


3) TThe java compiler gets invoked by the javac.exe file.  If we want to compile any file, execute the javac command against the file name

 Ex: javac E:/LearnSoftOnline.java and press enter.


Please check the E:/ now. You will find the compiled file with a ‘.class’ extension (Ex: LearnSoftOnline.class)
Executing:
4)    The java files are executed using the java command against the file name (without extension)
Ex: java LearnSoftOnline 


We got this exception because the java command is searching for ‘LearnSoftOnline.class’ file in the current directory (C:\Program Files\Java\jdk1.6.0_33\bin), where that java executable file is present.
So, we need to tell the java executable file about the location of our class file.In the present case, our class file is in the location E:\LearnSoftOnline.class
This path where the class files exists is called the classpath.So when running the java command, we need to tell the java command where our class files are presen by setting the classpath.
The class path from the command prompt can be specified by ‘–cp’.  As our class file is present in the E:/ we need to specify the classpath as -cp E:/
So the command for executing the java class will be
C:\Program Files\Java\jdk1.6.0_33\bin>java -cp E:/ LearnSoftOnline



Once it is executed, we can see the message is printed on the console.

So, every time you want to run or compile a java file, we have to go to the directory where the java executable are present (Ex: In this case, C:\Program Files\Java\jdk1.6.0_33\bin ) and then execute the java and javac commands.
To avoid this, we can tell our machine that java is installed in C:\Program Files\Java\jdk1.6.0_33\bin . This is done by setting the path variable.
Once we add the path ‘C:\Program Files\Java\jdk1.6.0_33\bin ‘ to the PATH environment variable, we can use the java and javac commands irrespective of the current directory location.

Before setting the PATH variable:

After setting the PATH variable:

We can execute and compile the from any directory with out mentioning the java installation directory  




   
If we don’t want to mention the classpath everytime we run the java command, we add the path “E:/” to the CLASSPATH environment variable
Once you add the ‘E:/’ to CLASSPATH, the program can be executed as 





Wednesday 29 August 2012

What is platform In-dependency How is Java Platform Independent?


Before Knowing how the Platform Independence is achieved in Java, let us see what the platform Independence mean.

What?
Platform in the term “Platform Independence” means the Operating System.
By saying Java is a platform independent language; it means the Java software that is used in one operating system can also be used in another operating system without any change to the software. For example, a Java app that runs in windows machine will run fine in Solaris machine and also work in a Mobile without any minor change to the Java software.
Here, with this independence, the programmer develops and builds the source code just once and the same compiled files can be used across platforms (Operating Systems). This was a big breakthrough when Java first came to the market and made it unique among other programming languages of that time(C and C++)

JVM is Java Virtual Machine in which the Java source code runs. To run a Java program in any Operating System, we need to have a JVM. For example, if we want to run a Java program on a Windows Operating System machine, we need to have a Windows JVM installed on that particular PC. In the same way, if I want to run the Java Program on a Solaris machine, I need to have Solaris Compatible JVM and the same is with the Mobile too. If I want to run a Java app on a mobile, I need to install the mobile operating systems compatible JVM.

Once the JVM is installed the same Java program can be run in any of those operating systems. The same Java program works in all platforms the same way. Here if you observe, we have different JVMs for different operating systems but the same Java program for all the operating systems.
To conclude this,
JVM is platform dependant, meaning the JVM is different for different platforms.
Java is platform Independent, meaning the Java source code is same among different platforms.
Many often confuse between JVM and Java and think JVM is even platform Independent. I want to stress on this particular thing that JVM is not platform Independent. It depends on platform. We need to install the compatible JVM in any operating system. For windows, we have Windows JVM, for Linux we have Linux JVM, For Solaris, we have Solaris JVM making the JVM Platform DEPENDENT. But, we use the same Java source code in all the operating systems making Java Platform INDEPENDENT.

How?
Now that we understand that Java is a platform Independent language, we can see now how it was made it that way.
Every operating system takes inputs in the form of Binary(0’s and 1’s).

Let us assume that 100100 represent the word “Hero” in windows operating system. This pattern may not be the same in other operating systems. Let us say, the word “Hero” is represented by “0010” in another operating system (say Solaris).
A Compiler is the one which converts the programming language to machine understandable language(0’s and 1’s).

A C compiler validates and converts the C language to machine understandable language(0’s and 1’s).If we write a Program in ‘C’ to print the word “Hero”. First we need to compile the C program using a C Windows Compiler which gives us the output as 100100. The same program when compiled in Solaris compiler will give us 0010.So once we run this compiled code in windows and Solaris operating systems respectively, we get the desired output. But the drawback of this approach is that the code is compiled 2 times. Once for the Windows OS and once again for the Solaris which is not desirable.

To remove this compiler dependency, the Java compiler is made in such a way that it does not output 0’s and 1’s directly. Instead it generated the byte code. This byte code when run in any JVM is converted to machine understandable language(0’s and 1’s)

So with Java, we write the program and build(compile) it. Once and we get the byte code as a output from the compiler, we run it in Windows JVM which gives the output as 100100 and the same byte code when run in Solaris JVM, gives the output as 0010.So in Java, we are compiling the code only once and using the same complied code among different operating systems(platforms) making it a Platform Independent language.

What is Object Oriented Programming or What is the use of Object Oriented programming?


With the Object Oriented programming language, the programmer can relate the programming to the real world and it helps in breaking the  complex problems to small chunks thus making the program more efficient.

Let us examine this with a basic example.I just want to show how the Object oriented approach makes the programming easy and helps to break the complex problem in to small chunks in the real world and also in the programming Language.

Ex: Programming to represent 'A School'

If the requirement was to design a software which represents a School, we will see how we start up this with the Object oriented Approach.

Everything is treated as an object in the Object Oriented Approach. There are tree main steps involved in the Object oriented approach.

1) Identify different type of Objects
2) Define the state and behavior of each object
3) Define the Object Interaction.


1) Identify the objects that are present in a School

Everything that we see as an entity in a school can be said as object.Some of the objects in a school can be:

Students, ClassRoom, Chairs, Progress Cards, Examinations, Teachers, Principal,Infrastructure, Accountant

There will be lot more objects involved in a school.But to keep it simple, let us limit to the above list.

2) Define the state and behavior of the objects.

State- Tells about the current position of the object. It tells about the properties that are measurable or can be defined.For Example a Student's state will contain his name, ID number, standard, grade,Address

Behavior- The Behavior of any object is the work that particular Object can do.For the same Student Object, the behavior will be "Study", "Play","Go to Home","Come to School","Do Homework"

In the same way let us try to define the state and behavior of all the objects specified in Step 1


Object
State
Behaviour
Students
Name, ID number, Standard, Grade, Address, School Name
"Study", "Play", "Go to Home", "Come to School", "Do Homework", “Give tests”
Class Room
Location, Class Room ID number, Height, Width, Length of the room, Number of students it can accommodate, for which Standard?, in which School?
No Behaviour
Chairs
Model, Make, Colour, Height, located in which class?
No Behaviour
Teachers
Name, Address, Id Number, Subject
Teaches any subject to Students, Give Homework, Verify Homework, Conducts tests
Principal
Name, Address,
Talk to teachers, Manage, Check the quality of education
School
Name, Location



Given the above information, with the object oriented approach, we first build each object.
Create the following
-          School Object
-          Principal Object which have reference to School Object
-          Teacher Object which have reference to Principal Object
-          Student Object  
-          Chair Object
-          Class Room Object containing Multiple Chair Objects
3) Once we create the above objects we can make the objects interact with one another
Like, the Principal object interacting with the teacher object, the teacher object interacting with student objects and so on.

If you observe,In steps 1 and 2,  we made the whole concept simple.Instead of directly programming for a school which sounds complex, we started programming for smaller chunks(Students, Principal, Teacher and so on) and later make these objects interact with one another.


In Other words, first the objects are being made and then they are allowed to interact with each other. This is what a typical Object Oriented programming is. By doing the above breakup and treating everything as an object, it helps one understand and analyse the complex real time problems.

The Object oriented approach has different concepts called as OOPS concepts like encapsulation, inheritance, polymorphism which decorates the above said Object oriented approach and making it more secure, flexible, extensible and usable.









Saturday 25 August 2012

Features of Java

What are the features of Java Language?

This is the basic question that one should know before he/she wanted to learn Java.

We have many other programming languages like C, C++ , .NET and many other.Out of all these, there should be some reasons which made many people to choose Java to develop their web applications or desktop products.

Platform Independence
The main feature of Java which in turn brought a revolution in the programming language is the Platform Independence which is identified by the paradigm "Write Once, Run Any Where" , shortly called as WORA.

Let us say, we designed an application and provided the complete code in Java and complied the code in a machine which has Windows operating system.Once the Java source files are complied, we get the compiled code. This complied code can run in any machine having any operating system.Where as many of the other programming languages are not like this.We need to have different compilers for different operating systems and the code that works in one operating system may or may not work in other operating system.

The details on how this platform independence is achieved in Java will be discussed in future posts.

Object Oriented
 Its the beauty of the Object oriented programming to relate the programming experience to the real world.
Everything in the real world can be treated as object.If the same paradigm is followed in a programming language, it helps the programmer to break the complex functionality to chunks and build up simple solutions.
Java is a Object oriented language thus giving the programmer/code more flexible, effective.

Simple
The Origin of Java is because of the complexities involved in the programming languages like C and C++

Many of the problems/complexities are either made simpler or eliminated in Java.

For example, the memory allocation which is done problematically in C and C++ is taken care by the Java virtual machine, thus making the programmer unaware of the complexities involved in the same.

The operator overloading which sounds a complex functionality in C++ is eliminated in Java

There are many other topic to discuss the differences between C,C++ and Java.We will deal with the same in other posts.


Robust

More stress is laid on the Java compiler to avoid programmatic mistakes.Also, the exception handling in Java is so flexible.Java has huge set of Exception types already defined and the mechanism involved in handling the exception makes the language robust by giving minimal chances for the Systems to crash.

Multi Threaded

If a man takes 10 days to build a wall, How may days will 5 men take to build the same kind of wall?

What ever the answer is for the above question, it will be obviously lesser than 10 days. This is because multiple people are involved int he construction of the same wall at a time making the construction of the wall fast and efficient.

This is what Multi threading mean. It gives time effective solutions.

Java is a Multi threaded  language.The way we achieve Multi threading in Java will be discussed later.For now, we can note that Java gives a option to the programmer to make the program multi threaded when ever desired.


Security:
Java Provides sevaral tools to help you manage access to resources on any system.

For example, Java provides an option to create,store and maintain encrypted passwords by the keystore.

Rich API
Java is very rich in its API.

It has enormous well defined classes which are ready to use by the programmer in their code.

For example, if the programmer wants find the position of the first occurence of the letter "T" in the String "JavaTutorial", he/she need not write a program to find out the same.

Java's String class has defined method which can give the result for you.

The example given above is just the basic one.In the same way, Java has many other in built functionalities already incorporated in its API.

Java is Rich in API and the richness of a programmer in Java is proportional to the knowledge one has on its API.