Sie sind auf Seite 1von 40

JAVA

Java can be classified/studied under 5 major categories: Desktop Technologies Web Technologies Distributed Technologies Enterprise Technologies Micro Technologies

Desktop Technologies + Core Java + Java 2 Scope: Desktop/LAN Web Technologies + Servlets + Java Server Pages (JSP) + Java Server Faces (JSF) Scope: Internet/Intranet/Extranet Distributed Technologies + Remote Method Invocation (RMI) Scope: Distributed storage/ Distributed Processing

Enterprise Technologies + Java 2 Enterprise Edition (J2EE) + Enterprise Java Bean (EJB) Scope: Huge Database Applications Micro Technologies + Java 2 Micro Edition (J2ME) Scope: Mobile/Pagers/Set Top Boxes

Introduction to Java
+ Java is a product from SUN Microsystems. SUN - Stanford University Network + Java was introduced in the year 1991 (initially introduced by the name of OAK) + Popularity of Java is because of
Rich Constructs Rich library Applets

Java Library --------------------------------------- Application C/C++ Library -----------------Customize----------- Application

Applets: Small programs written in Java executed within Browser. Example Yahoo! Chat Applet Servlets: Small programs written in Java executed on Servers. Midlets: Small programs written in Java executed in Micro devices. Example Mobile

Buzzwords/Characteristics/Featu res of Java:


Simple Program written in Java is Simple (Resembles with c/c++) Portable Program written in java is Platform independent. Powerful program in Java are powerful due to its rich library

Secure and Robust + No Pointers + Does not interact with hardware directly. Dynamic: + No concept of static memory. + Each object is allocated memory dynamically. Types of memory allocation
Static

+ Larger life span (reduces Efficiency) + Not Flexible (Storage problem)


Dynamic

+ Shorter life span + Flexible

Multi-Threading: Multiple threads working on same data is known as multi-threading. XP MS Word MS Excel Winamp

Typing Spell-check Grammar-Check Auto-Correct

+ Multi-tasking is implementing multiple tasks at same time which works on different data. + Multi-threading = multi-tasking + Sharing data + Inter thread comm. Distributed Processing/Storage: Storing data at different places and while accessing it combining it together, or processing in different processors to speed up the execution.

True OOP Structure: + Encapsulation: Hiding of non-essential details


+ Data Binding + Data Hiding
Security To avoid Complexity

+ Abstraction: Give essential details in summarized form. + Inheritance Reusability, extending, and overriding. + Polymorphism Multiple Use

Compiled and Interpreted, yet high performance: Save Compile C/C++ -------- .c/.cpp ----------- .obj (Machine code) Save Compile Interpret Java --------- .java ----------- .class ----------- OUTPUT javac java (Byte Code)

+ Byte code is not the machine code it is similar to machine code. + Machine code = Binaries + Machine Dependencies + Byte code = Machine Code - Machine Dependencies Requirement for Interpretation + JVM: Java Virtual Machine + JRE: Java Run Environment Platform independent: Java program are platform independent but not JVM and JRE.

Software Requirements
+ Jdk1.5 (Java Development Kit)-----J2SE(5.0) Compiler javac Interpreter java Debugger jdb Documentation javadoc To view Applets appletviewer Java API (Application Programming Interface) + Editor (notepad, vi, Edit) Or IDE (NetBeans, Eclipse, Kawa)

Sample Program
class First { public static void main(String args[]) { System.out.println(Hello World); } }// My First Java Program class <ClassName> { //Body }

public: it can be called from anywhere static: No object is required void: It does not return any value main: Name of function that gets executed String args[]: Command line argument, args is a array of datatype String. System.out.println(message); //New line after message System.out.print(message);// no new line after message \n \t \a \\ \ \ are same as they were in c/c++. // Single line comment /* Multi line comment */

How to Write & Execute Java Program


Open notepad + Start | Run | Notepad Or + Start | Program | Accessories | Notepad 2. Type program class First { public static void main(String args[]) { System.out.println(Hello World); } }// My First Java Program

3. Save it + Class name and file name should be same. + Extension should be .java. + It is case Sensitive. D:\Data\First.java 4. Compile it + Go to Dos prompt. Start | Run | cmd (XP/NT/2000) Start | Run | command (9x) + Go to the directory where you have saved. C:\windows\desktop> D: D :\> cd Data D:\Data> javac First.java

Syntax: javac FileName.java + Execute it/interpret it D:\Data>java First Syntax: java First

How To Troubleshoot a Java Program


+ D:\Data>Dir F* First.java //correct First.java.txt //incorrect

Problem: OS does not recognize java file Solution: XP/NT/2000: My Computer | Tools | Folder Options | Filetypes - Select new | Extension Java | OK + D:\Data>javac First.java

XP/NT/2000: Not an internal/ external DOS command

Problem: OS does not recognize javac Solution: + Start | Find | Files and Folders| javac.exe (search in My Computer) + Select javac.exe. | Right click | Properties + copy the location Ex. C:\Program Files\Java\jdk1.5.0\bin

+ XP/NT/2000 My Computer | Properties |Advanced Tab| Environment Variables | System Variables Select path |edit Variable Name path Variable Value ..; <Paste It> OK + D:\Data>java First NoClassDefFoundError set CLASSPATH =.;

Datatypes
+ Data types in any language specifies the type of data to be stored and the size of memory to be allocated. + Data types can be classified in two category
Fundamental Derived

Fundamental/Pre-Defined/BuiltIn/Primitive
Data types that the compiler understands char 2 bytes 216-1 byte 1 byte 28-1 short 2 bytes 216-1 int 4 bytes 232-1 long 8 bytes 264-1

Java uses EDBIC (Extended Binary Code, Decimal Interchange Code). C/C++ uses ASCII. 1-bit is used for the sign of the number. float 4 bytes 232-1 6 digit precision double 8 bytes 264-1 12 digit precision boolean 1 bit

Variables
+ Variables are temporary memory location used to store a value. + Value stores in a variable can change /vary. + Variable naming conventions
maximum variable name can have 255 characters no spaces, no special characters except underscore(_) and dollar($). Should start with an alphabet. Should be unique within its scope. Should not be a keyword.

+ Declaration <DataType> <variableName>; Ex. char c; int num; float f; NOTE: Java makes use camel Notation for variable naming. Ex. myVar, oneTwoThree

+ Initialization num =10; c = A; + Declaration & Initialization char c = A; int num =10;
//double

10.5f // float 10.5d //double ** f & d are literals. + Literals are used to specify data type of a value.

Type Casting:
+ Narrowing/Explicit/Boxing Converting from larger data types to smaller data types. + Widening/Implicit/Unboxing Converting from smaller data types to larger data types.

int i = 10; long l = 20; i =l; //Narrowing --- Incorrect i = (int) l; //Narrowing --- correct l = i; //Widening --- correct l = (long) i; //Widening --- correct

Operators
Operators are special symbol used to manipulate on operands. Operands are fundamental data type and can be in form of variables, values of constants. They are special symbols understood by compiler. Operators can be studied on the basis:
number of operands functionality

Functionality - Arithmetical +,-,/,*,% - Relational >,<,>=,<=,==,!= - Logical &&, ||, ! - Conditional <cond> ? true : false int a = 10, b =20,c =0; c = a > b ? a :b; //c =20 - increment(++) increases the value by 1. + pre increment + post increment

int a =10,b =20; a = ++b; //a =21; b=21 a = b++; //a =21; b=22 - decrement (--) Decreases the value by 1.
+ pre decrement --i; + post decrement i--; - Assignment =,+=,-=,*=,/=, %=

Constructs
Conditional/decision/selection structures: They are used to select one out of multiple options. Like C/C++ even java provides: + if + switch

If: it can be used in 3 different ways. Ways of using if if(<cond>) { //true part } if(<cond>) { //true part } else { //false part } if(<cond>) { //true part } else if(<cond>) { //true part } else { //false part }

//Complexsive/Multiple if

Switch: switch(<variable>) { case <value1>: //true-part break; case <value2>: //true-part break; . . default: //false part }

WAP that initializes a character and print whether it is a uppercase, lowercase or special symbol.
class Test1 { public static void main(String args[]) { char c ='a'; if(c >= 65 && c<=90) System.out.println("Upper case"); else if(c>='a' && c<='z') System.out.println("lower case"); else System.out.println("Special Case"); } }

WAP to initializes a character and print whether it is a vowel or not.


class Test2 { public static void main(String args[]) { char c ='E'; switch(c) { case 'A': case 'a' : { System.out.println("Vowel " + c); break; } case 'E': case 'e' : { System.out.println("Vowel " + c); break; }

case 'I': case 'i' : {

System.out.println("Vowel " + c); break;


} case 'O': case 'o' : { System.out.println("Vowel " + c); break; } case 'U': case 'u' : { System.out.println("Vowel " + c); break; } default:

System.out.println("Not a vowel");
} } }