This document provides an overview of the history and features of the Java programming language. It discusses the origins of Java from C and C++ and how it was created to be a platform-independent language through the use of bytecode and the Java Virtual Machine (JVM). Key aspects of Java covered include its design goals of portability and security, object-oriented features, and relationships between the JDK, JRE, and JVM. Abstract classes, interfaces, and marker interfaces are also summarized.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
126 views
Java For Placement Training 2020-2021
This document provides an overview of the history and features of the Java programming language. It discusses the origins of Java from C and C++ and how it was created to be a platform-independent language through the use of bytecode and the Java Virtual Machine (JVM). Key aspects of Java covered include its design goals of portability and security, object-oriented features, and relationships between the JDK, JRE, and JVM. Abstract classes, interfaces, and marker interfaces are also summarized.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 128
JAVA
History of programming languages
• FORTRAN - used to write fairly efficient programs for scientific applications, it was not very good for system code. • BASIC - easy to learn, it wasn’t very powerful, and its lack of structure made its usefulness questionable for large programs. • Assembly language- used to produce highly efficient programs, but it is not easy to learn or use effectively. C language • BCPL(developed by Martin Richards) B( invented by Ken Thompson) C . • C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T. What is Object-Oriented Programming? • OOP is a programming methodology that helps organize complex programs through the use of Abstraction, inheritance, encapsulation, and polymorphism. C++ • C++ was invented by Bjarne Stroustrup in 1979. • C++ extends C by adding object-oriented features. TOP DOWN APPROACH BOTTOM UP APPROACH
In this approach We focus on breaking up the In bottom up approach, we solve smaller
problem into smaller parts. problems and integrate it as whole and complete the solution.
Mainly used by structured programming Mainly used by object oriented programming
language such as COBOL, Fortan, C etc. language such as java, C++, C#, Python.
Each part is programmed separately therefore Redundancy is minimized by using data
contain redundancy. encapsulation and data hiding.
In this the communications is less among In this module must have communication. modules.
In top down approach, decomposition takes In bottom up approach composition takes
In procedural programming, program is In object oriented programming, program is
divided into small parts called functions. divided into small parts called objects.
Procedural programming follows top down Object oriented programming
approach. follows bottom up approach.
There is no access specifier in procedural Object oriented programming have access
programming. 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 Object oriented programming provides data hiding so it is more secure. secure. In procedural programming, overloading is Overloading is possible in object oriented not possible. programming. In procedural programming, function is more In object oriented programming, data is important than data. more important than function. Examples: C, FORTRAN, Pascal, Basic etc. Examples : C++, Java, Python, C# etc. JAVA • Java was conceived by • James Gosling, • Patrick Naughton, • Mike Sheridan at Sun Microsystems, Inc. in 1991. • This language was initially called “Oak,” but was renamed “Java” in 1995. • Java derives much of its syntaxes from C and C++. • According to Oracle, the company that owns Java, Java runs on 3 billion devices worldwide, which makes Java one of the most popular programming languages Design goals of java • Java was originally meant to be a platform neutral language for embedded software in devices. • The main design goal of java is to move the program away from platform and OS- specific compilers. • The language could be used to produce platform neutral code. Design goals of java • Java is the name for both the programming language that can be used for building complex web applications and for the software platform that used this programming language as its most essential component. • It is widely used by development companies to build secure, robust and scalable web applications. • Java offers support for the web application through JSPs and Servlets. Java’s Magic: The Bytecode • The output of a Java compiler is not executable code. Rather, it is Bytecode. • Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM). • Translating a Java program into bytecode makes it much easier to run a program in a wide variety of environments because only the JVM needs to be implemented for each platform. • The execution of bytecode by the JVM is the easiest way to create truly portable programs. The JIT compiler
• Java was designed as an interpreted language.
• The Just-In-Time (JIT) compiler is a key component of the JVM that improves the performance of Java applications by compiling platform-neutral Java bytecode into native machine code at run time. • A JIT compiler compiles code as it is needed, during execution. JDK vs. JRE vs. JVM • JDK is a software development kit whereas JRE is a software bundle that allows Java program to run, whereas JVM is an environment for executing bytecode. • The full form of JDK is Java Development Kit, while the full form of JRE is Java Runtime Environment, while the full form of JVM is Java Virtual Machine. • JDK is platform dependent, JRE is also platform dependent, but JVM is platform independent. • JDK contains tools for developing, debugging, etc. JRE contains class libraries and other supporting files, whereas software development tools are not included in JVM. • JDK comes with the installer, on the other hand, JRE only contains the environment to execute source code whereas JVM bundled in both software JDK and JRE. Default value • For primitive types like int, long, float the default value are zero (0 or 0.0). • For reference types (anything that holds an object in it) will have null as the default value. • For boolean variable it will be false. Usage 1: If parent class and child class have same fields then we can use super keyword to access the data member or field of parent class. Usage 2: If subclass contains the same method as parent class we can use super keyword to invoke parent class method. Usage 3 :Use of super with constructors • super keyword can also be used to access the parent class constructor. • One more important thing is that, ‘’super’’ can call both parametric as well as non parametric constructors depending upon the situation. • Call to super() must be first statement in Derived(Student) Class constructor. • If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. • If the superclass does not have a no-argument constructor, you will get a compile-time error. Runtime Polymorphism(Dynamic Method Dispatch ) • It is a process in which a call to an overridden method is resolved at runtime rather than compile-time. • In this process, an overridden method is called through the reference variable of a superclass. • The determination of the method to be called is based on the object being referred to by the reference variable. • Let's first understand the upcasting before Runtime plymorphism. output • Free memory in JVM before Garbage Collection= 127611672 • Free memory in JVM after Garbage Collection= 128011264 Abstract class in Java
• Data abstraction is the process of hiding certain details and showing
only essential information to the user. • Abstraction can be achieved with either abstract classes or interfaces. • A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body). Abstract class in Java
• The abstract keyword is used for classes and methods:
• Abstract class: is restricted class that cannot be used to create objects (to access it, it must be inherited from another class). • Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from). Interfaces in Java • Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body). • It is used to achieve abstraction. • Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance . • Interfaces are used to implement abstraction. So the question arises why use interfaces when we have abstract classes? • The reason is, abstract classes may contain non-final variables, whereas variables in interface are final, public and static. Marker interface in Java • An empty interface in Java is known as a marker interface i.e. it does not contain any methods or fields by implementing these interfaces. • java.lang.Cloneable , java.io.Serializable and java.rmi.Remote are examples of marker interfaces. • Marker interface is used as a tag to inform a message to the Java compiler so that it can add special behavior to the class implementing it. java.lang.Cloneable • Cloneable interface : Cloneable interface is present in java.lang package. There is a method clone() in Object class. A class that implements the Cloneable interface indicates that it is legal for clone() method to make a field-for-field copy of instances of that class. classes that implement this interface should override Object.clone() method. java.io.Serializable • Serializable interface : Serializable interface is present in java.io package. It is used to make an object eligible for saving its state into a file. This is called Serialization. Classes that do not implement this interface will not have any of their state serialized or deserialized. java.rmi.Remote • Remote interface : Remote interface is present in java.rmi package. A remote object is an object which is stored at one machine and accessed from another machine. So, to make an object a remote object, we need to flag it with Remote interface. Here, Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. Types of Exceptions Wrapper classes • Java is an object-oriented programming language, so we need to deal with objects many times while working with Collections, Serialization, Synchronization, etc • Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects. Multithreading Multithreading • Multithreading in Java is a process of executing multiple threads simultaneously. • A thread is a lightweight sub-process, the smallest unit of processing. • Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. • Multiprocessing and multithreading, both are used to achieve multitasking. Advantages of Java Multithreading 1. It doesn't block the user because threads are independent and you can perform multiple operations at the same time. 2. You can perform many operations together, so it saves time. 3. Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread. Threads Creation • Threads can be created by using two mechanisms : 1. Extending the Thread class 2. Implementing the Runnable Interface Thread creation by extending the Thread class 1. Java provides Thread class to achieve thread programming. 2. Thread class is available in the package java.lang . 3. This class overrides the run() method available in the Thread class. 4. A thread begins its life inside run() method. 5. We create an object of our new class and call start() method to start the execution of a thread. 6. Start() invokes the run() method on the Thread object. The Main thread
• Java provides built-in support for multithreaded programming.
• When a Java program starts up, one thread begins running immediately. This is usually called the main thread of our program, because it is the one that is executed when our program begins. The Main thread • The main thread is created automatically when our program is started. • The Main thread in Java is the one that begins executing when the program starts. • All the child threads are spawned from the Main thread. • Also, it is the last thread to finish execution as various shut-down actions are performed by it. • The default priority of Main thread is 5 and for all remaining user threads priority will be inherited from parent to child. currentThread()
• Information about the main or any thread can be accessed by
obtaining a reference to the thread using a public, static method in the Thread class called currentThread(). Collections in Java • The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects. • Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion. • Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet). Collections in Java
• Prior to Java 2, Java provided ad hoc classes such as Dictionary,
Vector, Stack, and Properties to store and manipulate groups of objects. Although these classes were quite useful, they lacked a central, unifying theme. • All of these collections had no common interface. • Therefore, though the main aim of all the collections are same, the implementation of all these collections were defined independently and had no correlation among them. And also, its very difficult for the users to remember all the different methods, syntax and constructors present in every collection class. Collections in Java A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following • Interfaces − These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. • Implementations, i.e., Classes − These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures. • Algorithms − These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. Advantages of the Collection Framework • Consistent API: The API has a basic set of interfaces like Collection, Set, List, or Map, all the classes (ArrayList, LinkedList, Vector, etc) that implement these interfaces have some common set of methods.
• Reduces programming effort: A programmer doesn’t have to worry about the
design of the Collection but rather he can focus on its best use in his program. Therefore, the basic concept of Object-oriented programming (i.e.) abstraction has been successfully implemented.
• Increases program speed and quality: Increases performance by providing
high-performance implementations of useful data structures and algorithms Hierarchy of Collection Framework
• The java.util package contains all the classes and interfaces for the