Anda di halaman 1dari 64

1 08/08/2011

Rules
1. Look at the picture Carefully 2. Guess the Keyword according to picture and related to Object Oriented Concepts. 3. Tell brief description about each keyword.

SYLLABUS
UNITS TOPICS

UNIT - 1
UNIT - 2

INTRODUCTION TO JAVA
CLASSES, INHERITANCE, EXCEPTIONS, APPLETS

UNIT - 3
UNIT - 4 UNIT - 5 UNIT - 6 UNIT - 7 UNIT - 8

MULTI THREADED PROGRAMMING, EVENT HANDLING


SWINGS JAVA 2 ENTERPRISE EDITION OVERVIEW, DATABASE ACCESS SERVLETS JSP, RMI ENTERPRISE JAVA BEANS

RESOURCES
TEXT BOOKS
BOOK NAME
Java - The Complete Reference J2EE - The Complete Reference

AUTHOR, EDITION, PUBLISHER


Herbert Schildt, 7th Edition, Tata McGraw Hill, 2007 Jim Keogh, Tata McGraw Hill, 2007

REFERENCES
BOOK NAME
Introduction to JAVA Programming The J2EE Tutorial

AUTHOR, EDITION, PUBLISHER


Y. Daniel Liang, 6th Edition, Pearson Education, 2007 Stephanie Bodoff et al, 2nd Edition, Pearson Education, 2004

Websites
http://www.java.com/en/download/faq/java_download.xml

http://www.java.com/en/
http://www.oracle.com/technetwork/java/index.html http://download.oracle.com/javase/tutorial/ http://www.java2s.com/Tutorial/Java/CatalogJava.htm http://www.roseindia.net/java/ http://www.javabeginner.com/learn-java/introduction-to-java-programming http://www.easytutor.2ya.com/ http://www.javabeginner.com/learn-java/introduction-to-java-programming http://www.freejavaguide.com/ http://kswouonline.in/CourceMeterials/bca42.pdf

http://sharadavikas.com/CourceMeterials/mca43.pdf
6

Name
Birth Date Birth Place best known as

James A. Gosling
May 19, 1955 Calgary, Alberta, Canada

Father of Java Programming Language

Educational Qualification
B.Sc in Computer Sc. Ph.D in Computer Sc. University of Calgary Carnegie Mellon University 1977 1983

Profession
1984 and 2010 March 28, 2011 May 20 Sun Microsystems Google Advisor at the Scala company Typesafe

Contributions
Inventor of the Java programming language in 1994 original design of Java and implemented its original compiler and virtual machine.

elected to Foreign Associate member of the United States National Academy of Engineering
major contributions to several other software systems, such as NeWS and Gosling Emacs co-wrote the "bundle" program, a utility thoroughly detailed in Brian Kernighan and Rob Pike's book The Unix Programming Environment

Honors
2007: he was made an Officer of the Order of Canada

Sun Microsystems, Inc, Santa Clara, California (part of Silicon Valley)

Interpreted Programming Languages Compiled Programming Languages


Byte Code in Java Java Runtime enviroment Java Virtual machine Access Modifiers

Difference between C++ and Java


No operator overloading in Java No multiple inheritance in Java.

No templates in Java.
No friend Access Modifier.

All assignments are through reference for objects.


No stack allocation for objects (all are on heap) in Java. Java supports Multithreading, Networking , GUI , primitive graphics and Data structures like Vector etc.

10

Primary Characteristics of Java


Distributed Networked Environments- Heterogenous Platform

High Performance- Multithreading, Daemon Process


Easy Reuse of Code Automatic Memory Management, dynamic
boundary checking, runtime binding of objects

Security- no pointers, Less access to Heap Memory, Dynamic


allocation, Bytecode verifier

11

Applications of JAVA
Java server Pages (JSP) Dynamic web pages

Applets used within web pages for GUI


JAVA 2 Enterprise Edition-Data transfer using XML

JAVA BEANS- reusable software component


JAVA2 Micro Edition

12

JAVA Development Kit (JDK)


Java loader, interpreter Javac - compiler Jar binds libraries Javadoc creates documentation Jdb- debugger Jps process status Javap diassembler Javah
13

Java Bytecode
Hello.java Java API
Java Platform

Java Virtual Machine (JVM)

Hardware and Platform

how Java works on a typical computer

14

Compile-time environment Source File A.java Locally through network


Java Compiler

Run-time environment

Class File A.class

Java Virtual Machine


Object.class. String.class.

A.class

Java API Class Files

The Java programming environment

15

JVM emulation run on a physical CPU

16

JVM handles translations

17

The internal architecture of the Java virtual machine

18

Structure of JAVA Program


4 Basic components of JAVA Program1. Package Statements 2. Import Statements 3. Class Declarations 4. Interface Declarations Two types of JAVA program Stand-alone applications Applets

19

JAVA Naming Conventions


Packages: Names should be in lowercase. package pokeranalyzer

package mycalculator
Classes: Names should be in CamelCase. Try to use nouns because a class is normally representing something in the real world

class Customer
class Account Interfaces: Names should be in CamelCase. They tend to have a name that

describes an operation that a class can do.


interface Comparable interface Enumerable
20

JAVA Naming Conventions contd


Methods: Names should be in mixed case. Use verbs to describe what the method does. void calculateTax() string getSurname() Variables: Names should be in mixed case. The names should represent what the value of the variable represents. string firstName int orderNumber Constants: Names should be in uppercase. static final int DEFAULT_WIDTH static final int MAX_HEIGHT

21

Data Types
Primitive Types Reference Types

Type Identifier [, Identifier]; char my_pen;

22

Primitive Data types


Integer Data Types
Type Size Byte 8 bits short 16 bits Int 32 bits Long 64 bits
23

Floating-Point Data Types


Type Size

Single Precesion 32-bits Double Precision 64-bits

Boolean Data Type boolean timeOver;


24

Character Data Type


single Unicode characters 16-bit unsigned integer

char initialState, finalState;

25

Reference Data Types


Arrays
Syntax int num[]; char[] letters; long matrix[][]; int [ ]Array4; Examples char alpha[] = new char[26]; int even = {4, 6, 8};

Strings
String class
String info; String name = "SunMicro";

26

Declaration
class Hiding { public static void main (String args[]) { int Top; Top = 2; ... switch (ACommand) { case '1': int Top = 4; // Top has just been declared again // and assigned a value of 4 break; ... } .... /*return to the main loop System.out.println(Top); /*Top still has a value of 2*/ }

27

Tokens
Identifiers
must begin with a letter, an underscore (_), or a dollar sign ($) Letters include both uppercase and lowercase letters. identifier characters can include the numbers 0 to 9. No underscore or Dollar sign in the beginning
Valid
HelloWorld Hello_Dad HeyFriend3 Sink baggage

Invalid
Hello World (uses a space) Hello Dad! (uses a space and punctuation mark) 3heyFriend (begins with a numeral) float (this is a Java keyword) #age (does not begin with letter)

multiple-word identifiers
Valid toughGuy Invalid Toughguy, ToughGuy,TOUGHGUY
28

Keywords
predefined reserved words always lowercase
Data Declaration
boolean byte char double float int long short break continue do for while case else if switch catch finally throw try abstract class default extends implements instance of interface

Loop

Conditi-onal

Exception

Structure

Modifier and Access


final native new private protected public static synchronized threadsafe transient

Miscellaneous

false import null package return super this true

void

29

Literals
Integer Literals Floating-Point Literals Boolean Literals Character Literals String Literals

30

Integer Literals
whole numbers, such as 1 and 2468 can be decimal (base 10), octal (base 8), or hexadecimal (base 16). Decimal Literals positive, zero, or negative Decimal literals cannot start with 0, as in 01234 Numbers beginning with 0 are reserved for octal and hexadecimal literals. The positive decimal integer ranges between -2,147,483,648 or 231 and 2311, or 2,147,483,647. Octal Literals start with 0 and can be followed by any number 0-7 Hexadecimal integer literal start with 0x or 0X can be positive, zero, or negative The maximum value of an octal literal is 017777777777 (231-1)
31

Float Literals
has a decimal point in ithas a decimal point in it must follow IEEE-754 specification. Single-precision floating-point numbers consist of a 32-bit space and are designated by uppercase or lowercase f.

Double-precision numbers are allotted a 64-bit space and are


designated by uppercase or lowercase d. Double-precision floating-point numbers are the default

3.7 is a double-precision floating-point number


3.7f is a single-precision floating-point number.
32

Character Literals
Description Any character Sequence 'y' Output y

Backspace (BS)
Horizontal tab (HT) Linefeed (LF) Formfeed (FF) Carriage return (CR) Double quote Single quote Backslash Octal bit pattern Hex bit pattern Unicode character

'\b'
'\t' '\n' '\f' '\r' '\"' '\'' '\\' '\ddd' '\xdd'

Backspace
Tab Linefeed Form feed Carriage return " ' \ Octal value of ddd Hex value of dd

'\udddd' Actual Unicode character of dddd

33

String Literals
sequence of characters enclosed in double quotes "" for a null character string javac compiler does not strip out whitespace from within string literals.

String literals can be concatenated.


A string literal cannot be more than one line " India is " + " beautiful country "

34

Separators - (), {}, [] , ; , , , Whitespaces - spaces, tabs, and linefeeds Comments /* comment */ // comment
/** comment */

35

Access Specifiers/modifiers
Public Private Protected Default

36

public class Square { // public class private double x, y // private public setCorner(int x, int y) { this.x = x; this.y = y; } public getCorner() { return Point(x, y); }}

37

Operator

Operation

Example
Arithmetic Operators

Meaning

Addition Subtraction Multiplication Division Modulus

A + B

* / %

AB A*B A/B A%B Increment and Decrement operators

++ --

Increment by 1 Decrement by 1

A++ or ++A A-- or --A

A = A + 1 A = A -1

Assignment operators

=
+= -= *= /= %=

Assign value
Add to current variable Subtract from current Multiply current Divide current Modulus current

A = 7

A=7
A=A+B A=A-B A=A*B A=A/B A=A%B

A += B
A -= B A *= B A /= B A %= B

Comparison Operators (return true or false) == != < > <= >= Equal Not equal Less than Greater than Less than or equal Greater than or equal A== B A!= B A < B A> B A <= B A >= B Is A equal to B? Is A not equal to B? Is A less than B? Is A greater than B? Is A less than or equal to B? Is A greater than or equal to B?

Java Operators (Contd)

38

Operator

Operation

Example Bitwise operators

Meaning

& | ^ << >> >>>

Bitwise AND Bitwise OR Bitwise XOR Left shift Right shift Zero fill right shift

Bitwise complement

<<=

Left shift assignment

>>=

Right shift assignment

>>>=

Zero fill right


shift assignment

x&=y x|=y x^=y

AND assignment OR assignment

39

NOT assignment Operators Java

Same Precedence Highest

.
++ * + << < ==

[]
-/ >> >

()
! % ~

>>> <= >=

&
^ && !=

Lowest

|| ?: =
Operator Precedence
40

Control Flow
Statements Blocks a block of code: { Store = "Grocery"; a=b+c; System.out.println( a); }
41

Conditional if
if (expression) statement; or if (expression) { statement(s); }

42

switch (expression) { case value: statement(s); break; case value: statement(s); break; . . . default: statement(s); break; }
43

while ( expression ) statement; or while ( expression ) { statement(s); } int a=1,sum=0; while (a<5) { sum=sum+a; a++; }
44

for (initialization; expression; modification) statement; or for (initialization; expression; modification) { statement(s); }
Exampleint sum=0,a; for (a=1; a<5; a++) { sum=sum+a; }
45

int i; for (i=0; i < ArrSize; i++) { if (i < 0) { System.out.println("Invalid Position"); break; } System.out.println(alpha[i]); }

46

int i; for (i=0; i < ArrSize; i++) { if (i < 0) { System.out.println("Invalid Position"); continue; } System.out.println(alpha[i]); }

47

error: for (i=0; i < ArrSize; i++) { for (i=0; i < ArrSize; i++) { if (i < 0 && j<0) { System.out.println("Invalid Position"); break error; } System.out.println(alpha[i]); } } Print(n);
48

Type Casting
Automatic Conversion Explicit Conversion (Casting) myInteger = (int) myLongInteger

public class ImplicitCast { public static void main (String ar[]){ int i=10; float f=i; System.out.println(f); //10.0 char ch='a'; int j=ch; System. out.println(j); //97 } }

Two type of casting in JAVA Up casting Down casting

49

Example - Type Casting


//Integer code public class CastExample { public static void main(String arg[]) { String s=27; int i=(int)s; System.out.println(i); } } //Integer to String int a=97; String s=Integer.toString(a); (or) String s=+a;
50

Contd
//Double to String String s=Double.toString(doublevalue); //Long to String String s=Long.toString(longvalue); //Float to String String s=Float.toString(floatvalue); //String to Integer String s=7; int i=Integer.valueOf(s).intValue(); (or) int i = Integer.parseInt(s); //String to Double double a=Double.valueOf(s).doubleValue();

51

Contd
//String to Long long lng=Long.valueOf(s).longValue(); (or) long lng=Long.parseLong(s); //String to Float float f=Float.valueOf(s).floatValue();

//Character to Integer char c=9; int i=(char)c; //String to Character String s=welcome; char c=(char)s;
52

Creating Objects
Classname objectname = new classname ( );
For exampleExampleClass ExamObject = new ExampleClass ( );

53

Instance Variables
class FirstPerson { int age = 0; public void setData(int newAge) { age=newAge; } public int getData( ) { return age; } }
54

Program demonstrating Char Variables


Class CharDemo { Public static void main(String args[ ]) { char ch1,ch2; Ch1=88; Ch2=y; System.out.print(ch1 and ch2:); System.out.println(ch1+ +ch2); } } Output: Ch1 and ch2:x y
55

Program demonstrating char variables behaves like integers Class CharDemo1{ public static void main(String args[ ]) { char ch1; ch1=X; System.out.println(ch1 contains+ch1); ch1++; System.out.println(ch1 is now+ch1); } }
Output: ch1 contains X ch1 is now Y

56

Program illustrating the use of boolean variable


class BoolTest{ public static void main(String args[ ]){ boolean b; b=false; System.out.println("b is"+b); b=true; System.out.println("b is"+b); if(b) System.out.println("This is not executed"); System.out.println("10>9is"+(10>9)); } }
57

Program illustrating dynamic initialization


class DynInit{ public static void main(String args[]){ double a=3.0, b=4.0; double c=Math.sqrt(a*a+b*b); System.out.println("Hypotenuse is "+c); } }

58

//Program illustrating type conversion


class Conversion{ public static void main(String args[]){ byte b; int i=257; double d=323.142; System.out.println("\nConversion of int to byte"); b=(byte)i; System.out.println("i and b"+i+" "+b); System.out.println("\nConversion of double to int"); i=(int)d; System.out.println("d and i"+d+" "+i); System.out.println("\nConversion of double to byte"); b=(byte)d; System.out.println("d and b"+d+" "+b); } }

59

One-Dimensional Arrays
Declaration 1. Type var-name[ ]; Int month_days[]; 2. array-var=new type[size]; month_days=new int[12]; Initialization Month_days[1]=28; Int month_days*+=,31,20,3060

Two-Dimensional Arrays
int twoD[] []=new int[4][5]; Alternative Array declaration syntax Type [ ] varname;

int a1[ ]=new int[3]; int [] a2=new int[3]; Char twod1[] []=new char[3][4]; Char [ ][ ] twod2=new char[3][4];
61

Assignments
1. Write a JAVA program demonstrating basic arithmetic operators. 2. Write a JAVA program demonstrating various assignment operators. 3. Write a JAVA program to convert temperature from Celsius to Fahrenheit 4. Write a JAVA program to Find diam/area/circum of circle 5. Write a JAVA Program to check the input characcter for uppercase, lowercase, no. of digits 6. Write a JAVA Program for counting no. of Chars, Words and Lines in a file 7. Write a JAVA program Find the digit sum of a sequence of digits 8. Design and implement queue using JAVA.
62

Group1
Implement Centigrade to Fahrenheit converter using JAVA

Group2
JAVA program to list all even numbers between two numbers

Group3
Implement Simple calculator using JAVA

Group4
Write a JAVA program Find factorial of 5.
65

Group5
Implement linear search using JAVA

Group6
JAVA program to check whether given year is leap or not.

Group7
Implement table of 10 using JAVA

Group8
Implement Palindrome using JAVA
66

Anda mungkin juga menyukai