[go: up one dir, main page]

0% found this document useful (0 votes)
3 views9 pages

Oop 1

This document provides an introduction to Java, covering its history, features, architecture, and components such as JVM, JRE, and JDK. It highlights Java's object-oriented nature, platform independence, and robust security features, as well as its applications in various domains. Additionally, it outlines the setup of the Java environment, the structure of a Java program, and the process of compilation and execution.

Uploaded by

bprajna64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Oop 1

This document provides an introduction to Java, covering its history, features, architecture, and components such as JVM, JRE, and JDK. It highlights Java's object-oriented nature, platform independence, and robust security features, as well as its applications in various domains. Additionally, it outlines the setup of the Java environment, the structure of a Java program, and the process of compilation and execution.

Uploaded by

bprajna64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT 1

INTRODUCTION TO JAVA
Syllabus
Brief history; features; java architecture; components: JVM, JRE, JDK; Applications; Java environment
setup; Structure of java program; Compilation and execution of java program; Clean coding in java.
Introduction
 Java is an object-oriented programming language and a platform.
 Java is a high level, robust and secure object-oriented programming language.
 Platform: Any hardware or software environment in which a program runs is known as a platform.
Since Java has its own runtime environment (JRE) and API, it is called platform.
Brief History
 Java was originally designed for interactive television, but it was too advanced technology for the
digital cable television industry at the time.
 Java team members (also known as Green Team), initiated this project to develop a language for
digital devices such as set-top boxes, televisions, etc.
 Since it was best suited for internet programming, Java technology was incorporated by Netscape
(Mozilla Firefox).
 The principles for creating Java programming were "Simple, Robust, Portable, Platform
independent, Secured, High Performance, Multithreaded, Architecture Neutral, Object Oriented,
Interpreted, and Dynamic".
 Java was developed by James Gosling, who is known as the father of Java, in 1995. James Gosling
and his team members started the project in the early '90s at Sun Micro Systems (which is now a
subsidiary of Oracle Corporation) of USA.
 Currently, Java is used in internet programming, mobile devices, games, e-business solutions,
 Significant points,
1. James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June
1991. The small team of sun engineers called Green Team.
2. Initially it was designed for small, embedded systems in electronic appliances like set-top
boxes.
3. Firstly, it was called "Green talk" by James Gosling, and the file extension was .gt.
4. After that, it was called Oak and was developed as a part of the Green project.
5. Oak is a symbol of strength and chosen as a national tree of many countries like the U.S.A.,
France, Germany, Romania, etc.
6. In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies.
7. The team chose the name Java among the suggested names “Java”, “dynamic", "revolutionary",
"Silk", "jolt", "DNA", etc.

JAVA Features
 Java has become a popular and useful programming language because of its excellent features,
which play a very important role in contributing to the popularity of this language.
 The Java features are called “Java Buzzwords”.
 Sun Micro Systems officially described Java with the following list of features,
1. Simple and Familiar
2. Compiled and Interpreted

Subject: SEPP Notes/20CS43P pg. 1


3. Platform Independent
4. Portable
5. Architectural Neutral
6. Object-Oriented
7. Robust
8. Secure
9. Distributed
10. Multi-threaded and Interactive
11. High Performance
12. Dynamic and Extensible
Simple and Familiar
 Java is simple because its coding style is very clean and easy to understand.
 Java is familiar because it has a base of familiar languages like C and C++ and contains many
features of these languages.
 It removes the drawbacks, complexities, and confusing elements of C/C++.
Compiled and Interpreted
 Usually, a computer language can be either compiled or interpreted.
 Java combines both these approaches, thus making a two-stage system.
 Java integrates the power of Compiled Languages with the flexibility of Interpreted Languages.
 Java compiler (javac) compiles the java source code into the bytecode.
 Java Virtual Machine (JVM) then executes this bytecode which is executable on many operating
systems and is portable.
Platform Independent
 Being platform-independent means a program compiled on one machine can be executed on any
machine in the world without any change.
 Java achieves platform independence by using the concept of the BYTE code.
Portable
 Portability of Java comes from architecture-neutrality.
 Java bytecode can run on any hardware that has a compliant JVM which can convert the bytecode
according to that machine.
 In Java, the size of primitive data types is machine-independent which make Java programs
portable among different platforms such as Windows, Unix, Solaris, and Mac.
 Any changes and updates made in Operating Systems, Processors and System resources will not
enforce any changes in Java programs.
Architectural Neutral
 The program written on one platform or OS is independent of other platforms or environments and
can run on any other Operating System without recompiling them.
 It is based on the ‘Write-once-run-anywhere’ (WORA) or ‘Write-once-run-everywhere’ (WORE)
approach.
 This feature is very useful when we develop applets or download applications from the Internet.
Object-Oriented
 Java strongly supports the concepts of Object-Oriented Programming due to which it is called a
pure object-oriented language.

Subject: SEPP Notes/20CS43P pg. 2


 Java supports major Object-Oriented programming features like Encapsulation, Abstraction, and
Inheritance.
 Almost everything in Java is an object.
 All programs and data live within objects and classes.
Robust
 Java is capable of handling run-time errors, supports automatic garbage collection and exception
handling, and avoids explicit pointer concept.
 Java has a strong memory management system.
 It helps in eliminating errors as it checks the code during both compile and runtime.
 Exception handling identifies and removes runtime errors.
 Any runtime error whenever encountered, the program gets terminated.
Secure
 Security is an important issue for any programming language as there is a threat of malicious
activities and viruses.
 Java systems not only verify all the memory access but also ensure that no threat of viruses
communicated through an applet.
 The absence of pointers in Java ensures that programs cannot give access to memory locations
without proper authorization.
 Java is a more secure language as compared to C/C++, as it does not allow a programmer to
explicitly create pointers.
 Programs run in a virtual machine sandbox – A separate environment that allows users to execute
their applications without affecting the underlying system.
 It has a bytecode verifier that checks the code fragments for any illegal code that violates the access
right.
Distributed
 Java is distributed because it encourages users to create distributed applications.
 In Java, a program can be split into many parts and store these parts on different computers.
 A Java programmer sitting on a machine can access another program running on the other machine.
 This feature is very helpful in developing large projects.
Multi-threaded and Interactive
 A thread is an independent path of execution within a program, executing concurrently.
 Multithreaded means handling multiple tasks simultaneously or executing multiple portions
(functions) of the same program in parallel.
 The code of java is divided into smaller parts and Java executes them in a sequential and timely
manner.
 Advantages of Multithreading,
a. Maximum utilization of resources is possible. o It doesn’t occupy memory for each thread. It
shares a common memory area.
b. There is no need to wait for the application to finish one task before beginning another one.
c. There is a decreased cost of maintenance, and it is timesaving.
d. It improves the performance of complex applications.
High Performance
 The performance of Java is impressive for an interpreted language because of its intermediate
bytecode.

Subject: SEPP Notes/20CS43P pg. 3


 Java provides high performance with the use of “JIT – Just In Time compiler”. This saves time and
makes it more efficient.
 The inclusion of multithreading enhances the overall execution speed of Java programs.
 Bytecodes generated by the Java compiler are highly optimized, so Java Virtual Machine can
execute them much faster.
Dynamic and Extensible
 With the help of OOPs, one can add classes and add new methods to classes, creating new classes
through subclasses.
 It is highly dynamic as it can adapt to its evolving environment.
 Java even supports functions written in other languages such as C and C++ to be written in Java
programs. These functions are called “native methods”.
 These methods are dynamically linked at runtime
JAVA Architecture
The art or practice of designing and building structures and especially in detail with its purpose and inter
relationship.

Fig: JAVA Architecture


 In Java, there is a process of compilation and interpretation.
 The code written in Java, is converted into byte codes which is done by the Java Compiler.
 The byte codes, then are converted into machine code by the JVM.
 The Machine code is executed directly by the machine.

Components - JVM, JRE, JDK

Subject: SEPP Notes/20CS43P pg. 4


Fig: Components of JAVA

JAVA Virtual Machine (JVM)


 JVM(Java Virtual Machine) runs Java applications as a run-time engine. JVM is the one that
calls the main method present in a Java code.
 JVM is a part of JRE(Java Runtime Environment).
 When we compile a .java file, .class files(contains byte-code) with the same class names present
in .java file are generated by the Java compiler.
 This .class file goes into various steps when we run it.
 These steps together describe the whole JVM.
 The JVM is a Java platform component that provides an environment for executing Java programs.
 JVM interprets the bytecode into machine code which is executed in the machine in which the Java
program runs.
 Functions of JVM are,
a. Loads the code
b. Verifies the code
c. Executes the code
d. Provides Runtime Environment.
Class Loader
 Class loader is a subsystem of JVM.
 It is used to load class files.
 Whenever we run the java program, class loader loads it first.
Class Method Area
 It is one of the Data Area in JVM, in which Class data will be stored.
 Static Variables, Static Blocks, Static Methods, Instance Methods are stored in this area.
Heap
A heap is created when the JVM starts up. It may increase or decrease in size while the application runs.

Java Runtime Environment (JRE)

Subject: SEPP Notes/20CS43P pg. 5


 The JRE software builds a runtime environment in which Java programs can be executed.
 The JRE is the on-disk system that takes your Java code, combines it with the needed libraries, and
starts the JVM to execute it.
 The JRE contains libraries and software needed by your Java programs to run.
 JRE is a part of JDK but can be downloaded separately.

Java Development Kit (JDK)


The JDK comes with a collection of tools that are used for developing and running java programs.
TOOLS DESCRIPTION

Java Java interpreter, which runs applets and applications by reading and interpreting
bytecode files

Javac java compiler, which translates Java source code to bytecode files that the
interpreter can understand

Javadoc Creates HTML-format documentation from java source code files

Javah Produces header files for use with native methods

Javap Java disassembler, which enables us to convert bytecode files into a program
description

Jdb Java debugger, which helps us to find errors in our programs

Applications
 Mobile Applications.
 Desktop GUI Applications.
 Web-based Applications.
 Enterprise Applications.
 Scientific Applications.
 Gaming Applications.
 Cloud Based Applications.

JAVA Environment Setup


1. Download and Install JDK 8.*
2. Setting up PATH and CLASSPATH
a. Click Windows + S, search for “Environment Variables”, and click Edit the system
environment variables. In the System Properties window, click Environment Variables.
b. Now, go to Environment Variable > System Variable section > click New.
c. In the Variable name field, enter JAVA_HOME and in the Variable value field enter the path
where the JDK is installed. Click OK to save the changes.
3. Check for Successful Installation
Open command prompt and run the command java –version or javac - version

Subject: SEPP Notes/20CS43P pg. 6


Structure of JAVA program
The following diagram represents the Structure of JAVA Program,

Fig: Structure of JAVA


Documentation Section
 It includes the comments that improve the readability of the program.
 A comment is a non-executable statement that helps to read and understand a program especially
when the programs get more complex.
 This section is optional and comments may appear anywhere in the program.
 Java programming language supports three types of comments.
a. Single line (or end-of line) comment
 It starts with a double slash symbol (//) and terminates at the end of the current line.
 The compiler ignores everything from // to the end of the line.
 Example: // Calculate sum of two numbers
b. Multiline Comment
 Java programmer can use C/C++ comment style that begins with delimiter /* and ends
with */.
 All the text written between the delimiter is ignored by the compiler.
 Example: /*calculate sum of two numbers
and it is a multiline comment */
c. Documentation comments
 This comment style is new in Java.
 Such comments begin with delimiter /** and end with */.
 Example: /** The text enclosed here will be part of program documentation */
Package Statement
 A package is a collection of classes, interfaces and sub-packages.
 A sub package contains collection of classes, interfaces and sub-sub packages etc.
 Example: package package_name;
Import statements
 Java contains many predefined classes that are stored into packages.

Subject: SEPP Notes/20CS43P pg. 7


 In order to refer these standard predefined classes in the program, user need to use fully qualified
name (i.e. Packagename.classname)
 Example: import Date class of java.util package using import statement then write
import java.util.Date;
import java.util.*;
Interface Section
It is an optional section and is used when we wish to implement multiple inheritance feature in the
program.
Class Definition
Java program may contain multiple class definition. Classes are primary feature of Java program.
Main Method Class Section
 The Class section describes the information about user-defined classes present in the program.
 A class is a collection of fields (data variables) and methods that operate on the fields.
 Every program in Java consists of at least one class, the one that contains the main method. The
main () method which is from where the execution of program actually starts and follow the
statements in the order specified.

Compilation and Execution of JAVA Program


1. Create the source file
 Open a text editor, type in the code which defines a class (HelloWorld) and then save it in a
file (HelloWorld.java).
 File and class name are case sensitive and must be matched exactly (except the .java part)
 Example Code: HelloWorld.java
/**The HelloWorld class implements an application that displays "Hello World!" to the
standard output*/
public class HelloWorld
{
public static void main(String[] args)
{
// Display "Hello World!"
System.out.println("Hello World!");
}
}
2. Compile the program
 Compile the program using the command classname.java using javac classname.java
command
 Compile HelloWorld.java by using the following command,
javac HelloWorld.java it generates a file named HelloWorld.class
 ‘javac’ is not recognized as an internal or external command, operable program orhatch file.
javac: Command not found
If any one of these errors found, then user have two choices,
a) Specify the full path in which the javac program locates every time.
For example: C:\j2sdk1.4.2_09\bin\javac HelloWorld.java
b) Set the PATH environment variable
3. Run the program

Subject: SEPP Notes/20CS43P pg. 8


 Run the code using the command java classname
 Run the code through: java HelloWorld
 Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld If this error is
found then user may need to set the environment variable CLASSPATH

Clean Coding in JAVA


 Programming is not just about writing programs that do the work for user, it is also about how clean
you write the program.
 Clean code is a reader-focused development style that produces software that's easy to write, read
and maintain.
 We need to care about Clean Code because it eventually shows the programmers capability of
writing a well maintainable piece of code that is easily understood by other developers that are
going to work with that code.
 Characteristics of Clean Code
a. Simple: The program code should be made as simple as possible.
b. Maintainable: The code should be maintainable in the long run as many different developers
can work on that program code.
c. Testable: The Program code should be easily testable and less prone to errors.
d. Readable: Program code should be easily readable.

QUESTIONS

1. List and explain the features/buzzwords of Java Programming Language.


2. Why Java is called a secure language?
3. Explain why Java is both compiled and interpreted?
4. List the characteristics of Clean Code.
5. Explain Java Architecture.
6. Write a note on a)JVM b)JRE c)JDK
7. Explain the structure of Java.
8. Explain the compilation and execution of any program.

Subject: SEPP Notes/20CS43P pg. 9

You might also like