PROGRAMMING IN UNIT-I

AN OVERVIEW OF JAVA:

, Patrick Naughton, Chris Worth, ED Frank and Mike Sheridan conceived Java at , Inc in 1991.  It took 18 months to develop the first Working version. This language was initially called ‘OAK’ but was renamed “JAVA” in 1995.  The primary motivation was the need for a platform-independent (i.e. architecture- neutral) language that could be used to create software to be embedded in various consumer electronic devices such as microwave oven and remote controls.  As you can probably guess, many different types of CPU’s are used as controllers. The trouble with and C++ (and most other languages) is that they are designed to be compiled for a specific target.  Although it is possible to compile a C++ program for just about any type of CPU, to do so requires a full C++ compiler together for that CPU.  The problems are that compilers are expensive and time consuming to create. An easier- more cost efficient solution was needed.  In an attempt to find such a solution, Gosling and others began work on a portable, platform-independent language that could be used to produce code that would run on a variety of CPU’s under different environments.  This effort ultimately led to the creation of Java.  In 1993, the World Wide Web (WWW) appeared on the Internet and transformed the text-based Internet into a graphical rich environment.  The project team came up with the idea of developing web applets (tiny programs) using the new language that could run on all types of computers connected to the Internet.  In 1994, the team developed a web browser called “Hot java” to locate and run applet program on Internet.  Hot java demonstrated the power of new language, thus making it instantly popular among the Internet users.  So, while the device for an architecture-neutral programming language provided the initial spark, the Internet ultimately led to Java’s large-scale success.  Java is influenced by C++ it is not an enhanced version of C++.

Difference between JAVA and C++

 JAVA does not support operator overloading.  JAVA does not have template classes as in C++  JAVA does not support multiple Inheritances of classes. This is accomplished using a new feature called “Interface”.  JAVA does not support Global variables. Every variable and method is declared within a class and forms part of that class.  JAVA does not use pointers.  There are no header files in JAVA.  JAVA has replaces the destroy function with a finalize () function.

FUNDAMENTALS OF OBJECT ORIENTED PROGRAMMING

 OBJECT ORIENTED PROGRAMMING is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand.  Object oriented program organizes a program around its data and a set of well-defined interfaces to that data.  An object oriented programming can be characterized as data controlling access to code.

Some of the features of object-oriented paradigm are:

(i) Emphasis is on data rather than procedure. (ii) Programs are divided into what are known as “objects”. (iii) Data structures are designed such that they characterize the objects. (iv) Methods that operate on the data of an object are tied together in the data structure. (v) Data is hidden and cannot be accessed by external functions. (vi) Objects may communicate with each other through methods. (vii) New data and methods can be easily added whenever necessary. (viii) Follows bottom-up approach in program design.

Method Method

Data

Method Method

Object=Data+Methods

BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING

OBJECTS

 Objects are the basic runtime entities in an object-oriented system. This may represent a person, a place, a bank account or any item that the program may handle.  When a program is executed, the objects interact by sending messages to one another.  For example ‘customer’ and ‘account’ are two objects in a banking program, then the customer object may send a message to the account object requesting for the balance.  Each object contains data and code to manipulate the data.

PERSON ------ OBJECT NAME DATA ROLLNO MARK1 MARK2 TOTAL( ) METHODS

CLASSES: Class is a collection of objects. Once a class has been defined, we can create any number of objects belonging to that class. Fruit(class)----> mango(object)

DATA ABSTRACTION AND ENCAPSULATION

 The wrapping up of data and methods into a single unit (class) is known as Encapsulation.  These methods provide the interface between the object’s data and program. The insulation of the data from direct access by the program is called data hiding.  ABSTRACTION refers to the act of representing essential features without including the background details or explanations.

INHERITANCE

 It is the process by which one object acquires the properties of another object..  In oop, the concept of inheritance provides the idea of reusability; this means that we can additional features to an existing class without modifying it.  This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes.

POLYMORPHISM

 Polymorphism is another important oop concept. It means the ability to take more than one form.  More generally, the concept of polymorphism is often expressed by the phrase “ one interface multiple methods”.  Single Function name perform different task is called function overloading.

Shape

Draw()

Circle Box Triangle

Draw(circle) Draw(box) Draw(triangle)

DYNAMIC BINDING

 Binding refers to the linking of a procedure call to the code to be executed in response to the call.  Dynamic binding means that the code associated with a given procedure call is not know until the time of the call at runtime.

MESSAGE COMMUNICATION

 An object-oriented program consists of a set of objects that communicate with each other. 1. Creating classes that defines objects and their behaviour. 2. Creating objects from class definitions. 3. Establishing communication among objects.

Message

Sending Object Receiving Object

BENEFITS OF OOPS

1. Through inheritance, we can eliminate redundant code and extend the use of existing classes. 2. We can build programs from the standard modules that communicate with each other, rather than having to start writing the code from the scratch. This leads to saving development time and higher productivity. 3. The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program. 4. It is possible to have multiple objects to co-exist without any interference. 5. It is possible to map objects in the problem domain to those objects in the program. 6. It is easy to partition the work in a project based on objects. 7. The data centered design approach enables us to capture more details of a model in an implement able form. 8. Object oriented systems can be easily upgraded from small to large systems. 9. Software complexity can be easily managed.

APPLICATIONS OF OOP

(i). Real systems (ii). Simulation and modeling (iii). Object oriented databases. (iv). Hypertext, hypermedia and expertext (v) AI and expert systems (vi). Neural networks and parallel programming. (vii). Decision support and office automation system. (viii). CAD/CIM systems

JAVA FEATURES

1.simple. 2.object oriented 3.Compiled and interpreted. 4.robust. 5.secured. 6.architecture neutral. 7.portable 8.high performance 9.multi threaded. 10.Dynamic.

Simple: o Java is a small and simple language. o Java inherit c syntax and object oriented features of c++. Object oriented: o Java is a true object oriented language. o All program code and data should be within classes and object. Complied and interpreted: o A computer language is either complied or interpreted.java combines both these approaches. o Java compiler translates source code into bytecode. o Java interpreted translate bytecode into machine code.

Robust: o Java is a robust language. o It is designed for relieving memory management problem and mishandling exception. Secured:

ARCHITECTURE NEUTRAL. o Platform independent- Java programs can be easily moved from one computer system to another, anywhere and anytime. o Changes and Upgrades in operating systems, processors and system resources will not force any changes in Java programs.

PORTABLE

 Java ensures portability in two ways.  First java compiler generates Byte code instructions that can be implemented on any machine.  Secondly, the sizes of the primitive data types are machine independent.

Multithreaded and Interactive

 Multithreaded means handling multiple tasks simultaneously. Java supports multithreaded programs.  This means that we need not wait for the application to finish one task before beginning another. Dynamic and Extensible  Java is a Dynamic language. Java is capable of dynamically linking in new class libraries, methods and objects.  Memory allocation and deallocation in run time

Java Environment  Java environment includes a large number of development tools, and hundreds of classes and methods.  The development tolls are part of the system known as (JDK) and the classes and methods are part of the Java Standard Library (JSL) also known as API (Application Programming Interface). E.g.  AppletViewer (for viewing java applets).  javac (java compiler)  java (java Interpreter, which runs applets and application by reading and interpreting byte code files)  javah (produces header files for use with native methods)

Text Editor

Java Source HTML Code javadoc Files

javac

Header javah Files

java jdb

Java Program Output

 javap(java dissembler, which enables us to convert bytecode files into a program description)  jdb (java debugger, which helps us to find errors in our programs)

DATA TYPES:

o The datatype is used for storing the values. o If a value become too big for variable type,it is truncated.

INTEGERS:

 Java defines four integer types byte, short, into long. All of these are signed , positive and negative values. Long- eight bytes Int- four bytes Short- two bytes Byte- one byte.

FLOATING POINT TYPES:

Integer types can hold only whole numbers and therefore we use another type known as floating point type to hold numbers containing fractional parts such as 23.45. There are two types of floating point storage.

Float-4 bytes Double-8 bytes

CHARACTER TYPE:

 In order to store character constants in memory, java provides a character data type called char.  The char type assumes a size of 2 bytes but, basically it can hold only single character.  Java support Unicode character set. It define all of the international alphabets.

BOOLEAN TYPE  Boolean type is used when we want to test a particular condition during the execution of the program. The two values that a Boolean type can take: true or false. JAVA TOKENS:

RESERVED KEYWORDS  Java language has 60 reserved words as keywords.  Keywords have specific meaning in java; we cannot use them as names for variables, methods and so on.  Java is case sensitive. All keywords are to be written in lowercase letters. E.g. native case

Eg Auto int Void long Goto float Do Char IDENTIFIERS:

It refer to the name of variables, functions & arrays. Both lowercase & uppercase are permitted. It cannot use a keyword. Eg: Name, area, sum…..

CONSTANT:

It refer to fixed data value that do not change during the execution of a program.

Constants

Numeric Constant Character Constant

Integer Real Single String Constant Constant Character Constant Constant INTEGER CONSTANT:

It refer to a sequence of digit. Three types of integers 1. Decimal 2. Hexadecimal 3. Octal DECIMAL INTEGER:

It consist of digit from 0 to 9. eg 1789.

HEXADECIMAL :

It must begin with either 0x or 0x and then followed by any digits from 0 to 9. eg-0x123,0xabcd.

OCTAL INTEGER CONSTANT:

It contain the digit from 0 to 7 eg 0777.

REAL CONSTANT OR FLOATING POINT CONSTANT:

o The numbers are represented by fractional part like 17.548. This is called floating point constant. eg 0.86

o A real number may also be expressed in exponential notation.

General Format:

Mantissa e exponent

The mantissa may be either decimal or integer.

The exponent is an integer number with an optional plus or minus sign. The letter ‘e’ separating the mantissa.

Eg.0.65e4

SINGLE CHARACTER CONSTANT:

It is single character enclosed with single quotes.

Eg.’k’

STRING CONSTANT:

A string constant is an sequence of character enclosed with in double quotes.

Eg. “hello”.

BACKSLASH CHARACTER CONSTANTS:

 Java supports some special backslash character constants that are used in o/p methods. Ex: ‘\b’ Backspace ‘\n’ Newline ‘\t’ Horizontal Tab

VARIABLES: It is a data name that may be used to store a data value. A variable name can be chosen by the programmer.

Eg.total Sum. RULES:  The must be begin with letter and remaining character may be alphabets or digits.  It should not be a keyword.  White space is not allowed.  A maximum of 8 should be allowed.  Upper case and lower case are significant.

DECLARATION OF VARIABLES:

The declaration statement tells the complier 2 things. 1)It tells the complier what the variable name is. 2)It specify what type of data variable name is.

General format:

Data type v1,v2……vn:

Eg: int count; int number, total;

ASSIGNING VALUES THE VARIABLES:

Value can be assigned to variables using the assignment operator ‘=’.

General format:

Data type variable name = constant(data value);

Eg: int X=10; int N= ‘a’;

 The left-hand side of an assignment statement is variable name and right-hand side of an assignment statement is value.

 It is possible to use multiple assignment operators.

Eg a=b=10;

LITERALS

 Literals in java are a sequence of characters (digits, letters and other characters) that represent constant values to be stored in variables.  Java language specifies five major types of literals. They are: a) Integer literals b) Floating-point literals c) Character literals d) String literals e) Boolean literals INTEGER LITERALS:

It refer to a sequence of digit.

Three types of integers

1. Decimal 2. Hexadecimal 3. Octal

DECIMAL LITERALS

It consist of digit from 0 to 9. eg 1789.

HEXADECIMAL LITERALS

It must begin with either 0x or 0x and then followed by any digits from 0 to 9. eg-0x123,0xabcd.

OCTAL INTEGER LITERALS

It contain the digit from 0 to 7 eg 0777.

FLOATING POINT LITERALS:

o The numbers are represented by fractional part like 17.548. This is called floating point literal. eg 0.86f

SINGLE CHARACTER LITERALS:

It is single character enclosed with single quotes.

Eg.’k’

STRING LITERALS:

A string constant is an sequence of character enclosed with in double quotes.

Eg. “hello”.

BOOLEAN TYPE  Boolean type is used when we want to test a particular condition during the execution of the program. The two values that a Boolean type can take: true or false.