Sunday, November 28, 2010

Passing parameters in a method

Always remember in Java, there's only pass by value. Pass by reference does not exist, full stop.

class A{
public static String commonStore = null;

static void keepCheckingIfValueIsNotNull(String value){
while(true){
if(value!=null) {
break;
}
else S.o.p("loop continues");
}
}
}
class B{
//lets do a call to check commonStore.
keepCheckingIfValueIsNotNull(A.commonStore);
A.commonStore = "string";
}

Will the above break the loop. It will not because, we're only passing the value of the variable, not the reference, itself.
To make this work, we should:
if (A.commonStore!=null) break;

So, what's the confusion. That's next.

Friday, August 27, 2010

Session 5:Interface

One of the most interesting and intriguing concept that is an absolute necessity if you are to understand Object Orientation principles is an interface. The main usage is around re-usability and minimum change necessary when adhering to a contract.

What is it?

An interface is a contract. There may be several implementations of an interface.

Analogy:
I want to use a service provider for my tv.
I have paid BT and they deliver their "custom" set-top box to use.

In 6 months time I am fed up with BT and want to switch over to Sky.
Obviously I can't use the same set-top box and I have to pay for the sky set-top box.

What if I had one set-top box which spoke to all these providers?

And that is the interface. A set-top box which is a common contract for all service providers.
It is a specification/standard which has to be adhered by the service provider.

In language terms it is a service contract which provides specifications or method declarations.

Your service providers are to implement the interface and adhere to the contract, so that as a user of that service, switching over is easier and reduces complexity and cost.

Example:
Service:
public interface Jms{
public void storeMessage(String message);
public String getMessage();
}

ServiceProvider 1 [ActiveMQ]:
public class ActiveMQProvider implements Jms {
private Message message = new Message();
@Override
public void storeMessage(String message) {
message.store (message);
}

@Override
public String getMessage() {
return message.retrieve();
}
}

ServiceProvider 2 [IBM-MQ]:
public class IbmMQProvider implements Jms {

private String message;

@Override
public void storeMessage(String message) {
this.message = message;
}

@Override
public String getMessage() {
return this.message;
}
}

Service Client:

public class JmsClient {

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

private static void useJmsService() {
Jms jms = new IbmMQProvider();
//To switch service: comment the previous line and un-comment the next line
//Jms jms = new ActiveMQProvider();
jms.sendMessage("Message in a bottle");
System.out.println(jms.getMessage());
}

}

Benefit:

The benefit/advantage is from the client's perspective. It is the user of these services who gains from it all. To switch services you dont have to re-do your entire Service Client code base. Just a change in ServiceProvider will do. Well that should be the aim anyway. Because in the real world nothing is too simple.

Wednesday, August 25, 2010

Session 4: More on data types - primitives

Java is a statically typed language, which means you have to declare the variable before defining/using it. There is a difference between declaration and definition of a variable.

Take the following case:
//declaration
Object object;
//definition
object = new Object();

Unless you define a variable using it may result in a null pointer exception.

There are broadly 2 types of variables.
Primitives and Object variables.

Primitives are a group of commonly used datatypes provided in java.
8 types of primitives(size defined in bits) are listed.
byte(8), short(16), int(32), long(64), float(32), double(64), boolean(1) and char(16).

int i = 10;

There are object wrappers in Java for these primitives .

Integer integer = new Integer(10);
int i = integer.intValue();

Since numerical variables are used very often, using them as primitives provides greater performance benefit compared to object wrappers.

Saturday, March 25, 2006

Session 3- Access Modifiers

Access Modifiers:

Access Modifiers are key-words which help you to define the level of access to a particular variable or method.

The basic modifiers are:
private
protected
No Access Specified
public

private variable/method means the highest level of restriction.
The private entity cannot be accessed by any other Class apart from the Class in which it was created
Its like a man's shaving kit.


protected variable/method is the next level in restricting access.
The protected entity cannot be accessed by any other Class apart from the Class in which it was created and the Class which has inherited it.
Lets put a note on inheritance here. By inheriting a Parent Class the Child Class gets all the properties and methods which are above the level of private restriction.
Its like the child inheriting parents genes.

No access specifier is mentioned for a variable/method means package level restriction.
If no access specifier is mentioned for a entity it is accessible by any Class with in the same package.
Its like a friend having access to you.

public variable/method is the last level in access.
The public entity is accessible to all Classes across all the system.

Scope Modifiers:
Final
Synchronized
Transient
Volatile

Final modifier means it is the end of hierarchy in the inheritance tree.
The final entity[Class,method/variable].
A final class cannot be inherited.
All variables in a final Class are final variables.
To define a Constant in java you have to specify final as the key-word.
A final method cannot be inherited-so cannot be overridden.

Synchronized modifier is used while doing multi threaded code.
The synchronized entity will not allow more than one thread to possess it at a time.

Transient modifier is only applicable for variables.
The transient variable will not be written when a class is serialized.
Serialization will be covered later.

Volatile modifier is only applicable for variables.
The volatile variable is asynchronous for value assignments.

Transient and Volatile are rarely used.


Next session is an extension of Access Modifiers, variables and data types...

Wednesday, December 14, 2005

Session2-Classes and Objects

Classes:
Class in Java has data and functions that operate on those data.
In writing a Class we only describe it.

Classes use the concept of abstraction and are defined as a list of abstract attributes.
By using access modifiers Data can be made inaccessible to the outside world and only those functions which are stored in

the class could be made to access them.

Objects:
They are instances of a class.
Class is the blueprint while Object is the live model built from the blueprint.
We don't create classes in memory but when we instantiate a Class then it is called an Object and it is part of memory.

Example:

A class will have a data part and a functional part

Class Person
{
//Data part
organs eyes;
organs ears;

//functional part

see()
{
//implement seeing with your eyes.
}

hear()
{
//implement hearing with your ears.
}

}

Until now this is just a blue print. When you want to make a real person you would create a real model from this blue print which is called an Object.

Person person1 = new Person();

In the above example we have combined the data part and functional part in a single

entity[Class] which is a blue print of what we want. This is encapsulation.

We can have another Class which can inherit the properties of this Person.
That is inheritance.

In next session we will look at Access Modifiers, variables and data types.

Wednesday, December 07, 2005

Session 1 - Object Oriented Concepts

Object Oriented Concepts help programmers to translate real life situations[logical] in to computer applications.
The main concepts are given below.

Data abstraction
Abstraction refers to the act of representing essential features without including the background details or explanations.
Nutshell:
Expose on a need to know basis.
Example:
In a mobile phone to make a call all we do is to enter the number and press call/ok.
we don't need to know the technology of how it works.

Information Hiding
This is a design consideration. All data and internal implementations to be hidden from client view.
Nutshell:
Secrecy needed for data protection from data accessors [clients].
Example:
That is to fortify your defences against any client intrusions on your castle.
Subtle difference between abstraction and hiding.

Encapsulation
Storing data and functions in a single unit is encapsulation.
The idea is to combine the data part and the functional part of a single unit.
This is essentially a language facility. Java offers this facility.
Nutshell:
All related items in One place
Example:
It is like storing all your food items in the kitchen so that it helps you with easy access while cooking rather than having a separate store room.

Inheritance
Inheritance is the process by which objects can acquire the properties of objects of other class. In OOP, inheritance provides reusability, like, adding additional features to an existing class without modifying it. This is achieved by deriving a new class from the existing one. The new class will have combined features of both the classes.
Nutshell:
Like Father Like Son
Example:
Best example is people inheriting their genes from their predecessors.

Polymorphism
Polymorphism means the ability to take more than one form. An operation may exhibit different behaviours in different instances. The behaviour depends on the data types used in the operation. Polymorphism is extensively used in implementing Inheritance.
Nutshell:
One God Several Faces
Example:
A person exhibiting different characteristics while being in different moods.
But still he is the same person.

Advantages of OOP
Object-Oriented Programming has the following advantages over conventional approaches:

OOP provides a clear modular structure for programs which makes it good for defining abstract data types where implementation details are hidden and the unit has a clearly defined interface.

OOP makes it easy to maintain and modify existing code as new objects can be created with small differences to existing ones.

OOP provides a good framework for code libraries where supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces.

NOTE:
Information hiding is not only confused with encapsulation, it is also often confused with abstraction.
Abstraction is a technique that helps us identify which specific information should be visible, and which information should be hidden. Encapsulation is then the technique for packaging the information in such a way as to hide what should be hidden, and make visible what is intended to be visible.

Next Session:
Classes and Objects.

Tuesday, December 06, 2005

Learn Java for free

Friends,

I'm a J2EE developer with a reputed company.I work in UK. I have been programming with Java and J2EE for the last 6 years.
When I started learning Java I was recommended with many books.I had a problem doing it that way.Most of the books were just talking about Foo and Foo.I didn't have an iota of idea about what was Foo an why they were talking about Foo.I couldn't read more than 2 pages a day.Sometimes it is very hard to push past even 1 page.So I thought may be I can make it easy.
All I'm going to do is to teach Java in a very simple manner.Most of the examples will be easy enough to understand relating them to real situations.I want to make this a fun session.We will have one session a week.I hope you will like this effort of mine.
Please feel free to add your comments.

Thanks in Advance for your support.
Pazham