Sei sulla pagina 1di 13

--------------------------------------------------------------------------------

-----------
Introduction to java Programming
--------------------------------------------------------------------------------
-----------
Introduction for =Sun Micro Systems 1991 (James Gosling & Group)
=Internet Oriented Programming language
=Java Family :Java,Java script,Hot Java
=1994 Hot Java (web Browser)
=1999 Sun Micro systems (James Gosling)
Running Java =Win 98,Win Nt,Mac,Ms Dos,Unix,Linux
Interpreter = Java Development Kit(JDK)
Web Object Two Types:
1.Passive Information files
2.Dynamic active Programs. Ex:Java A pplet Prg
Java Programs in Two Types
1.Application -.Stand-alone application
2.Applet -Web Programs
Advantages:
-Simple
-Object Oriented Language (oops)
-Distributed
-Interpreted(High level language To Machine Level Language)
-Robust (Big size programs)
-Secure
-Architecture Natural (Server-Client Architecture)
-Protable (System To System data passed to easy way)
-High Performance
-Multithreaded
-Dynamic (Good condition run)
Not Allowed in Java:
-Pointer
-Structure and Union
-Operator Overloading
-Multiple Inheritance
-Goto,Delete Operaters
-Automatic type conversition
-Allowed constructor.Not allowed Destructors
-Unsigned integer
Program Execute:
Java Prg-Java compiler-Byte code or class file
|
Machine code
Accept code :Apple,Ibm,sun work station any paltform prg run
|
Architecture Netural
Note :Sun Micro Systems ='Write once and run it any where'
Example:
Process of Compiler:
Java Program Java Compiler Virtual Machine
(Bytecode)
process of converting bytecode into machine code
Bytecode Java Interpreter Machine code
(Virtual machine) (Real Machine)
Note :Java is an Case sensitive
:Compiler (Compile And Total Error) -Javac
:Interpriter -Java
--------------------------------------------------------------------------------
-----------
Creating a java Application:
--------------------------------------------------------------------------------
-----------
Java Programming Structure:
Class <className>
{
Variable Declaration;
Main method class;
{
Main method definition
}
}
Example:
Open Java Application:
Step :1
To Create a Directory Example: f:\shan
Step :2
To Create a Batch Files:
1.Open Notepad
set path=%path%;c:\programs Files\java\Jdkpath1.6.0\bin;
2.Save File: F:\shan\jdkpath.bat
Step :3
Start----->Run-----Cmd and Ok
F;\shan>jdkpath
Step :4
To check The java Compiler
f:\shan>javac
Step::5
To Create a Java Files
F:\Shan>edit First.java
Example:
class First
{
public static void main(String args[])
{
System.out.println("Java is better than c++.");
}
}
********************************************************************************
*****************
Step :6
To Save Files:
File--->Save---->First.java
Step :7
To Exit a Command prompt:
File ---->Exit
Step :8 Coimple and Execute:
1.save file "First.java" (Mention for Class Name)
2.Compile =javac First.java (Dos Prompt)
3.Run Prg =Java First
Note: 1.Compiling source code into bytecode using javac compiler. Example:Fir
st.class
2.Executing the bytecode program using java interpreter.

Example:2
Using More than Statement
class Second
{
public static void main(String args[])
{
System.out.println("Hello java.");
System.out.println("Welcome to the world of Java .");
System.out.println("Let us Learn Java c++.");
}
}

Command Line:
// =Single line
/*.......*/ =Multi line
--------------------------------------------------------------------------------
--
Variable and Constants:
--------------------------------------------------------------------------------
--
Variable :
Variable is an Tempory Buffer or Memory space.
Syntex:
DataType variableName;
DataTypes:
byte,short,int,long :Example:int sn0=101;
float,double :Example:float fv=66.6789f or double dv=77.56789
0
char,String :Example:char gender='m' Example:String name="Ku
mar"
boolean :Example:True/False or yes/no

Integer :
Key Size
``````` ```````
byte 8 bit (1 byte)
int 32 bit (4 bytes)
short 16 bit (2 bytes)
long 64 bit (8 bytes)
Float :
``````````
Key Size
``````` ```````
float 32 bit (4 bytes)
double 64 bit ( 8 bytes)
Character:
`````````````````
Key Size
``````` ```````
char 16 bit (2 bytes)
String :
````````````
Key Size
``````` ```````
String -
Boolean :
````````````````
Key Size
``````` ```````
boolean - (t/f)
Constant:
Syntax:
const DataType VaraiableName=Values;
Example:
const int a=10;
const float av=56.789;
const char gender='m';
const char name[20]="Dharnish";
Backslash Character constant:
'\b' -Back space
'\n' -new line
'\r' -carriage return
'\'' -single quote
'\"' -double quote
'\\' -baskslash
--------------------------------------------------------------------------------
------------------
Operators
--------------------------------------------------------------------------------
------------------
Types of Operators:
1.Arthimatic op :Example:+,*,-,/,%
2.Relational Op :Example: >,<,>=,<=,==,<>
3.Logical Op :Example: &&,||,!
4.Assigement op :Example: a+=1 or a=a+1,a=a*1,a=a-1,a=a/b,a=a%b
5.Increment/Decrement op or Unary op Example: ++,--
1.Arithmetic Operations:
Operator Meaning Example
+ Addition 3+4
- Subtraction 3-4
* Multiplication 3*4
/ Division 3/4
% Modulus 3%4
Example:
class Arthimatic
{
public static void main(String args[])
{
int a=20,int b=30;
System.out.println("\nAdd Two Number :"+(a+b));
System.out.println("\nMultiplcation Two Number :"+(a*b));
System.out.println("\nSubstration Two Number :"+(a-b));
System.out.println("\nDivision Two Number :"+(a/b));
System.out.println("\nRemainder Two Number :"+(a%b));
}
}
a.Student Mark
b.Employee Payslip
c.Department Stores
-------------------------------------------------------------------------------
2.Relation Operators:
Operators Meaning Example
< Less then x<3
> Greater than x>3
<= Less than or equal x<=3
>= Greater than or eaual x>=3
== Equal x==3
!= Not Equal x!=3
Example:
class RelationOperations
{
public static void main(String args[])
{
int a=20,int b=30;
System.out.println("\n A is :"+a);
System.out.println("\nB is :"+b);
System.out.println("\nA Less than B :"+(a<b));
System.out.println("\nA Greate than B :"+(a>b));
System.out.println("\nA Less tha or Equal B :"+(a<=b));
System.out.println("\nRemainder Two Number :"+(a>=b));
System.out.println("\nA Equal of B :"+(a==b));
System.out.println("\nA Equal of B :"+(b!=a));
}
}
Note: Answer is True /False
3.Logical Operators:
Operators Meaning Example
&& and age>18 && age<16
|| or age>18 || age<16
! not age>18 ! age<16
Example:
class LogicalOperations
{
public static void main(String args[])
{
int a=10,int b=20;
System.out.println("\n A is :"+a);
System.out.println("\nB is :"+b);
System.out.println("\nB is :"+c);
System.out.println("\nA and B Equal :"+(a>10 && a<10));
System.out.println("\nA and B Equal :"+(a>10 =|| a<10));
System.out.println("\nA and B Equal :"+(a>10 ! a<10));
}
}
4.Assignment Operators:
Expression Meaning
x+=y x=x+y
x-=y x=x-y
x*=y x=x*y
x/=y x=x/y
Example:
int x=5
x=x+5 or x+=5
Example:
class AssienOp
{
public static void main(String args[])
{
int x=5;
System.out.println("Display x Value :"+x);
x+=10;
System.out.println("Add a x value :"+x);
x-=5;
System.out.println("Minus x value :"+x);
x*=5;
System.out.println("Multip x value:"+x);
x=x+5;
System.out.println("Divided x value:"+x);
x/=5;
System.out.println("Add a x value:"+x);
getch();
}
}
5.Increment and Decrement:
x++ Post-increment
++x Pre-increment
x-- Post-Decrement
-- Pre-Decrement
0
Example:
class increDecre
{
public static void main(String args[])
{
int a=10,b=20;
System.out.println("\nEnter the a value :"+a);
System.out.println("\nEnter the b value :"+ ++b);
System.out.println("\nEnter the ++a value :"+ a++);
System.out.println("\nEnter the b++ value :"+ b++);
System.out.println("\nEnter the a value :"+a);
System.out.println("\nEnter the b value :"+b);
}
}
6.Conditional Operators:
Syntax:
VariableName=Condition ? Statement1 :Statement2
--0----------------------------------------------------------------------------
CommandLine Argument
------------------------------------------------------------------------------
Example:
class Comm1
{
public static void main(String args[])
{
System.out.println(args[0]);
System.out.println(args[1]);
}
}
Example:
class add
{
public static void main(String args[])
{
int a,b,c,d,e,f,g;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=a+b;
d=a*b;
e=a-b;
f=a/b;
g=a%b;
System.out.println("\nAdd Two No is :"+c);
System.out.println("\nMul Two No is :"+d);
System.out.println("\nSub Two No is :"+e);
System.out.println("\nDiv Two No is :"+f);
System.out.println("\nRemainder is :"+g);
}
}
Excerisess:
1.To Find out Student Mark List Sno sname m1 m2 m3 Tot av
tot=m1+m2+m3
av=tot/3
2.To Find out Employee Payslip Sno ename bpay hra da ta gpay lic pf ded
npay
gpay=bpay+hra+da+ta
ded=lic+pf
npay=gpay-ded
3.To Find out Department Stores Sno pname qty rate total disc Amount
total=qty*rate
disc=total*5/100
amt=total-disc
Using Coomand Line Arguments.

Example:Length of String:
class comm2
{
public static void main(String args[])
{
int i=0;
for(i=0;i<args.length;i++)
{
System.out.println(i+args[i]);
}
}
}
Example:
class mark
{
public static void main(String args[])
{
int sno;
String sname;
int t,e,m,s,h;
int tot;
float av;

sno=Integer.parseInt(args[0]);
sname=args[1];
t=Integer.parseInt(args[2]);
e=Integer.parseInt(args[3]);
m=Integer.parseInt(args[4]);
s=Integer.parseInt(args[5]);
h=Integer.parseInt(args[6]);
tot=t+e+m+s+h;
av=tot/5;
System.out.println("\n-------------------");
System.out.println("\nSerial No :"+sno);
System.out.println("\nName :"+sname);
System.out.println("\nTamil :"+t);
System.out.println("\nEnglish :"+e);
System.out.println("\nMaths :"+m);
System.out.println("\nScience :"+s);
System.out.println("\nHistory :"+h);
System.out.println("\n------------------");
System.out.println("\nTotal Mark is :"+tot);
System.out.println("\nAverage Mark :"+av);
}
}
--------------------------------------------------------------------------------
---
Itirative KeyBoard Input:
--------------------------------------------------------------------------------
---
Data Reading and Writing
Example:
import java.io.*;
class salary
{
public static void main(String[] args) throws Exception
{
}
}
Using Class and Object:
DataInputStream in=new DataInputStream(System.in);
BufferedReader request = new BufferedReader(new InputStreamReader(Syste
m.in));
String Request:
Example:
import java.io.*;
class Exercise1
{
public static void main(String[] args) throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(Sys
tem.in));
String answer;
System.out.print("Are you willing to be an organ donor? ");
answer = in.readLine();
}
}

Example:
import java.io.*;
public class Exercise2
{
public static void main(String[] args) throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(Sys
tem.in));
String answer;
int ans;
// Request the availability of a credit card from the user
System.out.print("Are you ready to provide your credit card number(1=Yes
/0=No)? ");
answer = in.readLine();
ans = Integer.parseInt(answer);
}
}
Example:
import java.io.*;
class Exercise3 {
public static void main(String[] args) throws Exception
{
BufferedReader request = new BufferedReader(new InputStreamReader(System
.in));
System.out.print("Enter Full Name: ");
String fullName = request.readLine();
System.out.println("Full Name: " + fullName);
}
}
Example:
import java.io.*;
class Exercise4 {
public static void main(String[] args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader
(System.in));
String CustomerName, HomePhone;
System.out.print("Enter Customer Name: ");
CustomerName = reader.readLine();
System.out.print("Enter Customer Phone: ");
HomePhone = reader.readLine();
System.out.println("Customer: " + CustomerName);
System.out.println("Home Phone: " + HomePhone);
}
}
Example:
Number Request
Integer.parseInt
Double.parseDouble().
import java.io.*;
class Exercise5
{
public static void main(String[] args) throws Exception
{
BufferedReader jin = new BufferedReader(new InputStreamReader(System.in)
);
int Number;
String strNumber;
System.out.print("Enter a Number: ");
strNumber = jin.readLine();
System.out.print("Number: " + Number);
Number = Integer.parseInt(strNumber);
}
}

Example:
import java.io.*;
class Add2
{
public static void main(String args[])throws IOException
{
String name;
DataInputStream in=new DataInputStream(System.in);
System.out.println("\nEnter Your Name :");
name=in.readLine();
System.out.println("Dear"+name+"Happy Birth Day to You");
}
}

Example:
import java.io.*;
class Name
{
public static void main(String args[])throws IOException
{
String name;
DataInputStream in=new DataInputStream(System.in);
System.out.println("\nEnter the name:");
name=in.readLine();
System.out.println("Hai"+name);
System.out.println("goodevening"+name);
}
}
Example for add a Two Numbers:
import java.io.*;
class Add1
{
public static void main(String args[])throws IOException
{
String name;
int a,b;
DataInputStream in=new DataInputStream(System.in);
System.out.println("\nEnter the Name :");
name=in.readLine();
System.out.println("\nEnter the First No :");
a=Integer.parseInt(in.readLine());
System.out.println("\nEnter the Second No :");
b=Integer.parseInt(in.readLine());
System.out.println("\nName is :"+name);
System.out.println("\nThe Result is :"+(a+b));
}
}

Example : (Floating Numbers)


class ArthFloat
{
public static void main(String args[])
{
float a=20.5f, b=6.4f;
System.out.println("\nFirst Number :"+a);
System.out.println("\nSecondt Number :"+b);
System.out.println("\nAdd Two Number :"+(a+b));
System.out.println("\nMultiplcation Two Number :"+(a*b));
}
}

Example:
Ternory Operators:
import java.io.*;
public class Exercise7
{
public static void main(String[] args) throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in))
;
int number1, number2, maximum;
String strNumber1, strNumber2;
System.out.print("Enter first numbers: ");
strNumber1 = in.readLine();
System.out.print("Enter second numbers: ");
strNumber2 = in.readLine();
number1 = Integer.parseInt(strNumber1);
number2 = Integer.parseInt(strNumber2);
maximum = (number1 < number2) ? number2 : number1;
System.out.print("\nThe maximum of ");
System.out.print(number1 + " and " + number2 + " is ");
System.out.println(maximum);
}
}
Example:
import java.io.*;
public class Exercise8 {
public static void main(String[] args) throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(Sys
tem.in));
String firstName, lastName, strOrganDonor, gender;
System.out.println(" -=- Motor Vehicle Administration -=-");
System.out.println(" --- Driver's License Application ---");
System.out.print("First Name: ");
firstName = in.readLine();
System.out.print("Last Name: ");
lastName = in.readLine();
System.out.print("Gender(F=Female/M=Male): ");
gender = in.readLine();
System.out.print("Are you willing to be an Organ Donor(y=Yes/n=No)? ");
strOrganDonor = in.readLine();
System.out.println("\n -=- Motor Vehicle Administration -=-");
System.out.println(" --- Driver's License Information ---");
System.out.println("Full Name: " + firstName + " " + lastName);
System.out.println("Sex: " + gender);
System.out.print("Organ Donor? ");
System.out.println(strOrganDonor == "y" ? "Yes" : "No");
}
}
--------------------------------------------------------------------------------
---------------------------------------------
Java Environment:
1.Java Development Kit (JDK)
2.Java statndard Library (JSL)
3.Application Programming Interface (API)
1.Java Development Kit (JDK)
-Appletviewer (for viewing java applets)
-Javac (Java Compiler)
-Java (Java Interpreter)
-Javap(Java Disassembler)
-Javah (for c header files)
-Javadoc (for creating Html documents)
-Jdb (Java debugger)
2.Java statndard Library (JSL)
The java standard library (or api) include hundred of classes and method
grouped into several functional packages.commonly used package are
1.Language support package:
A collection of classes and methods
2.Utilities package:
A collection of classes to provide utility fuction such as
Date and time functio.
3.Input/Output package:
Used in input and output statements.
4.Networking Package:
Used in internet.
5.Awt Package (Abstract windows tool kit)
Used for graphical user interface.
6.Applet Package:

Potrebbero piacerti anche