Use of Command line Arguments
class Cmdline
{
public static void main(String args[])
{
int count,i=0;
String s1;
count=args.length;
System.out.println(“No.of Arg=“+count);
while(i<count)
{
S1=args[i];
i=i+1;
System.out.println(i+ “=:”+s1);
}
}
}
Chapter -3
Data types variables and Arrays
By:A.J.Patankar
Data Types
Data Types
Primitive Non-Primitive
Classes Arrays
Numeric Non Numeric
Interface
char
boolean
int float
Variables
A variable is a named memory location
that can hold a value.
Format to declare variable. type varName
Type Desc. Keyword
Character 16 bit char
Boolean True/false boolean
Byte 8 bit byte
Short 16 bit short
Integer 32 bit Int
Long 64 bit long
Float 32 bit float
Double 64 bit double
Types of Variables
1. Primitive Variables: A value of primitive
type holds value of that exact primitive
type.
2. Reference Variable: A variable of
reference type can hold
a null reference
A reference to any object whose class is
assignment compatible with type of
variable.
Kinds of Variables
Class variable
Instance variable
Array Components
Method Parameters
Constructor Parameters
Exception Handler Parameters
Local Variables
Scope & Lifetime of Variables
Java allows variables to be declared within any
block.
A block defines a scope of variable
There are 2 types of Scope
1:Class Scope & 2: Method Scope
A variable declared inside the scope is not
visible outside the scope
Variable declared inside becomes localized and
protect it from unauthorized access and
modifications
Two variables one in inner scope and one in outer
scope cannot have same value
Example #1
class ScopeDemo
{
public static void main(String args[])
{
int a=2;
if(a==2)
{
int b=4;
System.out.println(“a=“+a+”b=“+b);
}
b=10; //An error
System.out.println(“a=“+a);
}
}
Example #2
class TypePromotion
{
public static void main(String args[])
{
int i;
float f;
i=10;
f=23.25f;
System.out.println(i*f);
}
}
One Dimensional Arrays
One Dimensional array is a list of
variable of the same type that are
accessed through a common name.
An individual variable in the array is
called an array element.
Format : type varName[ ]= new type[size];
Ex: One Dimensional Arrays
class ArrayDemo
{
public static void main(String args[])
{
int myarray[]={33,71,-16,45};
System.out.println(“myarray.length=“+myarray.length);
System.out.println(myarray[0]);
System.out.println(myarray[1]);
System.out.println(myarray[2]);
System.out.println(myarray[3]);
}
}
Multidimensional Arrays
It is arrays of arrays
To declare multidimensional array
variable specify each additional index
using another set of square brackets.
example:
int numbers[ ][ ]=new int[3][5] ;
Multidimensional
class TwoDArray
Arrays
{
public static void main(String args[])
{
int marray [ ][ ] =new int[3][2];
marray[0][0]=33;
marray[0][1]=71;
marray[1][0]=-16;
marray[1][1]=45;
marray[2][0]=99;
marray[2][1]=27;
System. out.println(“marray.length=“+marray.length);
System.out.println(marray[0][0]);
System.out.println(marray[0][0]);
System.out.println(marray[0][0]);
System.out.println(marray[0][0]);
System.out.println(marray[0][0]);
System.out.println(marray[0][0]);
}
}
Type Conversion
In java it is possible to assign value of
one type to variable of another type
Example:
int i=260;
byte b=(byte) i;
double d= 1071.89;
byte db= (byte) d;