Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Java Complete

Java Complete

Published by Teamlease Edtech Ltd (Amita Chitroda), 2021-07-06 08:16:20

Description: Java Complete

Search

Read the Text Version

Java Slide 1 of 92 Copyright © 2020

History • 1991 - Oak • 1995 - Java • Developers - James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan • @ Sun Microsystems, Inc. Slide 2 of 92 Copyright © 2020

Why Java ??? Copyright © 2020 • Simple • Secure • Portable • Object-oriented • Robust • Multithreaded • Architecture-neutral • Interpreted • High performance • Distributed • Dynamic Slide 3 of 92

Types of Java • Java SE • Desktop application development • Java EE • Web application development • Java ME • Mobile application development Slide 4 of 92 Copyright © 2020

How Java Works Java Source Code .java javac Byte Code .class JVM Windows Linux Unix Slide 5 of 92 Copyright © 2020

• JDK – Java Development Kit • JVM – Java Virtual Machine • JRE – Java Runtime Environment • API – Application Programming Interface Slide 6 of 92 Copyright © 2020

Keyword abstract continue goto package synchronized assert default this boolean do if private throw break double throws byte else implements protected transient case extends try catch final import public void char finally volatile cass float instanceof return while int short interface static long strictfp native super const for new switch Slide 7 of 92 Copyright © 2020

First Java Program public class FirstPrg { public static void main (String[] args) { System.out.println(―My first JAVA program.\"); } } Slide 8 of 92 Copyright © 2020

Compile & Run Program • Compile • javac FirstPrg.java • Run • java FirstPrg Slide 9 of 92 Copyright © 2020

Naming Conventions • Class/Interface - ProperCase • Variable/Object - camelCase /Method • Package - lowercase • Constant - UPPER_CASE Slide 10 of 92 Copyright © 2020

File Naming Rules & Comment • Public class name MUST be filename • Comment Copyright © 2020 • /* This is * Documentation * Comment */ • // Single line Comment • /* Multi Line Comment */ Slide 11 of 92

Java identifiers • Class name • Method name • Variable name • Identifier must start with alphabet, $, _ Slide 12 of 92 Copyright © 2020

Literal Copyright © 2020 • Character • ‗A‘, ‗9‘, ‗$‘ • Numeric • 3, 4.5, -99.8 • String • ―Hello‖, ―55.5‖, ―6.7$‖ • Logical • true, false Slide 13 of 92

Data types Type Byte Range byte 1 -27 to 27-1 short 2 -215 to 215-1 char 2 4 -231 to 231-1 int 4 -1.4*1045 to 3.4*1038 float 8 -263 to 263-1 long 8 -4.9*10324 to 1.8 * 10308 double true / false boolean Slide 14 of 92 Copyright © 2020

Working with variable • Variable declaration • Datatype varName; • Value assignment • varName = value; • JVM will not initialise local variables. • Local variable‘s life span – block in which declared Slide 15 of 92 Copyright © 2020

Hands on System.out.println public class SopClass { public static void main(String[] args) { int a=10, b=20; System.out.println(―a+b =― + a+b); System.out.println(a + ― + ― + b + ― = ― + a+b); System.out.println(a + b + ― = ― + a+b); } Slide 16 of 92 Copyright © 2020

Operators • Arithmetic (+, -, *, /) • Modulus (%) • Assigment (=) • Relational (>,>=, <, <=, ==, !=) • Logical (&&, ||, !) • Increment (++) • Decrement (--) • Binary (~, &, |, >>, <<, >>>) • Ternary (? : ) Slide 17 of 92 Copyright © 2020

Selection Copyright © 2020 • if( condition) { statement1; statement2; } • if(condition) statement; else statement2; Slide 18 of 92

Selection contd… • switch(var) { case const1 : statement1; statement2; break; case const2 : statement3; statement4; break; ……………………………… ……………………………… default : statement } Slide 19 of 92 Copyright © 2020

Iteration / Loop Copyright © 2020 • while (condition) { statement1; statement2; } • do { statement1; statement2; }while(condition); Slide 20 of 92

Iteration / Loop • for (initial value; condition; expression) { statement1; statement2; } • for (datatype var : array/collection) { statement1; statement2; } Slide 21 of 92 Copyright © 2020

Working with loops • continue • break Slide 22 of 92 Copyright © 2020

OOP Object Oriented Concepts Slide 23 of 92 Copyright © 2020

Object Oriented Programming OOP Designed to model real world All real world things concepts using a computer program are considered as objects in OOP Enables modeling of real world Method of designing and entities into similar entities implementing software systems using a computer Slide 24 of 92 Copyright © 2020

Object Object Core of Object Represents an oriented entity in the real “An objpercotgisraamcmoinncgrete entity that exists and which hawsoarlwd ell defined state and behavior.” Provides practical Accomplishes basis for computer specific tasks applications Slide 25 of 92 Copyright © 2020

Example of an Object Cashier Object Customer Object State Behavior Slide 26 of 92 Copyright © 2020

Message Passing Message Passing Objects communicate When a particular operation with each other by is to be performed, it is passing messages requested for by sending a message to the object for which the operation is define “A message is a request sent by one object to another object to carry out certain actions.” Slide 27 of 92 Copyright © 2020

Class  A Class defines an entity in terms of common characteristics and actions.  Class is a mechanism used to group properties of actions common to various objects. Examples of Classes Class of Cars Class of Shapes Class of Animals “A class is a blueprint for a group of objects that have common properties and behavior.” Slide 28 of 92 Copyright © 2020

Example of Class and Object Employee class object object object object object Cashier System Stock Salesman Purchase Administrator Manager Manager Slide 29 of 92 Copyright © 2020

Properties or Attributes • Characteristics of objects represented as variables in a class. • Each object has its own value for each of its properties. • The property names are shared by all instances of a class. Name Salesman Object Address Properties Age “A characteristic possessed by an object or entity when represented in a class is called a property.” Slide 30 of 92 Copyright © 2020

Methods Methods Actual implementation of an by Methods specify the manner in operation which the data of an object is “An action performed an object imsakninpuolawtend as a method.” Specification of how the requested operation is carried out Slide 31 of 92 Copyright © 2020

Example of Method Method stitchClothes Take Steps Measurement for stitching Stitch clothes clothes Get Instruments Slide 32 of 92 Copyright © 2020

Difference between Class and Objects Class and Objects Class defines an Object is the actual entity entity Class is a conceptual model that All objects belonging to the defines all the characteristics same class have the same and actions required of an characteristics and actions object Slide 33 of 92 Copyright © 2020

Encapsulation • Information Hiding • Hiding implementation details of an object from its user • Packing things together and presenting them in their new integrated form. • For example, two or more chemicals form a capsule • Packing the methods and attributes together in a single unit. • Units are implemented in the form of classes “The process of hiding attributes, methods or details of implementation is called Encapsulation.” Slide 34 of 92 Copyright © 2020

Example of Encapsulation 3-1 Auti Ltd. Buto Ltd. Requirements Ms Samanta Mr. James Marketing Manager Purchase Manager Interfaces Copyright © 2020 Slide 35 of 92

Example of Encapsulation 3 - 2 Auti Ltd. Buto Ltd. PUBLIC PUBLIC Phone No Phone No E-mail Id E-mail Id Catalog of Products Catalog of Cars PRIVATE PRIVATE How spare parts are manufactured How cars are assembled Stock of spare parts Stock of cars Cost for creating spare parts Cost for assembling cars Selective Availability of Data Slide 36 of 92 Copyright © 2020

Example of Encapsulation 3 - 3 Entity Auti Ltd. EntityButo Ltd. PROPERTIES PROPERTIES Phone No E- mail Id Phone No Catalog of Products E- mail Id Catalog of Cars Current StockAQuutianLttitdy. Car SpecificaBtiountso Ltd. Employee details Material required details Employee Details Stock details METHODS Dealers details Receive Orders How spare parts are manufactured METHODS Calculating cost for creating spare Place Orders parts How cars are assembled Calculating profit margin Calculating cost for assembling cars Calculating salaries Placing an order Creating necessary reports Slide 37 of 92 Copyright © 2020

Abstraction Technique of dealing Focussing only on the with the essential details complexity of an and overlooking the object. non-essential details of an object. Slide 38 of 92 Copyright © 2020

Example of AbstractioCarnrier Courier Company Hands over report Dr. Smith Hands over acknowledgement receipt Gets acknowledgemen Report is t receipt signed packed and sealed Report sent to destination Slide 39 of 92 Copyright © 2020

Data Abstraction • Concept of abstraction applied to the attributes (data) of a class. • Implemented in programming languages using Abstract Data Types (ADT). ―The process of identifying and grouping attributes and actions related to a particular entity as relevant to the application is Data Abstraction.‖ Slide 40 of 92 Copyright © 2020

Example of Data Abstraction Class Student Attributes Name Roll Number Seat Number Ranking Methods GiveExam() AttendClasses() FillExamForm() Slide 41 of 92 Copyright © 2020

Implementing Classes in Java Syntax class <classname> { <body of the class> } where, class is the keyword used for creating a class, <classname> is the name of the class, and <body of the class> consists of declaration of attributes and methods. Slide 42 of 92 Copyright © 2020

Methods in classes 5-1 Name of the method Type of object or Method Body of the primitive type Definition method that the method returns List of parameters Copyright © 2020 Slide 43 of 92

Methods in classes 5-2 Syntax <returntype> <methodname> (<type1> <arg1>, <type2> <arg3,…) { <set of statements> } where, returntype is the data type of the value returned by the method, <methodname> is the user-defined name of the method, and method’s parameter list is a set of variable declarations. Slide 44 of 92 Copyright © 2020

Methods in Classes 5-3 class Book { String bookName; String authorName; int nopages; method boolean available; void isAvailable() { if(available == true) System.out.println(\"The Book is available\"); } … Slide 45 of 92 Copyright © 2020

Methods in Classes 5-4 • Methods are accessed using dot notation. • Object whose method is called is on the left of the dot, while the name of the method is on the right. • For Example, Obj.isAvailable(); • Java provides class methods, which are similar to instance methods. • Class method declaration is preceded with a static keyword. Slide 46 of 92 Copyright © 2020

Methods in Classes 5-5 class Book { String bookName; String authorName; int nopages; boolean available; } static void isAvailable() { if(available == true) System.out.println(\"The Book is available\"); } Book objBook = new Book(); Dot notation objBook.isAvailable(); ….. Copyright © 2020 Slide 47 of 92

this Keyword • Used inside any instance method to refer to the current object. • The value of this refers to the object on which the current method has been called. • The this keyword can be used where a reference to an object of the current class type is required. Slide 48 of 92 Copyright © 2020

Example of this Keyword Reference to an object class pixel { int x,y; void init (int x, int y) { this.x = x; this.y = y; } } public static void main (String args[]) { pixel p = new pixel(); p.init (4,3); } The program initializes x = 4 and y = 3. Slide 49 of 92 Copyright © 2020

Constructors 4-1 • Method invoked whenever an instance of a given class is created. • Same name as the class and no return type. • Java allocates memory for the object, initializes the instance variables and calls the constructor methods. • Two types of constructors • Parameterized constructors • Implicit constructors Slide 50 of 92 Copyright © 2020


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook