Thursday, 17 April 2014

First java program

A First Simple Program

/*This is first simple Java program 
Call this file Example,java*/

class Example 
{
 public static void main(String args[])
 {
   System.out.println("This is a Simple java program");
 }
}
---------------------------------------------
Entering the program
--------------------------------------------------------
Compiling the  program

To compile the Example program, execute the compiler, javac , specifying source file on the command line as shown here

c:\>javac Example.java

The javac compiler creates a file called Example.class that contains bytecode version program. As discussed earlier, the Java Bytecode is the intermedia representation representation of your program that contains instructions that Java Virtual machine will execute
Thus output of javac is not code that can be directly executed.

To actually run the program, you must use the application launcher called java. To do so, pass the class name Example as a Command -line argument, as shown here.
 
c:\>java Example

Output:This is a Simple java program
---------------------------------------------------------
A closer look
--------------------------------------------------------

Monday, 7 April 2014

Java Primitive Data Types - Default Values

TARGET LANGUAGE:JAVA

THEORY:(DATA TYPE:Default Values)

  • Java primitive data types are initialized to some default values when they are declared as class members.
  • While programming in Java you declare and use variables at two places.
  • First, inside a function those are local to that function.
  • And second, as a class member. When a variable is declared local to a function it must be initialized or assigned before its first use otherwise compiler reports an error "variable <variable name> might not have been initialized". But when a variable is declared as a class member or field, it is not always essential to assign a value to the member.
  • In that case class members are initialized to some default values by the compiler. However, leaving class members uninitialized is not considered a good practice.
  • The following table lists the default values for the data types shown in Table 1.
  •  

Sunday, 6 April 2014

Character data type...

 TARGET LANGUAGE:JAVA

THEORY:(DATA TYPE:Character and Boolean)

Character

  • Data type used to store characters is char.
  • Java uses UNICODE to represent characters
  • Java char is 16 bit type
  • The range of char is 0 to 65,536
  • There are no negative char.
UNICODE-
Unicode provides a unique number for every character,
no matter what the platform, no matter what the program,
no matter what the language.
Unicode Table


EXAMPLE:
//Demonstrates char Data types
 
class charDemo1
{
  public static void main(String args[])
  {
    char ch1,ch2;
    ch1=88;
    ch2='Y';
    System.out.print("ch1 and ch2");
    System.out.println(ch1+" "+ch2);
  }
}

//ch1 and ch2:X Y

-Notice that ch1 is assigned 88,which is ASCII(and UNICODE) value of the corresponding letter X
 
class charDemo2
{
  public static void main(String args[])
  {
    char ch1,ch2;
    ch1='X';
    System.out.println("ch1:"+ch1);  
    ch1++;
   
    System.out.println("ch1:"+ch1);
  }
}

//ch1:X
//ch1:Y

-As ch1 is increamented the ASCII value is increamented.Thus second time in the output we get Y. 

BOOLEAN

Java has a primitive type,called Boolean for logical values.
It can have only one of the two values true,false.

EXAMPLE

class Booltest
{
  public static void main(String args[])
  {
   boolean b;
   b=false;
   System.out.println("b:"+b);

   b=true;
   System.out.println("b:"+b);
  
   if(b)//a Boolean value can control if statement
   {
     System.out.print("This is executed");
   }
b=false;

   if(b)//This is not executed
   {
     System.out.print("This is not executed");
   }

  }
}

Output of relational operator in if statement is Boolean value
eg 10>9


---------------------------------------------
In the next publication  we shall deal with literals
---------------------------------------------


Friday, 4 April 2014

Floating-Point types

TARGET LANGUAGE:JAVA
THEORY:(DATA TYPE:Float)

Floating Point Type

Floating point numbers also known as real numbers are used when evaluating expressions that require fractional precision.

Example:

  1. Calculations such as  square root..
Program:

public class CalculateSquareRootWithMathSqrt
 {
 public static void main(String[] args) 
        {       
                float a=30;
                 
  // square root of 16 which equals with 4
  System.out.println(Math.sqrt(16));
   
  // square root of 44 which is 6.6332495807108
  System.out.println(Math.sqrt(44));
  
  // sin of 30 is -0.9880316240928618
  System.out.print(Math.sin(a));
  
  //cos of 30 is 0.15425144988758405
  System.out.print(Math.cos(a));
        }

}

There are two kinds of floating point types Float and double.
Their ranges are specified in the table below.

Primitive  data Types

FLOAT

  • The type specifies a single precision value that uses 32 bit of storage.
  • Variables of float are useful when you need a fractional component but don't require large degree of precision. 
DOUBLE

  • The type specifies double  precision values that uses 64 bit of storage
  • Variables of float are useful when you need a fractional component but  require large degree of precision. 
Program: Area of circle

class Area
{
 public static void main(String args[])
 {
   double pi,r,a;
   r=10.7;//radius of circle
   pi=3.14;//pi,approximately
   a=pi*r*r;//compute area
   System.out.print("Area of circle"+a);
 }
}
---------------------------------------------
In the next publication,we shall see character type and then literals.
---------------------------------------------
After which we shall take a closer look at the simple program...in the next Publication 
--------------------------------------------