Get Adobe Flash player

Monday, December 26, 2011

Structure of a Java Program

Structure of Java Program ...

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 programThe first line class Sampledeclare 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 requiredIn Javaclass SampleCPPclass{//Body of the...

Page 1 of 612345Next