JAVA
C:\>cd program
files
C:\Program
Files>cd java
C:\Program
Files\Java>cd jdk1.5.0
C:\Program
Files\Java\jdk1.5.0>cd bin
C:\Program
Files\Java\jdk1.5.0\bin>edit
Introduction
Ø
Platform
Independent Object Oriented Program
Ø
Multi-threaded
Programming Language
Ø
1991- Sun
Micro System
The
creation of Java
Ø
Java was
conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, Mike
Sheridan
Ø
Initial
Name = OAK
Ø
Renamed
Java in 1995
Features of
Java
Ø
Simple
o Similar to C++
o No need of header files, pointer arithmetic,
structures, unions, operator
overloading, virtual base classes
Ø
Object-Oriented
Ø
Network
Savvy
o It has extensive lib of routines for coping
with TCP/IP protocols like HTTP & FTP
o Access the objects across the Net via URLs
o RMI enables communication b/w distributed
objects
Ø
Robust
o No pointer using. So we can never access a bad
pointer, make memory allocation errors or have to protect against memory
leaking away.
Ø
Secure
Ø
Architecture
Neutral (Platform Independent)
o Work on many different platforms
o It is a platform independent Both Source and
binary level
Ø
Portable
Ø
Interpreted
Ø
High
Performance
Ø
Multithreaded
o Do many things simultaneously
Ø
Dynamic
Simple
Program
1. import java.io.*;
class
simple
{
public
static void main(String arg[])
{
System.out.println("Welcome
to java");
}
}
2. import java.io.*;
class
simple
{
public
static void main(String arg[])
{
int
a=10,b=10,c;
c=a+b;
System.out.println("Result
is="+c);
}
}
3. import java.io.*;
class
simple
{
public
static void main(String arg[])
{
String
na="Arunya",st="North",pl="Aundipatty";
System.out.println("Name
="+na+"\n"+"Street ="+st+"\n"+"Place
="+pl);
}
}
4. import java.io.*;
class
simple
{
public
static void main(String arg[])
{
int x=1,y=20,z;
z=x>y?x:y;
System.out.println("Biggest
Number ="+z);
}
}
Programming
Constructs
Conditional Statments
Simple if statement
Syntax
if(condition)
{
Statement;
}
Example
import
java.io.*;
class
simple
{
public
static void main(String arg[])
{
int
x=20,y=20;
if(x==y)
{
System.out.println("Numbers
are Equal");
}
}
}
if else statement
syntax
if(condition)
{
True_Statement2;
}
else
{
False_Statement2;
}
Example
1. import java.io.*;
class
simple
{
public
static void main(String arg[])
{
int
x=10,y=20;
if(x>y)
{
System.out.println("A
is Big");
}
else
{
System.out.println("B
is Big");
}
}
}
Nested if
statement
syntax
if(condition)
{
if(condition)
{
Statement1;
}
else
{
Statement2;
}
else
Statement3;
Example
import
java.io.*;
class
simple
{
public
static void main(String arg[])
{
int x=50,y=70,z=80;
if(x>y)
{
if(x>z)
System.out.println("X
is Big");
else
System.out.println("Z
is Big");
}
else
{
if(z>y)
System.out.println("Z
is Big");
else
System.out.println("Y
is Big");
}
}
}
if else if statement
syntax
if(condition1)
Statement1;
else if(condition2)
Statement2;
else if(condition n)
Statement
n;
else
default statement;
Statements_x;
Example
import
java.io.*;
class
simple
{
public
static void main(String arg[])
{
int
rno[]={111,222,333,444};
int
marks[]={81,75,43,58};
for(int
i=0;i<rno.length;i++)
{
if(marks[i]>79)
System.out.println(rno[i]+"Honours");
else
if(marks[i]>59)
System.out.println(rno[i]+"A"
);
else
if(marks[i]>49)
System.out.println(rno[i]+"B");
else
System.out.println(rno[i]+"FAIL");
}
}
}
Control
Statements
for
Statement
Syntax
for(initialization;
test condition;increment)
{
Body of the loop
}
Example
1. import java.io.*;
class
simple
{
public
static void main(String arg[])
{
int n=5,i;
for(i=1;i<=n;i++)
{
System.out.println(i);
}
}
}
2. import java.io.*;
class
simple
{
public
static void main(String arg[])
{
int n=5,i;
for(i=n;i>=1;i--)
{
System.out.println(i);
}
}
}
while Statement
Syntax
While(condition)
{
Statement
}
Example
1. import java.io.*;
class
simple
{
public
static void main(String arg[])
{
int
n=5,i=1;
while(i<=n)
{
System.out.println(i);
i++;
}
}
}
2. Reverse the Digit
import java.io.*;
class
simple
{
public
static void main(String arg[])
{
int
n=123,r,s=0;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
System.out.println("Reverse
the Number is="+s);
}
}
3. Armstrong Number
Checking
import
java.io.*;
class
Armstrong
{
public
static void main(String arg[])
{
int
n=143,r,s=0,m;
m=n;
Amstrong Numbers 1,153,370,371,407
while(n>0)
{
r=n%10;
s=s+r*r*r;
n=n/10;
}
if(s==m)
{
System.out.println("Amstrong
Number");
}
else
{
System.out.println("Not
a Amstrong Number");
}
}
}
Do - while Statement
Syntax
Do
{
Statement
}
While(condition);
Example
1. import java.io.*;
class
simple
{
public
static void main(String arg[])
{
int
i=1,n=5;
do
{
System.out.println(i);
i++;
}
while(i<=n);
}
}
2. Square, Square
root,Cube
import
java.io.*;
class
simple
{
public
static void main(String arg[])
{
int
i=1,n=3,s,cu;
double
sq;
System.out.println("Number
\t Square \t Squareroot \t Cube");
do
{
s=i*i;
sq=Math.sqrt(i);
cu=i*i*i;
System.out.println(i+"\t"+s+"\t"+sq+"\t"+cu);
i++;
}
while(i<=n);
}
}
Break Statement
import
java.io.*;
class
simple
{
public
static void main(String arg[])
{
int
i=1;
while(i<=10)
{
if(i==5)
{
break;
}
else
{
System.out.println(i);
}
i++;
}
}
}
Switch case Statement
Syntax
Switch(test)
{
Case
value1:
Statement;
Break;
Case
value2:
Statement;
Break;
Default:
Statement;
}
Example
import java.io.*;
class switchdemo
{
public static void
main(String args[])
{
int a,b;
char ch;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
ch=args[0].charAt(0);
switch(ch)
{
case'+':
System.out.println("Sum="+(a+b));
break;
case'-':
System.out.println("Difference="+(a-b));
break;
case'*':
System.out.println("Product="+(a*b));
break;
case'/':
System.out.println("Quotient="+(a/b));
break;
default:
System.out.println("Invalid
Choice");
}
}
}

No comments:
Post a Comment