Pages

Saturday, May 29, 2010

Declear a class with one method


public class MainClass
{
public static void main( String args[] ) //main
{
GradeBook myGradeBook = new GradeBook(); //new for allocation memory

myGradeBook.displayMessage(); //calling of a method.
}
}

class GradeBook
{
public void displayMessage()
{
System.out.println (" Welcome to the Grade Book...");
}

public class MainClass
{
public static void main( String args[] )
{
GradeBook myGradeBook = new GradeBook();

myGradeBook.displayMessage();
}
}

class GradeBook
{
public void displayMessage()
{
System.out.println( "Welcome to the Grade Book!" );
}

}


}

creat an object of a class


class Circle
{
double radius; // Radius of a circle

Circle()
{


} // Class constructor

Circle(double theRadius) {
radius = theRadius; // Set the radius

}

}

public class MainClass {
public static void main(String[] arg) //main method
{

Circle sp = new circle();

}

}

Declear a class in java


Classes are the fundamental building blocks of a Java program. You can define an Employee class as follows:

class Employee {
int age;

double income;
}
  1. By convention, class names capitalize the initial of each word.
  2. For example: Employee, Boss, DateUtility, PostOffice, RegularRateCalculator.
  3. This type of naming convention is known as Pascal naming convention.
  4. The other convention, the camel naming convention, capitalize the initial of each word, except the first word.
  5. Method and field names use the camel naming convention.

program of try and catch..

public class MainClass
{

public static void main(String args[])
{

int d, a;

try
{
d = 0;
a = 42 / d;
System.out.println("This will not be printed.");
}
catch (ArithmeticException e)
{

System.out.println("Zero cannot divide any no.");// we can give any message
}

System.out.println("After catch statement.");
}
}

loops

In java there is similar loop as we study in c/c+.

such as
while()
do while()
for()

and condition statement are
if().

Operator


In Java, there are six categories of operators.

  1. Unary operators
  2. Arithmetic operators
  3. Relational and conditional operators
  4. Shift and logical operators
  5. Assignment operators
  6. Other operators

Primitivie data type


Java has eight primitive types of data: byte, short, int, long, char, float, double, and boolean.

These can be put in four groups:

  1. Integers includes byte, short, int, and long
  2. Floating-point numbers includes float and double
  3. Characters includes char, like letters and numbers.
  4. Boolean includes boolean representing true/false values.