Tuesday, March 13, 2007

Anatomy of the first program!

Generically speaking any Java program has the following structure...

package x.y.z;
import a.b.c;

class classname{
public static void main(String args[]){
}
}

Text in italics above are all optional. We didn't have any package and import in our program... did we?

We'll talk a lot about packaging and imports later. But for now, let's take a look at the first program we wrote.

public class Receptionist{

void meet( ){
greetVisitor( );
}

void greetVisitor( ){
System.out.println(“Hello Guest! Welcome!”);
}

public static void main(String args[]){
Receptionist receptionist = new Receptionist( );
receptionist.meet( );
}

}

We have a class named Receptionist indicated by

public class Receptionist{
-----
}

But what is this public all about? Again this is something related to packaging and access modifiers which we would cover in detail later. For now, think of the public key word as something that is used to make the class... well... public i.e. accessible to everyone.

Next come methods. The template for any method is

( ){ }

Again this is a generic template and there are a few exceptions to this syntax which we'd see later. But for now be informed that every method should have a return type, a name and the parantheses following the name.

void meet(){
----
}

void visitor(){
----
}

public static void main(String args[]){
------
}

are all methods in our class. The idea of having void should be clear now. When a method need not return anything, we declare the method as void.

So we got a new question. What is meant by returning? Returning what and to whom? Why should a method ever return something? The answer is pretty trivial. Let's imagine that you need a method to find out (NOT PRINT) the sum of two numbers. You might write say c= a + b; and the addition is done. But how does the caller (someone who wanted this method to add up two numbers) know the result? Afterall isn't that the only reason the method was called? That is precisely when you RETURN something from your method. In our case we would return a number.

For now, the code shouldn't appear as strange as it did when you saw it the first time. As we go on, you should be pretty comfortable with all the basic aspects of a Jav program. Nevertheless, keep thinking about the other questions and their possible answers. That would help you understand the system better.

Tired of all this coding stuff? Chill out at http://forwardszone.blogspot.com

Monday, March 12, 2007

Let's meet our own receptionist!

If your solution to the previous exercise is something like what is given below, you are perfectly on track.

1. Our receptionist object needs to greet us when we meet it. In simple terms any verb in a sentence translates to a method or a step in a method. So our Receptionist class should have a meet method which does the job of greeting.
2. We create an instance of this class and name it say receptionist
3. We invoke the meet method in the receptionist object to greet us.


Here is the actual code that does what we want.

public class Receptionist{

void meet( ){
greetVisitor( );
}

void greetVisitor( ){
System.out.println(“Hello Guest! Welcome!”);
}

public static void main(String args[]){
Receptionist receptionist = new Receptionist( );
receptionist.meet( );
}

}


Great… now how to run this?

1. Save this in a file name Receptionist.java.
2. Compile the code using javac Receptionist.java
3. Run the program using java Receptionist

Your output should ideally be - Hello Guest! Welcome!

Not sure how to do the compilation or problems in compiling? Checkout http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html for setting up the execution environment.

If you take a closer look at this program, there are a lot too many questions to ask. Let me list down whatever I see as a first time Java programmer. You can add questions if you have any.
1. What does void stand for?
2. What happens in System.out.println?!
3. What is this main( ) method? Does it have any significance?
4. why is it termed public static void main( ) while other methods are termed just void functionName( )
5. Am I reading the code correctly? Are we creating an object of Receptionist inside a method in Receptionist? How is this possible?
6. Can I have any name for my variables? Wont the receptionist object be confused with Receptionist class by the compiler?
7. Why is there a limitation on the file name?
8. Should I always mark my class public? What does it mean?
9. If I have the greeting line in greetVisitor( ) method why should I have another method meet( ) where we call the greetVisitor( ) method?

For now, I leave it to you to answer these questions. Don’t skip these questions. Even if you can’t answer them, at least guess before you continue reading.

Let's jump in...

Even though there is a lot of theory left to completely understand a java program; many of us prefer practical learning. So let’s write our first java program and then take a closer look at it.

As I told earlier, there are three basic steps in getting things done in an object oriented language.

 Defining what you want out of an object – creating the class
 Constructing the object – instantiation of the class
 Doing whatever you need to – invocation of methods on the object.

Let’s assume that we want an object to greet us whenever we meet it. Think about it. What should be done to come up with such an object? List down the steps involved in creation of this object.

1.
2.
3.

HOLD ON! Please think about the implementation before reading further. This is not an optional exercise. If you couldn’t think about it, re-read the previous posts and attempt it. The solution follows in the next post.

Wednesday, March 7, 2007

But you mentioned something called a JVM!

Oh yeah. I did mention about the Java Virtual Machine (JVM) in the previous post. The JVM can be termed as the platform on which your code actually runs. In other words, JVM is the core component of the Java Runtime Environment (JRE). Strictly speaking you need not have an in depth idea on JVM to be a good Java programmer. However knowing the details about the JVM doesn’t hurt either. So I leave the choice of skipping or reading this post to you!

A java virtual machine (JVM) is, by its name, a virtual machine that is built on top of the underlying hardware and operating system. It is just another process from the perspective of the operating system. It is loaded and executed by the operating system.

From the perspective of a java program, the JVM is (almost) every thing they have. Once it is loaded by the operating system, the JVM loads the java program, execute the main method just like the operating system loads and executes an executable.

An abstract computing machine, or virtual machine, JVM is a platform-independent execution environment that converts Java bytecode into machine language and executes it. Most programming languages compile source code directly into machine code that is designed to run on a specific microprocessor architecture or operating system, such as Windows or UNIX. A JVM -- a machine within a machine -- mimics a real Java processor, enabling Java bytecode to be executed as actions or operating system calls on any processor regardless of the operating system. For example, establishing a socket connection from a workstation to a remote machine involves an operating system call. Since different operating systems handle sockets in different ways, the JVM translates the programming code so that the two machines that may be on different platforms are able to connect.

WAIT A MINUTE! DIDN’T YOU SAY JVM IS PLATFORM DEPENDENT!



Hmm… yeah! I did. JVM is platform dependent in that the JVM used is not common across platforms. If you need your code to run on UNIX, you need a JVM for UNIX. But the way in which JVM executes the code is platform independent. Confusing huh? :) Let’s take up an example.

Mrs. Samuels had two sons Tom and Harry. Since the family was in poverty, she told her sons that they need to bring $100 each within a week to sustain their living. Tom being a hard working guy took up a lot of part time jobs and earned $100. Harry being a lazy guy with a good deal of friends told his situation to his friends and got the money from them. By the end of the week, the instruction given by Mrs. Samuels and the end results were consistent. However the way Tom and Harry IMPLEMENTED them is totally different. In the same way the instruction set for a JVM is constant across platforms. But for getting things actually done, platform specific features are used and hence we need separate JVM for separate platforms.

There are two components of JRE - Jasper and Jasmin which might be worth mentioning while we are on the topic.

Jasper is a program to read Java class files in binary byte code format. The program is capable of generating ASCII files which can be used in conjunction with the Jasmin Assembler. Jasper is also intended to generate input into a class file browser that can produce the inheritance hierarchy and composition maps from the java class files.

Jasmin is an assembler for the Java Virtual Machine. It takes ASCII descriptions of Java classes, written in a simple assembler-like syntax using the Java Virtual Machine instruction set. It converts them into binary Java class files, suitable for loading by a Java runtime system.

Even if you don't understand much from this post, never mind. This is just an additional information that you might be interested in knowing.

Feel it's high time for a break? Checkout http://forwardszone.blogspot.com

Tuesday, March 6, 2007

WORA – Write Once Run Anywhere

WORA (Write Once Run Anywhere) was a revolutionary feature introduce by Java. Also called platform independence, this feature allows you to write code that works in all environments without modifying a single line….yes a SINGLE LINE.

If your mind questions “What is the big deal about this? How is the programming language connected to a platform anyway?”; then let’s revisit the process of compilation and execution. A compiler in YOUR machine running on YOUR operating system is intended to translate the code YOU write into the language YOUR machine understands. Doesn’t it just make sense? When in meeting with a German, you need a German translator and if your guest happens to be a Russian you need a Russian translator. But what if you have a person who can translate your sentences into any language you want! That is precisely what Java offered. YOU write your code and Java ensures that your code runs ANYWHERE.

How is this possible? How is Java compilation different from other languages? Try getting the idea from the comparison diagram below.



You should’ve got an idea by now. But let me give in another hint to make the understanding better. Imagine that the interpreter you have writes down whatever you dictate in some code language that all his colleagues understand. So depending on your guest, the correct translator would be put in place and he would take up the notes from your translator and convey your message to the client. This way you have just one translator who shares just one language with you and hence irrespective of your guest, your translator and you are going to speak in the same way.

This is precisely what happens in Java. The Java code you write is translated by the Java compiler into something called a BYTE CODE. You could think of this byte code as the code language in our translation example. This byte code is taken up by the Java Virtual Machine (JVM) and the JVM translates this byte code into the platform dependent code. So ultimately the JVM is PLATFORM SPECIFIC. In other words, you will have a JVM for Windows, a JVM for Linux and a JVM for Mac. But all these JVMs work on the common format – the byte code and translate that to the OS specific machine code. This way you are free from platform specific code and you WRITE your code ONCE and RUN it ANYWHERE.

Wanna Relax? Checkout http://forwardszone.blogspot.com.

Monday, March 5, 2007

Let's talk bout Java!

Okay… now that we know what objects and classes are, at least from a bird’s eye perspective (of a bird that is 100, 000 m up in the sky :)) let’s see how Java works with OOP from the same height.

Let’s revisit the three steps that are involved in the whole process of OOP.

1. You come up with a description on how your object is supposed to be or what are all the features that your object should have. (I’m sure you could remember the CAD for your dream house). In other words, you define the class.
2. Create the actual instance of this class. You construct the house. Technically, create an object.
3. Do whatever you want with the object. With a house you typically get expensive furniture to add to the grandeur, probably hang a beautiful painting in your drawing room, paste all your favorite clips in your bedroom and so on.

So far so good… so how do you do it with Java?

1. Create a class. A Java class is typically declared using the following syntax:
class MyDreamHome{



}

Pretty straight forward. You declare a class with the best keyword you could think of – “class”. You give in the name of your class after the “class” keyword with a space separating the class name and the key word. If you have any previous programming experience, you must know about blocks. A block is a set of statements clubbed together into… well… a ‘block’ of code. In Java blocks start with a ‘{‘and ends with a ‘}’. A class is a block and when you use the syntax above you say there is a block between ‘{‘ and ‘}’ AND it is a class (there are many varieties of blocks and class is one of them) AND it is called MyDreamHome. Hope I’m not confusing.

2. Second step is to instantiate the class. In other words, I actually create a new object that corresponds to my class definition. Yet again the creation of this new object is done using a spectacularly simple keyword ‘new’. If I need to create a new instance of MyDreamHome, I’d say

MyDreamHome myHome = new MyDreamHome( );

Let’s see what happens here. When I mention the key word new, it means a new instance. But a new instance of what? I should mention somewhere that I want an instance OF TYPE MyDreamHome. So I give in the class name for which I need to create the instance as the successor to the new key word and of course separated by a space. I could hear you saying “Fine. But what is the ( ) after the class name all about?!”. While creating an object, we are actually performing some action and when I perform an action, there is always a method associated with it and whenever a method is involved you always have parentheses ‘()’. Let’s reserve the definition of methods and members for now and assume that method is something that should be used for any actions performed in your program. The part of the expression before the ‘=’ sign should be familiar to you. We just give the object ‘a name’. Well… isn’t that typically what you do when you own a house? You consider things complete only when you hang out a board saying “John enclave” to mark that this is ‘your’ home. Don’t you? :) That is exactly what we do. We give the object a name (myHome) and we say it is of TYPE MyDreamHome.
3. We use the dot operator ‘.’ to get something from or do something on an object. For now forget about what this statement exactly means. But just have in mind that whatever you want to do something on an object or whatever you want from an object, it has to be done/got through a dot that follows the object name. for example myHome.xxx

If you are able to at least vaguely understand how object oriented programming works, it should be good for now.

Chill out at http://forwardszone.blogspot.com.

I’m new to programming and I don’t understand a letter!

If you are already familiar with atleast one high level language, this post is redundant for you. You can very well skip this and move on to the next post. Nevertheless there is a very good chance that you missed out something that you could probably get to know from this post.

One of the most popular definitions of a program is “the act of creating software or some other set of instructions for a computer”.

Strictly speaking explaining programming and the concepts of programming in and out is out of scope of this post. All I provide here; is the basics that you NEED to know to understand any language.

Just like we human beings have English, German, French and Spanish, computer has its own language the machine language. Provide it with a series of ‘meaningful’ 0s and 1s and your computer is more than happy to respond. However even the simplest word in this language might drive you crazy. Imagine writing something like ‘11101001001010010100100100100101001010’ for adding two numbers say 100 and 200. This is not at all convenient for a programmer. Imagine a complex application that requires a million lines of code written using this low level or machine language.

So what is the solution? We don’t understand bytes and a computer doesn’t understand anything else. So what is the solution? Have a middleman who can talk both the languages and that is precisely what we do. The middleman in this case is called a compiler or an interpreter where a compiler is like a block translator who waits for the whole message to be completed and then translates it; while an interpreter tells you the line by line meaning.

A program is typically composed of:

Variables – memory locations where you store the data required by your program
Operators – manipulations available on a particular data. For example numbers can be added or subtracted and hence addition and subtraction are operations associated with numbers.
Expressions – typically a meaningful combination of variables and their operators resulting in a value.
Delmiters – most languages employ a delimiter to mark the end of each statement. In other words the compiler/interpreter needs to understand; which is the first and which is the second line.
Grammar – the legitimate ways in which the operators and operands can be combined to form expressions. It’s quite similar to the sentence formation rules in human languages. For example “add one and two” makes sense while “two one and add” doesn’t even though they contain the same set of words and all of them have a meaning when they stand alone.

I believe this basic understanding should be good enough to proceed with. If you are looking for more details on programming, I’d recommend you to browse the web, get a clear idea of programming and then continue with the subsequent posts.

http://en.wikipedia.org/wiki/Computer_programming does provide some information on programming and its history.

Curious about software testing? Checkout http://testinghandbook.blogspot.com

Sunday, March 4, 2007

OOPS! Objects are everywhere!

If you are a Java beginner, the first thing probably you would've heard about Java is that...hmm... it's an Object Oriented language. Not to mention the appropriately named phrase that revolves around this - 'OOPS' concepts.

So what is this all about? What the heck is an Object Oriented language and what is OOPS? Surprisingly the seemingly complex concept is so trivial and it is the way we have been perceiving things for years. There is a huge similarity between human perception and Object Oriented Programming (OOP) and that makes the concept so popular.

Object Oriented Analysis is viewing the world in terms of classes and objects while Object Oriented Design is the way you link these objects and define your world.

Ok... Now tell me whatever you see in the picture below.



Telling out TV, Dart, Bear... Congratulations... U already know Object Oriented Analysis. Feel like telling me... R u nuts?! Well... I'm not. This seemingly trivial picture explains how we are inherently good in Object Oriented Analysis. Yes... We always view the world as a collection of objects. a TV, a bear and so on which would indeed translate to a TV OBJECT, a bear OBJECT, etc.

In short things that you can think of and associate to some activities are all objects. And this means a BIG thing. Not all objects are visible. For instance, God Himself can be considered an object (though He is not apparently visible) and His activities include creation, protection and destruction to say the least. I would be a sinner if I don't give you the most popular definition of an object at this point. An object is an instance of a class.

I could hear you saying... "Great... So what is a class now?". So what does this mean? Before actually constructing your dream house, you plan your house, you decide the number of rooms in it, the color, the floor and everything related to it. You come up with a wonderful CAD showing each and every detail of your house and boy you are so happy dreaming about it. But where is your house? A mere drawing can't be your house even though it clearly says how your house is supposed to be. The non-concrete abstract definition of your house that you have in your hand is the class. When you actually construct the house in flesh and blood (or rather stones, wood and concrete), that is where you instantiate or create an actual entity that is built as per the specifications in your class - the plan.

Hope this post provides a basic information on Classes, Objects and OOPS. We'll look into more details in future.

Feel like relaxing a bit? Checkout http://forwardszone.blogspot.com.

Saturday, March 3, 2007

Let's get started!

Dear Reader,

This is an “all under one roof” site for Java* that provides tutorials, certification tips, mock exams, interview questions, information on job openings for Java developers and even more.

This first launch is confined to core Java concepts and with your continued support, I plan to make it a full-fledged J2EE* guide that covers core J2EE as well as the frameworks, tools and utilities used in real-time J2EE development projects.

I sincerely hope you have a great time with this site. Should there be any suggestions, feedback or comments, please do write to me. I’d be delighted to hear from you.

Regards,
Moderator