Java

SRIVINAYAKASubject: JAVA PROGRAMMINGI.NAGESWARA RAO (Asst. Prof)Unit-1Introduction to oop: Need of OO programming, principles of OOP languages, procedure languages vs OOP, Applications of OOP, History of Java, JVM, java features, program structures, Installation of JDK 1.61.1 Need of OO programming.
  • To overcome the problems faced with structured programming the OOP programming is needed. It makes use of bottom-up approach. It also manages increasing complexity.
  • 1.2 Principles of OOP languages (or) Basic Concepts of OOP languages a) Object
  • Object is the basic run-time entities in an object oriented system.
  • They may represent a person, a place, a bank account, a table of data or any program.
  • When a program is executed, the objects interact by sending messages to one another.
  • b) Class
  • A class is a collection of objects of similar type
  • e.g. Mango, Apple & Orange are members of the class fruit
  • Once a class has been defined, we can create any number of objects belonging to that class
  • c) Data Encapsulation
  • The wrapping up of the data & functions into a single unit is known as encapsulation.
  • Data Encapsulation is the most striking feature of a class.
  • d) Data Abstraction
  • Abstraction refers to the act of representing essential features without including the background details or explanations.
  • Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost and functions to operate on these attributes.
  • e) Inheritance
  • Inheritance is the process by which objects of one class acquire the properties of objects of another class.
  • It supports the concept of hierarchical classification.
  • It provides the idea of reusability.
  • f) Polymorphism
  • Polymorphism means the ability to take more than one form.
  • In polymorphism an operation may exhibit different behaviours in different instances. The behavior depends upon the types of data used in the operation.
  • e.g. Consider the operation of addition for two numbers ,the operation will generate a sum. If the operation of addition for two strings,then the operation would produce a third string by concatenation. g) 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 known until the time of call at run-time. It associated with polymorphism and inheritance.
  • h) Message Passing
  • An object –oriented program consists of a set of objects that communicate with each other.
  • The process of programming in an object-oriented language involves the following steps
  • Creating classes that define objects and their behavior.
  • Creating objects from class definitions.
  • Establishing communication among objects.
  • Objects communicate with one another by sending and receiving information.
  • 1.3 Procedure-Oriented programming (pop)(i) Conventional programming using high level languages such as COBOL, FORTRAN and C is commonly known as procedure-oriented programming.(ii) In the procedure oriented approach the problem is viewed as a sequence of things to be done such as reading, calculating and printing.(iii) POP basically consists of writing of list of instructions for the computer to follow, and organizing these instructions into groups known as functions.Pop features (or) Characteristics
  • Emphasis is on procedure rather than data
  • Larger programs are divided into smaller programs known as functions
  • Most of the functions share global data
  • Data move openly around the system from function to function
  • Employs top-down approach in program design
  • Drawbacks of POP
  • The serious drawback with the procedural approach is that it does not model real-world problems very well.
  • It is difficult to create new data types.
  • Note-The ability to create the new data type of its own is called extensibility. POP languages are not extensible.1.4 Object-Oriented programming (i)Object-oriented programming (OOP) is a programming language model organized around objects rather than "actions" and data rather than logic.(ii) The major motivating factor in the invention of object-oriented approach is to remove some of the flaws encountered in the procedural approach(iii) OOP treats data as a critical element in the program development and does not allow it to flow freely around the system.oop features (or) Characteristics
  • Emphasis is on data rather than procedure
  • Programs are divided into small units are known as objects
  • Data is hidden and cannot accessed by external functions
  • Objects may communicate with each other through functions
  • Follows bottom-up approach in program design
  • 1.4.1 Benefits of OOP (or) Advantages of OOPOOP offers several benefits to both the program designer and the user.
  • Through inheritance, we can eliminate redundant code and extend the use of existing classes.
  • The principle of data hiding helps the programmer to build secure programs.
  • It is easy to partition the work in a project based on objects.
  • Software complexity can be easily managed.
  • Object-oriented systems can be easily upgraded from small to larger systems.
  • 1.4.2 Disadvantages of OOPSome of the disadvantages of object-oriented programming include:Steep learning curve: The thought process involved in object-oriented programming may not be natural for some people, and it can take time to get used to it. It is complex to create programs based on interaction of objects. Some of the key programming techniques, such as inheritance and polymorphism, can be challenging to comprehend initially.Larger program size: Object-oriented programs typically involve more lines of code than procedural programsSlower programs: Object-oriented programs are typically slower than procedure-based programs, as they typically require more instructions to be executed.Not suitable for all types of problems: There are problems that lend themselves well to functional-programming style, logic-programming style, or procedure-based programming style, and applying object-oriented programming in those situations will not result in efficient programs. 1.5 Difference between Procedure Oriented Programming (POP) & Object Oriented Programming (OOP)1.6 Applications of OOPApplications of OOP are beginning to gain importance in many areas.
  • Real-time systems
  • Simulation and modeling
  • Object-oriented databases
  • Hypertext, hypermedia and expertext
  • AI and expert systems
  • Neural networks and parallel programming
  • Decision support and office automation systems.
  • 1.7 History of Java1.7.1 Java’s Lineage
  • Java is related to C++,which is a direct descendant of C
  • From C, java derives its syntax. Many of java’s object oriented features were influenced by C++.
  • 1.7.2 The Birth of Modern Programming: C
  • It was invented by Dennis Ritchie.
  • It was a powerful, efficient, structured language that was relatively easy to learn.
  • It was designed, implemented and developed by real working programmers.
  • During the late 1970s and early 1980,C became the dominant computer programming language & it is widely used today.
  • Problem with c language
  • The C language can not manage its complexity if lines of code exceeds 25,000
  • The trouble with C is that it is designed for specific target.
  • The C language is platform dependent language
  • 1.7.3 C++
  • To manage the complexity of programming, object oriented programming was invented.
  • The OOP language C++ was invented by Bjarne Stroustrup.
  • Problem with C++ language
  • The trouble with C++ is that it is designed for specific target.
  • The C++ language is platform dependent language.
  • 1.7.4 Java
  • Java was invented by James Gosling,Patrick Naughton,Chris Warth,Ed Frank and Mike Sheridan at sun microsystems.
  • It is a platform independent language
  • Java was designed for the development of software for consumer electronic devices like TVs,VCRs.
  • 1.7.5 Java VersionsThere are many java versions that has been released. Current stable release of Java is Java SE 8.
  • JDK Alpha and Beta (1995)
  • JDK 1.0 (23rd Jan, 1996)
  • JDK 1.1 (19th Feb, 1997)
  • J2SE 1.2 (8th Dec, 1998)
  • J2SE 1.3 (8th May, 2000)
  • J2SE 1.4 (6th Feb, 2002)
  • J2SE 5.0 (30th Sep, 2004)
  • Java SE 6 (11th Dec, 2006)
  • Java SE 7 (28th July, 2011)
  • Java SE 8 (18th March, 2014)
  • 1.8 JVMi) JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed. ii)JVMs are available for many hardware and software platforms ii) It is:
  • A specification where working of Java Virtual Machine is specified. But implementation provider is independent to choose the algorithm. Its implementation has been provided by Sun and other companies.
  • An implementation Its implementation is known as JRE (Java Runtime Environment).
  • Runtime Instance Whenever you write java command on the command prompt to run the java class, and instance of JVM is created.
  • iv) The JVM performs following operation:
  • Loads code
  • Verifies code
  • Executes code
  • Provides runtime environment
  • v) JVM provides definitions for the:
  • Memory area
  • Class file format
  • Register set
  • Garbage-collected heap
  • Fatal error reporting etc.
  • 1.8.1 Internal Architecture of JVMa) Classloader:Classloader is a subsystem of JVM that is used to load class files.b) Class(Method) Area:Class(Method) Area stores per-class structures such as the runtime constant pool, field and method data, the code for methods.c) Heap:It is the runtime data area in which objects are allocated.d) Stack:e) Program Counter Register:PC (program counter) register. It contains the address of the Java virtual machine instruction currently being executed.f) Native Method Stack:It contains all the native methods used in the application.g) Execution Engine:1.9. The Java Buzzwords (or) java featuresa) Simple
  • Java was deigned to be easy for the professional programmer to learn and use effectively.
  • If any one understands the basic concepts of “C language” and object oriented concepts of “C++ language” then java is simple.
  • The difficult concepts of c and c++ have been omitted in Java.
  • b) Secure
  • Security becomes an important issue for a language that is used for programming on internet. Threats of viruses and use of resources are every where.
  • 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 cannot gain access to memory locations with out proper authorization.
  • c) Portable
  • Java programs can be easily moved from one computer system to another, anywhere and anytime.
  • Changes and upgrades in operating system resources will not force any changes in Java programs.
  • d) Object-Oriented
  • Java is a true object-oriented language. Almost everything in Java is an object.
  • All program code and data reside within objects and classes.
  • The object model in java is simple and easy to extend.
  • e) Robust
  • Java is a robust language. It provides many safeguards to ensure reliable code. Java checks the code at compile time and checks the code at run time.
  • Java incorporates the concept of exception handling which captures series of errors and eliminates any risk of crashing the system.
  • d) Multithreaded
  • Java supports multithreaded programming, which allows writing programs that do many things simultaneously.
  • e) Architectural-Neutral
  • The java designers made several hard decisions in the Java language and the Java virtual machine and their goal was “write once; run anywhere, any time, forever”. To great extent, this goal was accomplished.
  • f) Interpreted
  • Java enables the creation of cross-platform programs by compiling into an intermediate representation called Java bytecode.This code can be interpreted on any system that provides a Java Virtual Machine.
  • g) High performance
  • The Java byte code was carefully designed so that it would be easy to translate directly into a native machine code for very high performance by using a just-in-time compiler.
  • h) Distributed
  • Java is designed for the distributed environment of the Internet, because it handles TCP/IP protocols.
  • i) Dynamic
  • Java programs carry with them multiple amounts of run-time type information that is used to verify and resolve access to objects at run time. This makes it possible to dynamically link code in a safe and expedient manner.
  • 1.10 Java Program Structure Fig: General structure of java programa)Documentation SectionIt contains the comment lines giving the name of the program, the author, and other details.The styles of commentsi)Single line comment
  • It starts with // works until end of the line
  • e.g //This is a java program //Implemented by Goslingii) Multiline comment
  • It starts with /* and ends with */
  • e.g /*This is a java program Implemented by Gosling*/ iii) Documentation Comment
  • It is also called as doc comment. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.
  • It has the format /** …. */
  • e.g. /**This is java program implemented by Gosling*/This section is optional for java programb) Package Statement
  • The first statement allowed in java file is package statement. This statement declares a package name and informs the compiler that the classes defined here belonging to this package.
  • e.g package student;This section is optional for java programc) Import Statements
  • The import statement is similar to #include statement in C.
  • e.g import student.testThis statement instructs the interpeter to load the test class contained in the package student.This section is optional for java programd) Interface Statements
  • An interface is like a class but includes a group of method declarations.
  • It used mainly to implement multiple inheritance
  • This section is optional for java programe) Class Definitions
  • A java program may contain multiple class definitions.
  • Classes are primary and essential elements of a java program.
  • These classes are used to map the objets of real-world problems,
  • The number of classes used depends on the complexity of the problem.
  • This section is optional for java programf) Main Method Class
  • Every java program requires a main method as its starting point.
  • The main method creates objects of various classes and establishes communications between them.
  • This section is optional compulsary java program essential1.11 JDKTo Install the JDK Software and Set JAVA_HOME on a Windows System
  • Install the JDK software.
  • Go to .
  • Select the appropriate JDK software and click Download.
  • The JDK software is installed on your computer, for example, at C:\Program Files\Java\jdk1.6.0_02. You can move the JDK software to another location if desired.
  • To set JAVA_HOME:
  • Right click My Computer and select Properties.
  • On the Advanced tab, select Environment Variables, and then edit JAVA_HOME to point to where the JDK software is located, for example, C:\Program Files\Java\jdk1.6.0_02.
  • Installing OpenJDK on Red Hat Enterprise Linux 5
  • Subscribe to the base channel
  • The OpenJDK is available in the RHN base channel. By default, all installations of Red Hat Enterprise Linux are subscribed to this channel.
  • Install the Package
  • Use the yum utility to install OpenJDK: [localhost]# yum install java-1.6.0-openjdk-devel
  • Verify System Default JDK
  • To ensure that the correct JDK is set as the system default, run the alternatives commandTo Install the JDK Software and Set JAVA_HOME on a UNIX System
  • Install the JDK software.
  • Go to .
  • Select the appropriate JDK software version and click Download.
  • The JDK software is installed on your computer, for example, /usr/jdk/jdk1.6.0_02. You can change this location.
  • Set JAVA_HOME.
  • Korn and bash shells:
  • export JAVA_HOME=jdk-install-dir
  • export PATH=$JAVA_HOME/bin:$PATH
  • Bourne shell:
  • JAVA_HOME=jdk-install-dir
  • export JAVA_HOME
  • PATH=$JAVA_HOME/bin:$PATH
  • export PATH
  • C shell:
  • setenv JAVA_HOME jdk-install-dir
  • setenv PATH $JAVA_HOME/bin:$PATH
  • export PATH=$JAVA_HOME/bin:$PATH
  • Change the permissions to enable you to run the Java CAPS Installer.
  • chmod 755 JavaCAPS.bin
  • Additional TopicsA.1 ‘Write once and Run Anywhere’The java compiler compiles a java program and converts it into byte code i.e a highly optimized set of instructions designed to be executed by the JVM.Because of byte code, a java program can run on wide variety of platforms. JVM is platform dependent i.e its implementation differs from platform to platform, but these all JVMs can execute the same java byte code. Hence, it is java byte code due to which java code is written once but can be run on any platform.A.2 The fundamental forces that necessitated the invention of java along with the key factors involved in molding the java language.Although the fundamental forces that necessitated the invention of java are portability and security, other factors also played an important role in molding the final form of the language, the key considerations were summed up by the java team in the following list of buzzwords:"A.3 Java is a platform independentA programming language or technology is said to be platform independent if and only if which can run on all available operating systems with respect to its development and compilation.Java is a platform independent programming language, Because when we install jdk software on our system then automatically jvm is install on our system. For every operating system separate jvm is available which is capable to read .class file or byte code. When we compile our java code then .class file is generated by javac compiler these code are readable by jvm and every operating system have its own jvm so jvm is platform dependent but due to jvm java language is become platform independent.JAVA IMPORTANT QUESTIONS1.a) What is object oriented programming? How it is different from procedural conceptsb) Explain briefly the object oriented concepts (or) object oriented principles2.a) Explain java program structure b) ‘Write once and Run Anywhere’- support this statement with proper reasoning3.a) Discuss the advantages and disadvantages of oopb) Write short notes on the features of oop (or) buzz words of oop.4. a) Explain the fundamental forces that necessitated the invention of java along with the key factors involved in moulding the java language.b) Explain JVM with architecture5a) Explain JDK with tools b) Explain installation of JDK1.6