Tuesday, 26 May 2009

Variables

Here we would be talking about variables

Every programming language has variables. A variable can be defined

as a named storage location whose value can change during the

execution of program .

General declaration syntax is :

data type variable name = [initialization value]

eg int number ; // declaration without initialization

int number = 10 ; // declaration with initialization

  • Initialization is optional
  • Once declared other parts of program (code) can refer to it by it's name

Scope and Lifetime of a variable

Scope means which part of program can access a previously defined variable.

Normally, scope is defined by curly braces. We will explain scope rules with the help

of following examples.

Consider a for loop and a function

for ( int count = 0 ; count <>{

int i = 5 ;

System.out.println ("Value of i is : " + i ) ; // prints on console

}

void other ( )

{

System.out.println ("Value of i is : " + i ) ; // results in compile time error

}

As variable i is defined inside the for loop, enclosed in the braces it can be used

only there.Trying to use it anywhere else would result in compile time error

Variables declared inside ( ) can also be accessed within the following braces.

In the for loop above, variable count has been declared inside the ( ) and can be

accessed only inside the for loop .

Nested scopes



It is possible to embed another block defined by { } inside another block

Lets look at an example on how scope rule works when there are nested scopes

for ( int i = 0 ; i <>

{ // start outer

// j is not visible (accessible here)

for ( int j = 0 ; j <>

{ // start inner

// i is accessible here

} // end inner

} // end outer

Lifetime : As soon as a variable goes out of scope, it's value is destroyed because it can not

be used after it goes out of scope

for ( int i = 0 ; i <>

{

int x = 10 ;

x = x * 5 ;

System.out.println (" Value of x is : " + x) ;

}

Output is :

Value of x is : 50

Value of x is : 50

Value of x is : 50

This is because every time, next iteration of for loop occurs x goes

out of scope ( as code execution moves out of { } ) . So it's value gets destroyed.

It again gets initialized to 10 when the loop is entered .

Next we will be discussing about Arrays

Data Types

Data Types
Every language has some data types so does Java
Data type defines the behaviour of variable on which it is applied
Different data types defined in Java language are
Integer types (4)
Name width (bits) Range
byte 8 -128 to 127
short 16 -32,768 to 32,767
int 32 -2,147,483,648 to 2,147,483,7
long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807


  • All these types can hold non fraction values only
  • Range is the valid number which can be assigned to them

If a byte is assigned 150 , compile time error would be there

Floating types (2)

Name width (bits)

float 32

double 64

  • These are used to hold values with fractional part
  • Any fractional value is treated as double type by default by compiler

Character type

char : It is a 16 bit type used to hold single character enclosed in single literals ' '

boolean : This type holds either true or false only.

  • These are primitive types,corresponding to these are wrapper types which

will be discussed later

  • String data type is used to hold sequence of characters like world enclosed in double quotes " "
  • Moreover, In Java each class represents a data type

Let's move on to variables




Explanation of first program

Here is a brief explanation of the first sample program
Code
class first
{
public static void main ( String args [] )
{
System.out.println ("First Java program") ;
} // main function ends
} // class ends

class : It is one of the Java keywords.It specifies the beginning of
a new class which contains the code to execute.
main : It is a function.It is the entry point for a Java desktop application,
execution will begin with this function
public : It is one of the access specifiers in Java, a keyword
void : It is return type of function.Every function in Java must

have a valid return type.

String : It is one of the data types

args : It is a argument which stores value passed to a function

We will be giving details of all the above elements later on.





Monday, 25 May 2009

Running a simple program

Here you would learn how to write and run a simple Java program .
First of all, you have to set your environment. It includes
1 Setting the path variable
Go into the directory where you have installed Java
eg C:\Program Files\Java\jdk1.6.0_07\bin
Copy the corresponding path on your machine
This gives the location of java compiler
Now right click on My computer > Properties > Advanced > Environment variables
Click New in System Variables
Enter path in variable name field
Paste the directory path in Variable value field and click on Ok
Close it
2 Setting classpath

Create a folder which will contain your programs

Copy it's path

Create a new System variable with name as classpath and

value as the path of your new directory

Click Ok and close

3 Sample code

class first

{

public static void main ( String args [] )

{

System.out.println ("First Java program") ;

} // main function ends

} // class ends

You can copy this code in a notepad file with name first and extension .java

and save it in the

folder created above

4 Running the program

Click on Start > Run or Press Windows key and then R

Type cmd and press enter

Type cd path

where path is the location of your folder for programs (same as classpath)

Type javac first.java and press enter

A class file should appear in the directory

Now type java first and press enter

First Java program should get printed on the screen

See Details for explanation of code