0% found this document useful (0 votes)
5 views29 pages

Introduction of OOP in java

Java is a class-based, object-oriented programming language developed by James Gosling and released in 1995, currently owned by Oracle, with over 3 billion devices running it. It is platform-independent and used for various applications, including mobile, web, and enterprise software, featuring principles like simplicity, robustness, and security. The document also covers Java's history, version history, basic syntax, identifiers, modifiers, variables, arrays, enums, and the differences between JDK, JRE, and JVM.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
5 views29 pages

Introduction of OOP in java

Java is a class-based, object-oriented programming language developed by James Gosling and released in 1995, currently owned by Oracle, with over 3 billion devices running it. It is platform-independent and used for various applications, including mobile, web, and enterprise software, featuring principles like simplicity, robustness, and security. The document also covers Java's history, version history, basic syntax, identifiers, modifiers, variables, arrays, enums, and the differences between JDK, JRE, and JVM.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 29

Introduction to Java

Java
• Java is a class-based object oriented programming language and a platform.

• Originally developed by James Gosling at Sun Microsystems and released in 1995.


• Currently Java is owned by Oracle and more than 3 billion devices run Java.
• Java runs on a variety of platforms, such as Windows, Mac OS, LINUX/UNIX and Raspberry Pi etc.

• Java is used to develop Mobile apps, Web apps, Desktop apps, Games, Robotics, Embedded
Applications, Enterprise Applications and much more.
• Differernt Java Platforms / Editions are available : Java SE, Java EE, Java ME, and JavaFx

• Companies who are using Java include; Google, Microsoft, Facebook, IBM, Amazon, Netflix, Pinterest,
Uber, JetBrains, Many more...

• Platform: Any hardware or software environment in which a program runs, is known as a platform.
Since Java has a runtime environment (JRE) and API, it is called a platform.
The principles for creating Java
The principles for creating Java programming were
Simple,
Robust,
Portable,
Platform-independent,
Secured,
High Performance,
Multithreaded,
Architecture Neutral,
Object-Oriented,
Interpreted, and
Dynamic".
Java is −
• Object Oriented − In Java, everything is an Object. Java can be easily extended since it is based on the Object model.
• Platform Independent − Unlike many other programming languages including C and C++, when Java is compiled, it is
not compiled into platform specific machine, rather into platform independent byte code. This byte code is
distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.
• Simple − Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to
master.
• Secure − With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques
are based on public-key encryption.
• Architecture-neutral − Java compiler generates an architecture-neutral object file format, which makes the compiled
code executable on many processors, with the presence of Java runtime system.
• Portable − Being architecture-neutral and having no implementation dependent aspects of the specification makes
Java portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.
• Robust − Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error
checking and runtime checking.
• Multithreaded − With Java's multithreaded feature it is possible to write programs that can perform many tasks
simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly.
• Interpreted − Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The
development process is more rapid and analytical since the linking is an incremental and light-weight process.
• High Performance − With the use of Just-In-Time compilers, Java enables high performance.
• Distributed − Java is designed for the distributed environment of the internet.
• Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving
History of Java
1) Initially it was designed for small, embedded systems in electronic appliances like set-top boxes.
2) Firstly, it was called "Greentalk" by James Gosling
3) After that, it was called Oak and was developed as a part of the Green project.
4) Why the name Oak?
4) Oak is a symbol of strength and chosen as a national tree of many countries like the U.S.A., France,
Germany, Romania, etc.
5) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies.
Why Java Programming named "Java"?
6) The team gathered to choose a new name. The suggested words were "dynamic", "revolutionary", "Silk",
"jolt", "DNA", etc. They wanted something that reflected the essence of the technology: revolutionary,
dynamic, lively, cool, unique, and easy to spell, and fun to say.
7) According to James Gosling, "Java was one of the top choices along with Silk". Since Java was so unique,
most of the team members preferred Java than other names.
8) Java is an island in Indonesia where the first coffee was produced (called Java coffee). It is a kind of espresso
bean. Java name was chosen by James Gosling while having a cup of coffee nearby his office.
9) Notice that Java is just a name, not an acronym.
10) Initially developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle
Java Version History
• JDK Alpha and Beta (1995) • Java SE 9 (21st Sep 2017)
• JDK 1.0 (23rd Jan 1996) • Java SE 10 (20th Mar 2018)
• JDK 1.1 (19th Feb 1997) • Java SE 11 (September 2018)
• J2SE 1.2 (8th Dec 1998) • Java SE 12 (March 2019)
• J2SE 1.3 (8th May 2000) • Java SE 13 (September 2019)
• J2SE 1.4 (6th Feb 2002) • Java SE 14 (Mar 2020)
• J2SE 5.0 (30th Sep 2004) • Java SE 15 (September 2020)
• Java SE 6 (11th Dec 2006) • Java SE 16 (Mar 2021)
• Java SE 7 (28th July 2011) • Java SE 17 (September 2021)
• Java SE 8 (18th Mar 2014) • Java SE 18 (to be released by March 2022)
First Java Program
public class MyFirstJavaProgram {

/*
This is my first java program.
This will print 'Hello World' as the output
*/

public static void main(String []args) {


System.out.println("Hello World"); // prints Hello World
}
}
How to
save the file, compile, and run
the program
• Open notepad and add the code as above.
• Save the file as: MyFirstJavaProgram.java.
• The name of the java file must match the class name. When saving the file, save it using the
class name and add ".java" to the end of the filename.
• Open a command prompt window and go to the directory where you saved
the class. Assume it's D:\javapractice.
• Type 'javac MyFirstJavaProgram.java' and press enter to compile your code.
• If there are no errors in your code, the command prompt will take you to
the next line (Assumption : The path variable is set).
• Now, type ' java MyFirstJavaProgram ' to run your program.
• You will be able to see ' Hello World ' printed on the window.
Parameters used in First Java
Program
• class keyword is used to declare a class in Java.
• public keyword is an access modifier that represents visibility. It means it is visible to all.
• static is a keyword. If we declare any method as static, it is known as the static method. The
core advantage of the static method is that there is no need to create an object to invoke
the static method. The main() method is executed by the JVM, so it doesn't require creating
an object to invoke the main() method. So, it saves memory.
• void is the return type of the method. It means it doesn't return any value.
• main represents the starting point of the program.
• String[] args or String args[] is used for command line argument. We will discuss it in
coming section.
• System.out.println() is used to print statement. Here, System is a class, out is an object of
the PrintStream class, println() is a method of the PrintStream class. We will discuss the
internal working of System.out.println() statement in the coming section
Ways to write a Java Program
1) By changing the sequence of the modifiers, method prototype is not changed in
Java.
static public void main(String args[])

2)The subscript notation in the Java array can be used after type, before the variable
or after the variable.
public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
3)You can provide var-args support to the main() method by passing 3 ellipses (dots)
public static void main(String... args)
Having a semicolon at the end of class is optional in Java
class A{
static public void main(String... args){
System.out.println("hello java4");
}
};
What happens at compile time?
At compile time, the Java file is compiled by Java Compiler (It does not
interact with OS) and converts the Java code into bytecode.
What happens at
runtime?
At runtime, these steps are performed:
• Classloader: It is the subsystem of JVM that is used to
load class files.

• Bytecode Verifier: Checks the code fragments for illegal


code that can violate access rights to objects.

• Interpreter: Read bytecode stream then execute the


instructions.
Basic Syntax
• About Java programs, it is very important to keep in mind the following points.
• Java is case-sensitive: "MyClass" and "myclass" has different meaning
• Case Sensitivity − Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.
• Class Names − For all class names the first letter should be in Upper Case. If several words are used to form a name of the
class, each inner word's first letter should be in Upper Case.
• Example: class MyFirstJavaClass
• Method Names − All method names should start with a Lower Case letter. If several words are used to form the name of the
method, then each inner word's first letter should be in Upper Case.
• Example: public void myMethodName()
• Program File Name − Name of the program file should exactly match the class name.
• When saving the file, you should save it using the class name (Remember Java is case sensitive) and append '.java' to the end
of the name (if the file name and the class name do not match, your program will not compile).
• But please make a note that in case you do not have a public class present in the file then file name can be different than class
name. It is also not mandatory to have a public class in the file.
• Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java'
• public static void main(String args[]) − Java program processing starts from the main() method which is a mandatory part of
every Java program.
Java Identifiers
• Names used for classes, variables, and methods are called identifiers.
• In Java, there are several points to remember about identifiers. They are as
follows −
• All identifiers should begin with a letter (A to Z or a to z), currency character
($) or an underscore (_).
• After the first character, identifiers can have any combination of characters.
• A key word cannot be used as an identifier.
• Most importantly, identifiers are case sensitive.
• Examples of legal identifiers: age, $salary, _value, __1_value.
• Examples of illegal identifiers: 123abc, -salary.
Java Modifiers
There are two categories of modifiers −

• Access Modifiers − default, public , protected, private

• Non-access Modifiers − final, abstract, strictfp

We will be looking into more details about modifiers in the next section
Java Variables
Following are the types of variables in Java −

• Local Variables

• Class Variables (Static Variables)

• Instance Variables (Non-static Variables)


Question: Can you save a Java source file by another name than the
class name?
Yes, if the class is not public.

To compile: javac Hard.java


To execute: java Simple
Question: Can you have multiple classes in a java source file?
Answer is Yes!
Java Arrays and Enums
Java Arrays
Arrays are objects that store multiple variables of the same type. However, an array
itself is an object on the heap. We will look into how to declare, construct, and
initialize in the upcoming chapters.
Java Enums
• Enums were introduced in Java 5.0. Enums restrict a variable to have one of only a
few predefined values. The values in this enumerated list are called enums.
• With the use of enums it is possible to reduce the number of bugs in your code.
• For example, if we consider an application for a fresh juice shop, it would be
possible to restrict the glass size to small, medium, and large. This would make sure
that it would not allow anyone to order any size other than small, medium, or
large.
Java Enum Example
class FreshJuice {
enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
FreshJuiceSize size;
}
public class FreshJuiceTest {
public static void main(String args[]) {
FreshJuice juice = new FreshJuice();
juice.size = FreshJuice.FreshJuiceSize.MEDIUM ;
System.out.println("Size: " + juice.size);
}
}
Java Keywords
abstract assert boolean break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements

import instanceof int interface

long native new package


private protected public return

short static strictfp super


switch synchronized this throw

throws transient try void


volatile while
Difference between JDK, JRE,
and JVM
JDK (Java Development Kit)
• The Java Development Kit (JDK) is a software development environment which is used to develop Java
applications
• The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an
interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), etc. to
complete the development of a Java Application.

• Standard Edition Java Platform


• Enterprise Edition Java Platform
• Micro Edition Java Platform
Difference between JDK, JRE,
and JVM
JVM (Java Virtual Machine)
• JVM is an abstract machine. It is called a virtual machine because it doesn't physically exist. It is a
specification that provides a runtime environment in which Java bytecode can be executed.

• It can also run those programs which are written in other languages and compiled to Java bytecode.

• JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are platform dependent
because the configuration of each OS is different from each other. However, Java is platform independent.

• JVM performs the following main tasks: • JVM provides definitions for the:
• Loads code • Memory area
• Verifies code • Class file format
• Executes code • Register set
• Provides runtime environment • Garbage-collected heap
• Fatal error reporting etc.
Difference between JDK, JRE,
and JVM
JRE (Java Runtime Environment )
• 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 Oracle and other
companies.
• An implementation Its implementation is known as JRE (Java Runtime
Environment).
• The Java Runtime Environment is a set of software tools which are
used for developing Java applications.
• It contains a set of libraries + other files that JVM uses at runtime.
JVM Architecture
Let's understand the internal architecture of JVM. It contains classloader, memory area, execution engine etc.
Classloader
• Classloader is a subsystem of JVM which is used to load class files. Whenever
we run the java program, it is loaded first by the classloader.
• There are three built-in classloaders in Java.
• Bootstrap ClassLoader: This is the first classloader which is the super class of
Extension classloader. It loads the rt.jar file which contains all class files of Java
Standard Edition like java.lang package classes, java.net package classes,
java.util package classes, java.io package classes, java.sql package classes etc.
• Extension ClassLoader: This is the child classloader of Bootstrap and parent
classloader of System classloader. It loades the jar files located
inside $JAVA_HOME/jre/lib/ext directory.
• System/Application ClassLoader: This is the child classloader of Extension
classloader. It loads the classfiles from classpath. By default, classpath is set to
current directory. You can change the classpath using "-cp" or "-classpath"
switch. It is also known as Application classloader.
Memory Areas
Class(Method) Area: Class(Method) Area stores per-class structures such as
the runtime constant pool, field and method data, the code for methods.

Heap: It is the runtime data area in which objects are allocated.

Stack: Java Stack stores frames. It holds local variables and partial results,
and plays a part in method invocation and return. Each thread has a private
JVM stack, created at the same time as thread. A new frame is created each
time a method is invoked. A frame is destroyed when its method invocation
completes.
Memory Areas
• Program Counter Register: PC (program counter) register contains the address of
the Java virtual machine instruction currently being executed.
• Native Method Stack: It contains all the native methods used in the application.
• Execution Engine: It contains:
• A virtual processor
• Interpreter: Read bytecode stream then execute the instructions.
• Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the
byte code that have similar functionality at the same time, and hence reduces the amount
of time needed for compilation. Here, the term "compiler" refers to a translator from the
instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
• Java Native Interface: Java Native Interface (JNI) is a framework which provides
an interface to communicate with another application written in another
language like C, C++, Assembly etc. Java uses JNI framework to send output to
the Console or interact with OS libraries.

You might also like