Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming: Understanding Object-Oriented Programming and Java Features, Lecture notes of Java Programming

An introduction to Java programming, focusing on the concept of Object-Oriented Programming (OOPs) and the unique features of the Java language. It covers the differences between OOP and Procedural Oriented Programming, the benefits of using OOPs, and a brief history of Java. Additionally, it discusses the Java environment, including the Java Development Kit (JDK) and the Java Standard Library (JSL), as well as the process of creating and executing a Java program.

Typology: Lecture notes

2020/2021

Uploaded on 09/06/2021

dinesh-paul
dinesh-paul 🇮🇳

4 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction:
Java is a high-level, third generation programming language, like C, FORTRAN, Smalltalk,
Perl, and many others. You can use Java to write computer applications that play games, store
data or do any of the thousands of other things computer software can do. Compared to other
programming languages, Java is most similar to C. However although Java shares much of C's
syntax, it is not C. Knowing how to program in C or, better yet, C++, will certainly help you to
learn Java more quickly, but you don't need to know C to learn Java. A Java compiler won't
compile C code, and most large C programs need to be changed substantially before they can
become Java programs. What's most special about Java in relation to other programming
languages is that it lets you write special programs called applets that can be downloaded from
the Internet and played safely within a web browser. Java language is called as an Object-
Oriented Programming language and before beginning for Java, we have to learn the concept of
OOPs(Object-Oriented Programming)
Paradigms of Programming Language:
Paradigms can also be termed as method to solve some problem or do some task.
Programming paradigm is an approach to solve problem using some programming language or
also we can say it is a method to solve a problem using tools and techniques that are available
to us following some approach. There are lots for programming language that are known but all
of them need to follow some strategy when they are implemented and this
methodology/strategy is paradigms. Apart from varieties of programming language there are
lots of paradigms to fulfil each and every demand. They are discussed below:
UNIT - I : Introduction to OOPS: Paradigms of Programming Languages – Basic concepts of Object Oriented
Programming – Differences between Procedure Oriented Programming and Object Oriented programming -
Benefits of OOPs Application of OOPs. Java: History Java features Java Environment JDK API.
Introduction to Java: Types of java program Creating and Executing a Java program – Java Tokens- Java
Virtual Machine (JVM) – Command Line Arguments –Comments in Java program.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Java Programming: Understanding Object-Oriented Programming and Java Features and more Lecture notes Java Programming in PDF only on Docsity!

Introduction: Java is a high-level, third generation programming language, like C, FORTRAN, Smalltalk, Perl, and many others. You can use Java to write computer applications that play games, store data or do any of the thousands of other things computer software can do. Compared to other programming languages, Java is most similar to C. However although Java shares much of C's syntax, it is not C. Knowing how to program in C or, better yet, C++, will certainly help you to learn Java more quickly, but you don't need to know C to learn Java. A Java compiler won't compile C code, and most large C programs need to be changed substantially before they can become Java programs. What's most special about Java in relation to other programming languages is that it lets you write special programs called applets that can be downloaded from the Internet and played safely within a web browser. Java language is called as an Object- Oriented Programming language and before beginning for Java, we have to learn the concept of OOPs(Object-Oriented Programming) Paradigms of Programming Language: Paradigms can also be termed as method to solve some problem or do some task. Programming paradigm is an approach to solve problem using some programming language or also we can say it is a method to solve a problem using tools and techniques that are available to us following some approach. There are lots for programming language that are known but all of them need to follow some strategy when they are implemented and this methodology/strategy is paradigms. Apart from varieties of programming language there are lots of paradigms to fulfil each and every demand. They are discussed below: UNIT - I : Introduction to OOPS: Paradigms of Programming Languages – Basic concepts of Object Oriented Programming – Differences between Procedure Oriented Programming and Object Oriented programming - Benefits of OOPs – Application of OOPs. Java: History – Java features – Java Environment – JDK – API. Introduction to Java: Types of java program – Creating and Executing a Java program – Java Tokens- Java Virtual Machine (JVM) – Command Line Arguments –Comments in Java program.

Need for OOP Paradigm  OOP is an approach to program organization and development, which attempts to eliminate some of the drawbacks of conventional programming methods by incorporating the best of structured programming features with several new concepts.  OOP allows us to decompose a problem into number of entities called objects and then build data and methods (functions) around these entities.  The data of an object can be accessed only by the methods associated with the object. OOPs (Object Oriented Programming System) Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts: o Object o Class o Inheritance o Polymorphism

meow, dog barks woof etc. Abstraction Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing. In java, we use abstract class and interface to achieve abstraction Encapsulation Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines. A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here. Procedural Oriented Programming Object Oriented Programming In procedural programming, program is divided into small parts called functions. In object oriented programming, program is divided into small parts called objects. Procedural programming follows top down approach. Object oriented programming follows bottom up approach. There is no access specifier in procedural programming. Object oriented programming have access specifiers like private, public, protected etc. Adding new data and function is not easy. Adding new data and function is easy. Procedural programming does not have any proper way for hiding data so it is less secure. Object oriented programming provides data hiding so it is more secure. In procedural programming, overloading is not Overloading is possible in object oriented

Procedural Oriented Programming Object Oriented Programming possible. programming. In procedural programming, function is more important than data. In object oriented programming, data is more important than function. Procedural programming is based on unreal world. Object oriented programming is based on real world. Examples: C, FORTRAN, Pascal, Basic etc. Examples: C++, Java, Python, C# etc. Benefits of OOP  We can build the programs from standard working modules that communicate with one another, rather than having to start writing the code from scratch which leads to saving of development time and higher productivity,  OOP language allows to break the program into the bit-sized problems that can be solved easily (one object at a time).  The new technology promises greater programmer productivity, better quality of software and lesser maintenance cost.  OOP systems can be easily upgraded from small to large systems.  It is possible that multiple instances of objects co-exist without any interference,  It is very easy to partition the work in a project based on objects.  It is possible to map the objects in problem domain to those in the program.  The principle of data hiding helps the programmer to build secure programs which cannot be invaded by the code in other parts of the program.  By using inheritance, we can eliminate redundant code and extend the use of existing classes.

They are also called Object Database Management Systems (ODBMS). These databases store objects instead of data, such as real numbers and integers. Objects consist of the following: Attributes: Attributes are data that define the traits of an object. This data can be as simple as integers and real numbers. It can also be a reference to a complex object. Methods: They define the behavior and are also called functions or procedures.

3. Object-Oriented Databases These databases try to maintain a direct correspondence between the real-world and database objects in order to let the object retain its identity and integrity. They can then be identified and operated upon. 4. Real-Time System Design Real-time systems inherent complexities that make it difficult to build them. Object-oriented techniques make it easier to handle those complexities. These techniques present ways of dealing with these complexities by providing an integrated framework, which includes schedulability analysis and behavioral specifications. 5. Simulation and Modeling System It’s difficult to model complex systems due to the varying specification of variables. These are prevalent in medicine and in other areas of natural science, such as ecology, zoology, and agronomic systems. Simulating complex systems requires modeling and understanding interactions explicitly. Object-oriented programming provides an alternative approach for simplifying these complex modeling systems. 6. Hypertext and Hypermedia OOP also helps in laying out a framework for hypertext. Basically, hypertext is similar to regular text, as it can be stored, searched, and edited easily. The only difference is that hypertext is text with pointers to other text as well.

Hypermedia, on the other hand, is a superset of hypertext. Documents having hypermedia not only contain links to other pieces of text and information but also to numerous other forms of media, ranging from images to sound.

7. Neural Networking and Parallel Programming It addresses the problem of prediction and approximation of complex time-varying systems. Firstly, the entire time-varying process is split into several time intervals or slots. Then, neural networks are developed in a particular time interval to disperse the load of various networks. OOP simplifies the entire process by simplifying the approximation and prediction ability of networks. 8. Office Automation Systems These include formal as well as informal electronic systems primarily concerned with information sharing and communication to and from people inside and outside the organization. Some examples are: **Email Word processing Web calendars Desktop publishing

  1. CIM/CAD/CAM Systems** OOP can also be used in manufacturing and design applications, as it allows people to reduce the effort involved. For instance, it can be used while designing blueprints and flowcharts. OOP

Features of Java: I. Simple: Java was designed to be easy for professional programmers to learn, if he already knows C &C++. Java includes syntaxes from C and object oriented concepts from C++. The confusing concepts in both C & C++ are leftover here. So java is easy to learn and it is simple language. II. Secure: Security becomes an important issue for a language that is used for programming on internet. Threat of viruses and abuse of resources are everywhere. Java systems not only verify all memory access but also ensure that no viruses are communicated with an applet. The absence of pointers in java ensures that programs can’t gain access to memory locations without proper authorization. III. Portable: The most significant contribution of java over other languages is its portability. Java programs can be easily moved from on computer to another, anywhere and anytime. Changes and upgrades in operating systems, processors and system resources will not force any

changes in java programs. This is the reason why java has become popular language for programming on internet which interconnects different kinds of systems worldwide. IV. Object-oriented: java is a true object oriented language. Almost everything in java is an object. All program code and data reside within objects & classes. Java comes with an extensive set of classes, arranged in packages, that we can use in our programs by inheritance. The object Model in java is simple and easy to extend. V. Robust: Java is a robust language. It provides many safeguards to ensure reliable code. It has strict compile time and runtime checking for data types. It is designed as a garbage collected language relieving the programmers virtually all memory management problems. Java also incorporates the concept of exception handling which captures serious errors and eliminates any risk of crash the system. VI. Multithreaded: It means handling multiple tasks simultaneously. Java supports multithreaded programs. This means that we need not wait for the application to finish one task before another. For eg, we can listen to an audio clip while scrolling a page and at the same time download an applet from a distant computer. This feature greatly improves the interactive performance of graphical applications. VII. Architectural Neutral: One of the problems facing by programmers was program written today will not be run tomorrow even in the same machine or if the OS or if the processor upgrades. So java designers made it architectural neutral by implementing JVM (Java Virtual Machine) through java runtime environment. The main role of java designers to make it architecture neutral write once, run anywhere, anytime forever. VIII. Compiled & Interpreted: Usually a computer language is either compiled or interpreted. Java combines both these approaches thus making java a two stage system. First java compiler translates source code into what is known as byte code. Byte codes are not machine instructions and therefore, in second stage java interpreter generates machine code that can be directly executed by the machine that is running the java program. So we can say that java is both compiled and interpreted language. IX. High Performance: Java performance is impressive for an interpreted language, mainly due to the use of intermediate byte code. Java architecture is also designed to reduce overheads

Java Development kit (JDK) – The JDK comes with a set of tools that are used for developing and running Java program. It includes:

**1. Appletviewer ( It is used for viewing the applet)

  1. Javac (It is a Java Compiler)
  2. Java (It is a java interpreter)
  3. Javap (Java diassembler,which convert byte code into program description)
  4. Javah (It is for java C header files)
  5. Javadoc (It is for creating HTML document)
  6. Jdb (It is Java debugger)** For compiling and running the program we have to use following commands: a) javac (Java compiler) In java, we can use any text editor for writing program and then save that program with ”.java ” extension. Java compiler convert the source code or program in bytecode and interpreter convert “.java” file in “class” file. Syntax: C:\javac filename.java If my filename is “abc.java” then the syntax will be C:\javac abc.java b) java(Java Interpreter) As we learn that, we can use any text editor for writing program and then save that program with “.java” extension. Java compiler convert the source code or program in bytecode and interpreter convert “.java” file in “.class” file. Syntax: C:\java filename If my filename is abc.java then the syntax will be C:\java abc Introduction to Java: JAVA was developed by James Gosling at Sun Microsystems Inc. in the year 1991 , later acquired by Oracle Corporation. It is a simple programming language. Java makes writing,

compiling, and debugging programming easy. It helps to create reusable code and modular programs. Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++. Types of java Programs: Java is a robust, general-purpose, high-level programming language and a powerful software platform. It is also object-oriented, distributed, portable and multi- threaded. Java follows the ‘Write – once – run – anywhere’ approach. All Java programs must run on the Java platform that has two components, the Java Virtual Machine (JVM) and the Java Application Programming Interface (API). Java program has two types. They are:

  1. Application Program (Stand-alone application)
  2. Applets Program.
  3. Stand-alone applications are those java programs that can be developed and executed on a stand-alone local computer.(which we can execute from the command prompt). The stand-

To compile the program we must run the java compiler javac, with the name of the source file on command line as shown below C:> javac prog.java If everything is OK, java compiler(javac) creates a file called Sample.class () containing the bytecodes of the program prog.java Running the program: We need to use the java interpreter to run stand alone applications. At the command prompt, type… C:> java Sample Now, the interpreter looks for the main method in the program and begins execution from there. When executed our program displays the following o/p: Hi friend Java Tokens: A token is the smallest element of a program that is meaningful to the compiler. Tokens can be classified as follows:

**1. Keywords

  1. Identifiers
  2. Constants
  3. Special Symbols
  4. Operators** Keyword: Keywords are pre-defined or reserved words in a programming language. Each keyword is meant to perform a specific function in a program. Since keywords are referred names for a compiler, they can’t be used as variable names because by doing so, we are trying to assign a new meaning to the keyword which is not allowed. Java language supports following keywords:

abstract assert boolean break byte case catch char class const continue default do double else enum exports extends final finally float for goto if implements import instanceof int interface long module native new open opens package private protected provides public requires return short static strictfp super switch synchronized this throw throws to transient transitive try uses void volatile while with Identifiers: Identifiers are used as the general terminology for naming of variables, functions and arrays. These are user-defined names consisting of an arbitrarily long sequence of letters and digits with either a letter or the underscore(_) as a first character. Identifier names must differ in spelling and case from any keywords. You cannot use keywords as identifiers; they are reserved for special use. Once declared, you can use the identifier in later program statements to refer to the associated value. A special kind of identifier, called a statement label, can be used in goto statements. Examples of valid identifiers :  MyVariable, MYVARIABLE, myvariable  X, x1, i  myvariable, $myvariable, sum_of_array Examples of invalid identifiers :  My Variable // contains a space  123geeks // Begins with a digit  a+c // plus sign is not an alphanumeric character  variable-2 // hyphen is not an alphanumeric character  sum&_difference // ampersand is not an alphanumeric character

 Relational Operators  Logical Operators  Ternary Operator  Bitwise Operators  Shift Operators  instance of operator  Precedence and Associativity Java Virtual machine: As we know that all programming language compilers convert the source code to machine code. Same job done by Java Compiler to run a Java program, but the difference is that Java compiler convert the source code into Intermediate code is called as bytecode. This machine is called the Java Virtual machine and it exits only inside the computer memory. Following figure shows the process of compilation. The Virtual machine code is not machine specific. The machine specific code is generated. By Java interpreter by acting as an intermediary between the virtual machine and real machines shown below

Java Object Framework act as the intermediary between the user programs and the virtual machine which in turn act as the intermediary between the operating system and the Java Object Framework Command Line arguments: The java command-line argument is an argument i.e. passed at the time of running the java program. The arguments passed from the console can be received in the java program and it can be used as an input. So, it provides a convenient way to check the behavior of the program for the different values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt