TO MY PRESENTATION Presented by Md Shohel Rana
TO……. . MY PRESENTATION…. . Presented by: Md. Shohel Rana Junior instructor(Tech)computer Narsingdi polytechnic institute Mobile: 01717691490 Email address: mdshohelrana 81@gmail. com
Subject Name: Programming in Java Subject Code: 66651 Semester: 5 th T P C 2 3 3 Total Mark (150) Theory (100) TC(40) PM(16) Practical (50) TF(60) PM(24) PC(25) PM(10) PF(25) PM(10)
Generation of Programming Language • First Generation • Machine Language(0, 1) • Second Generation • Assembly Language(ADD, SUB) • Third Generation • Procedure Oriented Programming(C, Visual Fox pro, VB) • Fourth Generation • Object Oriented Programming(C++, JAVA, C#) • Fifth Generation • Structural Language(SQL) • Six Generation • Artificial Intelligence LISP, Prolog)
POP vs OOP
History of Java • First version released in 1995 • Eight major versions released since then • • JDK 1. 0 (1996) JDBC, Distributed Objects JDK 1. 1 (1997) New Event Model J 2 SE 1. 2 (1998) Swing J 2 SE 1. 3 (2000) Cleanup J 2 SE 1. 4 (2002) J 2 SE 5. 0 (2004) Generics J 2 SE 6. 0 (2006) J 2 SE 7. 0 (2011)
Five Design Principles of Java • simple, object-oriented and familiar • robust and secure • architecture-neutral and portable • execute with "high performance • interpreted, threaded, and dynamic
What is/isn’t Java? • Read chapter 1 of Core Java • Discussion is very balanced • Basically, compared to C Java is a relatively high-level language with many built-in features for portably doing useful things such as: • • Multithreading Writing distributed programs Writing GUI clients Error handling Extending Web servers Embedding programs in Web browsers Connecting to commercial databases
Java vs. C • Equally importantly, Java has many core language features that make it much more natural to express abstraction, develop software frameworks, etc. • Also, many core language features which ease debugging and promote reuse.
Compiling/running first java program • Create source code file (call it for example My. First. Program. java). • To compile: prompt >> javac My. First. Program. java • This produces byte code file named My. First. Program. class • To run: prompt >> java My. First. Program
Observations • . class file is not machine code. It is intermediate form called Java Byte code. Can run on any platform as long as platform has a Java Virtual Machine (JVM). • The second step on previous slide invokes the JVM to interpret the byte code on the given platform. • In theory, byte code can be moved to another platform and be run there without recompiling – this is the magic of applets. • Leave off the. class part when invoking the JVM.
Writing first program • To keep things simple our first few programs will all be written as just a single main program. • In java, the file in which your program resides must contain a. java extension (e. g. My. First. Program. java).
Writing first program • Just as in C, main(. . ) is the principle entry point into the program. When you say java Program Java looks in Program for a procedure named main. This is where the program starts. • To print to stdout in java use: System. out. println(“ …”); • My. First. Program.
Adding datatypes -- classes • Java has handful of built-in datatypes just discussed (int, float, etc. ) • Just like in C, user typically creates own homemade datatypes to work with particular application (ie structs and enums). • In Java these are called classes. • Many class definitions come as a standard part of the Java distribution. Most common Example is String class.
Strings • Java provides a class definition for a type called String • Since the String class is part of the java. lang package, no special imports are required to use it (like a header file in C). • Just like regular datatypes (and like C), variables of type String are declared as: String s 1; String s 2, s 3; //etc. • Note that String is uppercase. This is the Java convention for classnames.
Strings • Initializing a String is painless s 1 = “This is some java String”; • Note that double quotes are required. • Memory is allocated dynamically. • Think of above method as shortcut for more standard way (assuming s 1 has been declared): s 1 = new String(“This is some java String”); • new operator required to create memory for new String object.
String methods • Given a String object we can then access any public String method or instance variable (field). • Best to think of analogy with C. Given a variable of some struct type, we can access any of the struct’s members. If one of these members is a pointer to a function, we can essentially call a function using the struct. (x. doit(x, …)) • In Java, this idea is taken quite a bit further, but the above analogy is a good start.
Compile and Run • Compile • javac Hello. World. java • One file named Hello. World. class is created if the compilation is succeeds. • Run • java Hello. World
����� • • • Object Oriented Both complied and interpreted Platform Independent Portable Highly Efficient Easier, Simple and Small Robust and Secure Dynamic and Extensible Distributed Multithreaded
Structure of Java Program Documentation Section Package Statements Import Statements Class Definition Inheritance Statements Interface Statements Main class Definition { Main () method definition other () methods calling }
Create a Java Source File public class Hello. World { public static void main(String[] args) { System. out. println("Hello World!"); } }
Java Development Process. java =>. class => JVM execution
The Simplest Java Application: Hello, World! • Since Java is object-oriented, programs are organized into modules called classes, which may have data in variables and subroutines called methods. Each program is enclosed in a class definition. class Hello. World { public static void main (String[] args) { System. out. println(“Hello World!”); } } The notation class. method or package. class. method is how to refer to a public method (with some exceptions). Syntax is similar to C - braces for blocks, semicolon after each statement. One difference: upper and lower case matter!
Writing first program • Then, the program must be wrapped in a class definition which is the same as the file basename (My. First. Program). Careful, Java is case-sensitive. class My. First. Program { … } • Finally, main is defined similar to C, but with a few more modifiers: public static void main(String[] args){ … } These are all required. No shortcuts.
Keyword Ø Keyword ���������� ����� , �� ������ ���� ����� Ø ����� ���� ���� Ø Example: Abstract, do, implement, private, throws, Boolean, double, import, prot ected, transient, break, else inner, public, true, byte, extends, instanceof, rest, try, byval ue, false, int, return, var
Data Types of java Program
Range of Data types: Data Type Size Description byte 1 byte Stores whole numbers from -128 to 127 short 2 bytes Stores whole numbers from -32, 768 to 32, 767 int 4 bytes Stores whole numbers from -2, 147, 483, 648 to 2, 147, 483, 647 long 8 bytes Stores whole numbers from -9, 223, 372, 036, 854, 775, 808 to 9, 223, 372, 036, 854, 775, 807 float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits boolean 1 bit Stores true or false values char 2 bytes Stores a single character/letter or ASCII values
Java Data Types • Sizes fully specified by Java standard. • Java is a very strongly typed language • Integer types • • int (4 bytes signed) short (2 bytes signed) long (8 bytes signed) use suffix L (eg 100000 L) byte (1 byte signed) • Floating-point types • float (4 bytes) use suffix F (eg 1. 28 F) • double( 8 bytes)
Additional Data Types • char • Two-byte unicode • Assignment with ‘ ‘ • e. g. char c = ‘h’; • boolean • true or false e. g. boolean x = true; if (x){…};
Java Variables are containers for storing data values. In Java, there are different types of variables, for example: • String - stores text, such as "Hello". String values are surrounded by double quotes • int - stores integers (whole numbers), without decimals, such as 123 or -123 • float - stores floating point numbers, with decimals, such as 19. 99 or -19. 99 • char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes • boolean - stores values with two states: true or false Declaring (Creating) Variables To create a variable, you must specify the type and assign it a value: Syntax type variable = value; Where type is one of Java's types (such as int or String), and variable is the name of the variable (such as x or name). The equal sign is used to assign values to the variable. To create a variable that should store text, look at the following example: Example Create a variable called name of type String and assign it the value "John":
Java Identifiers All Java variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, total. Volume). The general rules for constructing names for variables (unique identifiers) are: • Names can contain letters, digits, underscores, and dollar signs • Names should begin with a letter • Names can also begin with $ and _ (but we will not use it in this tutorial) • Names are case sensitive ("my. Var" and "myvar" are different variables) • Names should start with a lowercase letter and it cannot contain whitespace • Reserved words (like Java keywords, such as int or String) cannot be used as names
String Examples • Best to see by way of example: String s = new String(“Hello”); Char c = s. char. At(3); System. out. println(c); • Method char. At called on String object s taking single integer parameter. • How might this look in a procedural language with structures? (homework)
Operators/Control Flow • Almost exactly like regular ANSI C. • +, *, -, /, %, ++, --, +=, etc. • ==, !=, >, < , etc. • if statements, for loops, while loops, do loops, switch statements, etc. • continue, break, return, System. exit(0). • Read pp 54– in Core Java. • No need to spend class time going over these.
Operators/Control Flow • Almost exactly like regular ANSI C. • +, *, -, /, %, ++, --, +=, etc. • ==, !=, >, < , etc. • if statements, for loops, while loops, do loops, switch statements, etc. • continue, break, return, System. exit(0). • Read pp 54– in Core Java. • No need to spend class time going over these.
Statement of Java Program Control Statement of Java Divided of 2 types Ø Conditional control Statement Ø Loop control statement Conditional control Statement Ø Ø if statement if else statement else if statement switch statement Ø Ø For statement While statement Do-While statement Continue statement Loop control statement
Operator of Java Program ������� ��� ����� ������� ������� ������� ��� ������� � ������ 1. Arithmetic Operator 2. Relational Operator 3. Logical Operator 4. Bitwise Operator 5. Assignment Operator 6. Conditional Operator 7. Special Operator
Array in Java ������ ����� ������ ����� Array ��� ��� ������ ���� Syntax of Array : Data Type Array Name[]; Array Name=new Data Type[Array Size]; ���� Data Type Array Name[]=new Data Type[Array Size];
������ -� ����� , ���� � ������ Class: ����� User Define Data Type� �������� � ����� access ���� Function ����� Class Declaration: Class. Name { Member Variable Decleration; Member Function Decleration; }
������ -� ����� , ���� � ������ Object Declartion: Class � ������� Variable �� Object ��� Class �� ������� ����� ���������� Syntax of Object Declaration: Example: Or Class. Name Object. Name; Object. Name=new Class. Name(); student S; S=new student(); student S=new student();
������ -� ����� , ���� � ������ Accessing Member Function: Class � member Accessing ������� Private, Public and Protected ���� Declaration �������� � ���������� Access ������� ��� , ������� Access ����� Accessibility mode ������ 1. Accessing Private Member 2. Accessing Public Member 3. Accessing Protected Member
Method Overloading Program: class over { Int cal(int x, int y) { Int z; z=x+y; System. out. println(“Using Summation Method); System. out. println(“Result is: +z”); Return 0; } float cal(float m 1, float m 2) { float mark; Mark=m 1+m 2; System. out. println(“Using Float Method); System. out. println(“The Mark is: +mark”); Return 0; } Int cal(int x) { System. out. println(“Using Input/Output Method”); System. out. println(x); Return 0; } public class overloading { Public static void main(String[] args) { Over s=new over(); s. cal(34. 25 f, 50. 26 f); s. cal(44); s. cal(23, 10); }
������ -� ������ � ���� Classification of Inheritance: Ø Single Inheritance Ø Multilevel Inheritance Ø Hierarchical Inheritance
Hierarchical Inheritance ����� ����� ������ ����� ��� �� , ��� ������ Hierarchical Inheritance ���� Super Class Sub Class Super Class
Inheritance in Java • Java classes can be derived from other classes, thereby inheriting fields and methods from those classes.
“Multiple Inheritance”
Inheritance in Java • Java classes can be derived from other classes, thereby inheriting fields and methods from those classes.
Overrideen method in Java Class superclass{ Int n; Superclass() { } Public void getdata() { } } Class subclass extends superclass { Public void getdata() n++; System. out. println(“It is superclas”); System. out. println(“n=” +n); Int n; Subclass() { n++; } { System. out. println(“It is Sub. Class”); System. out. println(“n=” +n); } } Public class over { Public static void main(String[] agrs) { Superclass sp=new subclass( sp. getdata(); subclass sb= new subclass(); Sb. getdata(); } }
Java System package call ���� Format ���� Import java. *; Package. Name; Package pack; Public class packexample { Int roll; String name; Float mark; Public void getdata() { Roll=960041; Name=“Munny”; Mark=85. 75 f; } Public void display(){ User Define package: user ��������� System. out. println(“Roll is=” +roll); �� �� ������ System. out. println(“Name is =“ +name); ������� System. out. println(“Mark is=“ +mark); ���� ) ����� } ������� Import packs. packexample; ���� ��� Class packuse{ ����� Public static void main(String[] arges) Packexample p=new packexample(); ����� p. getdata(); ���� p. display(); } ������� }
Necessity of package declaration Ø Package �� ����������� Organize ������� folder �� ������ ����� class file �� ������ �� ��� Ø ���������� ���� Strategical �� ����� same ��� �� class ��� folder � ��� Package ����������� �� ������ ����� Ø Package �� ���� class ���� Default access modifier �� ����� Ø Package name �� ������� class identity ������ Describe the function of Different packages Java. applet Init() Java. beans Start() Bean Descriptor Stop() Encoder Destroy() Event handler Java. awt Expression Java. awt. color Introspector Java. awt. front Java. awt. event Java. awt. image Java. swing Javax. swing. border Javax. swing. event Javax. swing. preview Javax. swing. text Javax. swing. tree
Interface With Syntax Interface Student { Static final int Roll=154000; Static final String Name=“Mitali”; Void display(); } Class Result implements Student { Public float mark; Public void display() { System. out. println(“Roll is=” +Roll); System. out. println(“Name is=” +Name); } Public void getmark() { Mark=65. 45 f; } Public void showmark() { System. out. println(“Mark is=” +mark); } } Public class interf { Public static void main(String[] args) { Result R=new Result(); R. Display(); R. getmark(); R. showmark(); }
Interface
Multiple interface ��� ������ �� ������ Interface Student { ����� String Name=“Tonny”; } ����� ��� Interface Exam { �� ���� Int roll=96004; Void display(); multiple Interface } ���� Class result implements student, exam { Multiple Interface Public float mark; Public Void display() ������ { System. out. println(“Roll is=” +roll); ���� ��� System. out. println(“Name is=” +name); ����� } Public void getmark(){mark=87. 5 f; } ����� Implement Public void showmark(){System. out. println(“Mark is=“ +mark); } �� ���� �� Public class mulinterf{ Public static void main(String[] args) ������� { Result R=new Result(); � ��� R. display(); R. getmark(); ������� R. showmark(); } � ��� } ����
veral methods or Thread class with state diagra Thread State: �������� ����� State ���� State ����� Ø Born State Ø Ready State Ø Running State Ø Waiting State Ø Sleeping State Ø Dead State
Way to Create Thread Class My. Thread extends Thread { Public void run() { For(int i=1; i<=5; i=i+2) System. out. println(“Inside Thread A”); } Extend method: �� } ���� Java. lang ����� Class Example. Thread ������� Extend { ������ ��� �� , ��������� Public static void main(String[] agrs) { ������� Run() method override ��� My. Thread Th=new My. Thread(); ������� Main() method System. out. println(“Starting My. Thread”); ������ Th. start(); Thread Class �� Start() Method ������� System. out. println(“Exit from Main Thread”); ����� Run() method } call ��� } ������� Thread Class ���� Ø ������ Thread Class Extend ���� Ø Runable Class implement ����
Way to Create Thread Implement method: Class A implement Runnable �� ���� Runnable interface �� { Public void run() ����� ���� class Define { ��� �� ���� run() for(int i=1; i<=5; i=i+2) method �� ������� override { ��� ����� System. out. println(“Inside Thread A: i=” +i); main() method } ���� main thread � ������ System. out. println(“Exit from A”); ������� } ����� ��� �� , �� Thread class Class B implement Runnable object declaration �� { ��������� Public void run() constructor method �� argument ������ { ������� for(int j=2; i<=5; i=j+2) ������ Thread { Class �� object �� ������� start() System. out. println(“Inside Thread B: j=” +j); method call ��� } ����� thread class � ������ System. out. println(“Exit from B”); run()method call ��� } ��� } Class Example { Public static void main(String[] agrs) { A obj A=new A(); Thread Th 1=new Thread(obj A); B obj B=new B(); Thread Th 2=new Thread(obj B); System. out. println(“Starting Thread A: ”); Th 1. start(); System. out. println(“Starting Thread B: ”); Th 2. start(); System. out. println(“Exit From Main Thred: ”); } }
Java Threading • A thread is a thread of execution in a program [6] • JVM allows an application to have multiple threads running concurrently. • Apache Harmony example:
How to stop a Thread • Using Thread. interrupt() method:
Java Threading • A thread is a thread of execution in a program [6] • JVM allows an application to have multiple threads running concurrently. • Apache Harmony example:
������ -� ����� /������ • Stream: Java program � i/o operation ������� �������� Stream ���� Stream � ������ • I/O Stream Class • File System Class Byte Stream Class: Byte ������ input/output Provide ���� Byte Stream ���������� Binary Data Reading and Writing operation �� ��� Byte Stream ������� Character Stream Class: Input Output Character data operation �� ���� Character Stream ������� ���
1. Extends Thread class 2. Implements Runnable interface
Thread lifecycle
Reading console input and Writing console output Reading console input: ���� �. � �� ����� � ���� ������� ��� ������� ����� ������� ������� �������� Reading console input �� ����� ����� Character oriented Stream, �� program maintenance ��� ���� java �� System. in ��� Reading �� ������� console input ���� ����� �� ������� Character Based Stream ������ Buffered Reader Object �� ����� System. in � Wrap ���� Buffered Reader Buffered Input System �� Support ���� Writing console ouput: Console output ������� Print() and Println() �� ������ �� ����� Print. Stream class �� ������� �� , �� System. out object refer ���� System. out ���� Byte Stream ���� �� ������� ������ ��� ���� Printstream,
Input and Output Operatin Input Device Input Stream Program File Writer Stream Data File �� Input Operation process Data File Reader Stream Program Output Stream Data File �� Output Operation process Output Device
Type-1(JDBC & ODBC BRIDGE)
Type-2 Driver (Native API Driver)
Type-3 Driver (Network Protocaol Driver)
Type-4 Driver (Database protocol Driver)
Architecture of TCP/ IP Protocol
- Slides: 98