Monday, December 26, 2011
A Java program with two classes
class Room
{
float length;
float breadth;
void getdata(float a, float b)
{
length=a;
breadth=b;
}
}
class RoomArea
{
public static void main(String args[])
{
float area;
Room room1 = new Room();//Create an object room1 from the class Room
room1.getdata(15,20);
area=room1.length * room1.breadth;
System.out.println("Area = "+area);
}
}
A Simple Java Program
class Sample
{
public static void main(String args[])
{
System.out.println("Java is pure OOP");
}
}
Now Understanding the program
The first line
class Sample
declare a class, which is an object oriented construct. Java is a pure object oriented language so everything in java must be placed inside a class. class is a keyword and used to declare a new class definition. Sample is a java identifier that specifies the name of the class to be defined.
The Braces { & }
In java every class definition begins with an open brace “{“ and end with a matching closing brace “}” just like in C++. But 1 thing to remember , in C++ class definition ends with a semicolon “;” but in java it’s not.
For example
In C++
class SampleCPPclass
{
//Body of the class
};//Semicolone required
In Java
class SampleCPPclass
{
//Body of the class
}//Semicolone not required
The main method
The line “public static void main(String args[])” defines a method named main. This is similar to the main() function in C/C++. Every Java program must include the main method because this is the entry point for the interpreter to begin the execution of the program. But the Java Applets will not use the main method.
Now the keywords public, static & void
public : The keyword public is an access specifier that declares the main method as unprotected so it can accessible to all other classes.
static: The keyword static declares main method as one that belongs to the entire class and not a part of any objects of the class. In Java the main method must always declared as static since the Java interpreter uses this method before any objects are created.
void : The keyword void is a type modifier states that the main method does not return any value.
The Output Line
The line “System.out.println("Java is pure OOP");” is similar to the printf() statement of C or cout<< construct of C++. Since Java is a pure OOL, every method must be a part of an object. Here the println() method is a member of the out object, which is a static data member of System class.
This program will print the string “Java is pure OOP”
Tuesday, November 15, 2011
Life Cycle of a java program
- A Java program is written using either a Text Editor like Textpad or an IDE like Eclipse and is saved as a .java file. (Program.java)
- The .java file is then compiled using Java compiler and a .class file is obtained from it. (Program.class)
- The .class file is now portable and can be used to run this Java program in any platform.
- Class file (Program.class) is interpreted by the JVM installed on a particular platform. JVM is part of the JRE software.
Web Browsers
- HotJava
- Netscape Navigator
- Internet Explorer
Java as an Internet Language
Features of Java
Difference between Java & C++
Difference between Java & C
Java i s lot like C but the major difference between Java and C is that Java is an object-oriented langugae and has mechanism to define classes and objects. To build a simple and safe language, the Java team did not include some of the C features in Java.
- Java does not include the C unique statement keywords sizeof, and typedef.
- Java does not contain the data types struct and union.
- Java does not define the type modifiers keywords auto,extern,register,signed, and unsigned.
- Java does not support an explicit pointer type.
- Java does not have a preprocessor and therefore we can not use #define, #include, and #ifdef statements.
- Java requires that the function with no arguments must be declared with empty parenthesis and not with the void keyword as done in C.
- Java adds new operators such as instanceof and >>>.
- Java adds labelled break and continue statements.
- Java adds many features required for object oriented programming.
Saturday, November 12, 2011
Basic Concept of Object Oriented Programming
Objects & Classes:
Objects are the basic runtime entities in an Object-Orients System. They may represent a person, a bank, a car etc… Each object has its own properties and behavior. For example if we chose a CAR as an object then its properties will be number of wheels, height, width, length, body material etc, and its behavior will be run, turn left, turn right, turn backward etc..
But in a programming domain objects interacts with each other by sending massages. Like a User account interact with a Bank Account…
In programming view an object is a combination of some data and code. These data and code of an object can be made a user defined data type using the concept of a Class. So what is class?
1. If we taught class as a Data type then objects will be the variable of this Data type.
2. If we taught class as a blueprint of a building then the building is an object.
3. If we taught class as a recipe then the cocked food is an object.