-
Notifications
You must be signed in to change notification settings - Fork 3
Chapter 1 : Introduction to Java
Saket Khopkar edited this page Dec 30, 2021
·
12 revisions
- Objects:
- Objects are the basic run time entities in object oriented system.
- Objects are the variables of the type class.
- They may represent a place, a person, a bank account, a table of data, or any item that the program must handle. They may represent user defined data such as time, limit.
- Each object contains data and code (function) to manipulate the data.
- Example: Customer and Account are two objects in a program, then Customer object can send a message to the Account object requesting for the bank balance.
- Classes:
- Class is a collection of objects of similar types.
- Class is a user defined data type.
- Once a class has been defined, we can create any number of objects of that class.
- Class contains variables (data members) and functions (methods or member functions).
- Example: fruit mango, apple, banana; Here, fruit is a class and mango, apple, banana are objects of the class fruit.
- Data Abstraction:
- Abstraction refers to the act of representing essential features without including the background details or explanations.
- Classes are defined as a list of attributes (variables) and functions to operate on these attributes.
- The attributes are called as data members and functions are called as methods or member functions.
- Classes use the concept of data abstraction hence they are known as Abstract Data Types (ADT).
- Encapsulation:
- The wrapping up of data and functions into a single unit (called class) is known as encapsulation.
- Data encapsulation is the most striking feature of the class.
- The data is not accessible to outside world, and only those functions which are wrapped in the class can access it.
- This insulation of the data from direct access by the program is called as data hiding or information hiding.
- Inheritance:
- The mechanism of deriving new class from old one is called as inheritance.
- Old class is called as base or super class and new class is called as derived or sub class.
- Inheritance provides the idea of reusability. In inheritance instead of creating the same which already exists, we can reuse it.
- Reusability increase reliability and saves time as well as money.
- A derived class inherits some or all properties of base class.
- Polymorphism:
- Polymorphism means the ability to take more than one form.
Operator Overloading:
- The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.
- Example: 10 + 15 =25 Here, + add two numbers, because operands are numbers. “New”+”York”=”NewYork” Here, + concatenates (join) two strings, because operands are strings. So, here + operator is overloaded.
Function Overloading:
- Using single function name to perform different types of task is called as function overloading.
- Example :- Shape -> Draw() . Here Shape is the base class and Draw is its method.
- Circle -> Draw(), Box -> Draw(), Triangle -> Draw() These are the method that are inherited from the Shape base class.
- Here, Draw() method does different function in different classes, yet its name is same.
- Dynamic Binding:
- Binding refers to the linking of a procedure call (function call) to the code (function body) 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 call at run time.
- Dynamic binding is associated with polymorphism and inheritance.
- In above diagram, by inheritance, every object will have this ‘draw’ procedure. Its algorithm is unique to each object and so the draw procedure will be redefined in each class that defines the object.
- At run time, the code matching the object under current reference will be called.
- Message Passing:
- Object Oriented Program consists of a set of objects that communicate with each other.
- The process of programming in an object oriented language therefore involves the following basic steps:
- Creating classes that defines objects and their behavior.
- Creating objects from class definitions.
- Establishing communication among objects.
- Objects communicate with one another by sending and receiving information.
- A message for an object is a request for execution of a procedure, and therefore will invoke a function (procedure) in receiving objects that generates the desired results.
- Message passing involves specifying the name of the object, the name of the function (message) and the information to be sent.
- Objects have a life cycle. They can be created and destroyed. Communication with an object is feasible as long as it is alive.
- Inheritance eliminates redundant (repeated) code. We can derive new class from existing class. It gives reusability, hence saves development time.
- Due to data hiding programs becomes more secure.
- It is possible to have multiple instances of an object to co-exist without any interference.
- It models real-world problems very well.
- It is easy to partition the work in a project based on objects.
- Object oriented systems can be easily upgraded from small to large systems.
- Software complexity can be easily managed.
- Message passing technique for communication between objects makes the interface description with external systems much simpler.
- The data centered design approach is used.
- Real-Time Systems
- Simulation and Modeling
- Object-Oriented Databases
- Hypertext, Hypermedia and Expertext
- Artificial Intelligence and Expert Systems
- Neural Networks and Parallel Programming
- Decision Support and Office Automation Systems
- CIM / CAM / CAD Systems
- Compiled and Interpreted:
- Usually computer language is compiled or interpreted. But Java combines both these approaches thus making Java a two stage system.
- First, Java compiler translates source code into bytecode. Bytecode is intermediate code and it is not machine code.
- In Second stage, Java interpreter generates machine code that can be directly executed by the machine that is running the Java program.
- We can thus say that Java is compiled and interpreted language.
- Platform-Independent and Portable:
- Portability is most significant feature of Java over other languages.
- Java programs can be easily moved from one computer system to another, anywhere and anytime.
- Changes and upgrades in operating systems, processors and system resources will not force any changes in Java programs.
- Due to above reasons Java is popular language for programming on Internet which interconnects different types of systems worldwide.
- We can download a Java applet from a remote computer onto our local system via Internet and execute it locally. This makes the Internet an extension of the user’s basic system providing practically unlimited number of accessible applets and applications.
- Java ensures portability in two ways. First, Java compiler generates bytecode that can be implemented on any machine. Secondly, the sizes of the primitive data types are machine-independent.
- 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.
- 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.
- Robust and Secure:
- Java is a robust language.
- It provides many safeguards to ensure reliable code.
- It has strict compile time and run time checking for data types.
- It is garbage-collected language, which relieves the programmer from memory management problems.
- It incorporates exception handling, which captures run time errors and eliminates any risk of crashing the system.
- Security is very important for a language that is used for programming on Internet.
- Java verifies all memory access and ensures that no viruses are communicated with an applet.
- The absence of pointers in Java ensures that programs cannot gain access to memory locations without proper authorization.
- Distributed:
- Java is designed as a distributed language for creating applications on networks. It has ability to share both data and programs.
- Java applications can open and access remote objects on Internet as easily as they can do in a local system.
- This enables multiple programmers at multiple remote locations to collaborate and work together on a single project.
- Simple, Small and Familiar:
- Java is a small and simple language. Many features of C and C++ that are either redundant or sources of unreliable code are not part of Java. For example, java does not use pointers, preprocessor header files, goto statement and many others.
- Java also eliminates operator overloading and multiple inheritance.
- To make the language look familiar to the existing programmers, it was modelled on C and C++ and therefore, Java code “looks like a C++ code. In fact, Java is a simplified version of C++.
- 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.
- For example we can listen to an audio clip while scrolling a page and at the same time download an applet from distant computer. This feature greatly improves the interactive performance of graphical application.
- The Java runtime comes with tools that support multiprocessor synchronization and construct smoothly running interactive system.
- High Performance:
- Java performance is impressive though it is an interpreted language. This is mainly due to bytecode.
- According to Sun, Java speed is comparable to the native C / C++. Java architecture is also designed to reduce overheads during runtime.
- Incorporation of multithreading enhances the overall execution speed of Java programs.
- Dynamic and Extensible:
- Java is dynamic language.
- Java is capable of dynamically linking in new class libraries, methods and objects.
- Java can also determine the type of class through a query, making it possible to either dynamically link or abort the program, depending on the response.
- Java programs support functions written in other languages such as C and C++. These functions are known as native methods. This facility enables the programmers to use the efficient functions available in these languages. Native methods are linked dynamically at runtime.
Applet:
- Java is strongly associated with Internet. Java is used to create applet or we can download the remote applet and run them on local machine using java enabled web browser such as HotJava.
- Applets have made the Internet a true extension of the storage system of the local computer.
- Internet users can setup their websites containing Java applets that could be used by other remote users of Internet. This ability of Java to hitch a ride on Information Superhighway has made Java a unique programming language for Internet. Hench Java is popularly known as Internet Language.
Security:
- Security is very important for a language that is used for programming on Internet.
- Java verifies all memory access and ensures that no viruses are communicated with an Applet.
- The absence of pointers in Java ensures that programs cannot gain access to memory locations without proper authorization.
Portability:
- Java ensures portability in two ways. First, Java compiler generates bytecode that can be implemented on any machine. Secondly, the sizes of the primitive data types are machine-independent.
As the java support applet, portability and security features which are the very important features related to internet hence Java is popular on Internet.
- The output produced by the Java compiler is not executable code, it is intermediate code called as byte code.
- Byte code is highly optimized set of instructions executed by Java run time system (JVM).
- Byte code is machine independent.
- Java Compiler produces an intermediate code 'bytecode' for machine that does not exist. This machine is known as Java Virtual Machine and it only exists inside computer memory.
- It is a simulated computer inside the computer and does all major functions of real computer.
- The virtual machine code is not machine specific. The Machine-Specific code (known as Machine Code) is generated by Java Interpreter by acting as intermediary between virtual machine and real machine.
- Java Environment includes large number of development tools and hundreds of classes and methods.
- The development tools are the part of Java Development Kit (JDK) and classes and methods are the part of Java Standard Library (JSL), also known as Application Programming Interface (API).
- The JDK comes with collection of tools that are used for developing and running Java Programs. They include:
- appletviewer (for viewing applets) :- Enables us to run applets (without actually using Java compatible browser).
- javac (java compiler) :- Translates Java source code to byte code files that interpreter can understand.
- java (java interpreter) :- Runs applets and applications by reading and interpreting bytecode files.
- javap (java disassembler) :- Enables us to convert bytecode files into program description.
- javah (java for C header files) :- Produces header files for use with native methods.
- javadoc (for creating HTML Document) :- Creates HTML format documentation from Java source code files.
- jdb (java debugger) :- Helps us to find errors in our programs.
- The Java Standard Library (JSL or API) includes hundreds of classes and methods grouped into several functional packages. Most commonly used packages are:
- Language Support Package :- A collection of classes and methods required for implementing basic features of Java.
- Utilities Package :- A collection of classes to provide utility functions such as Date and Time.
- Input / Output Package :- A collection of classes required for input and output manipulation.
- Networking Package :- A collection of classes for communicating with other computers via Internet.
- AWT Package :- The Abstract Window Toolkit package contains classes that implements platform independent Graphical User Interface.
- Applet Package :- This includes set of classes that allow us to create Java Applets.
- File name :- SampleOne.java
class SampleOne { public static void main(String args[]) { System.out.println("Hello World"); } }