Java Programming
Ms Simmi S
Assistant Professor
Department of Computer Science UG
Kristu Jayanti(Deemed To Be University
What is JAVA ????
• Java is a widely used object-oriented programming language and
software platform that runs on billions of devices, including notebook
computers, mobile devices, gaming consoles, medical devices and
many others.
• The rules and syntax of Java are based on the C and C++ languages.
• With it, developers can “write once, run anywhere” (WORA),
meaning that the compiled Java code will run on all Java-compatible
platforms without the need for recompilation.
• Java is a high-level, language-specific programming language
developed by James Gosling and Green Team the early 1990s .
• The members of Green Team: James Gosling, Patrick
Naughton, Chris Warth, ED Frank and Mike Sheridan.
• Firstly, it was called Greentalk by James Gosling and file extension
was .gt.
• After that, it was called Oak and was developed as a part of the Green
project.
History Of
History Of
• In 1995, Oak was renamed as Java because it was already a trademark by
Oak Technologies.
• In 2006 Sun started to make Java available under the GNU operating
system General Public License (GPL).
• Oracle continues this project called Open JDK.
Java Version History.
• The Java language has undergone several changes since JDK 1.0 as well as
numerous additions of classes and packages to the standard library.
• Versions 1.0 and 1.1 are named as JDK (Java Development Kit).
• From versions 1.2 to 1.4, the platform is named as J2SE (Java 2 Standard Edition).
• From versions 1.5, Sun introduces internal and external versions. From Java 6, the
version name is Java SE X.
• Major versions were released after every 2 years, however the Java SE 7 took 5
years to be available after its predecessor Java SE 6, and 3 years for Java SE 8 to
be available to public afterward.
• Since Java SE 10, new versions will be released very six months.
Editions In JAVA
Programmers can learn any of these editions based on the application they want
to make.
• Java Standard Edition[J2SE] - Contains core libraries, like java.lang, java.util,
etc. To develop client-side standalone applications or applets
• Java Enterprise Edition [J2EE]- Includes Java APIs, like JMS, EJB,
JSPs/servlets, etc. To develop server-side applications such as Java servlets and
Java Server Pages.
• Java Micro Edition [J2ME]- This edition is used to program Java in cell
phones, set-top boxes, handhelds, and so on.
• The most widely used edition in Java is Java SE (Standard Edition).
What is Java-Based Upon?
• Java is based on C and C++.
• The first Java compiler was developed by Sun Microsystems and was
written in C using some libraries from C++.
• Java files are converted to byte code format using a compiler that the
Java interpreter then executes.
• Java code runs on Java Virtual Machine (JVM)—the runtime
environment.
What is Java IDE?
Java IDE (Integrated Development Environment) is a software
application that enables users to write and debug Java programs more
easily.
Most IDEs have features such as syntax highlighting and code
completion that helps users to code more easily.
Usually, Java IDEs include a code editor, a compiler, a debugger,
and an interpreter that the developer may access via a single
graphical user interface.
• Eclipse
• NetBeans
• IntelliJ IDEA
• BlueJ
• JCreator
• JDeveloper
• MyEclipse
• Greenfoot
• DrJava
• Xcode
• Codenvy
Types of Java Applications
Standalone Application It is also known as desktop application or window-based
application. An application that we need to install on every machine such as media player,
antivirus etc. AWT and Swing are used in Java for creating standalone applications.
Web Application An application that runs on the server side and creates dynamic page, is
called web application. Currently, servlet, jsp, struts, jsf etc. technologies are used for
creating web applications in Java.
 Enterprise Application An application that is distributed in nature, such as banking
applications etc. It has the advantage of high level security, load balancing and clustering.
In Java, EJB is used for creating enterprise applications.
Mobile Application An application that is created for mobile devices. Currently Android
and Java ME are used for creating mobile applications.
What is object-oriented programming?
• Object-oriented programming (OOP) is a computer programming
model that organizes software design around data, or objects, rather
than functions and logic.
• An object can be defined as a data field that has unique attributes and
behaviour.
• OOP focuses on the objects that developers want to manipulate rather
than the logic required to manipulate them.
What is the structure of object-oriented
programming?
Classes
Objects
Methods
Attributes/Properties
Main principles of OOP?
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
Benefits OF OOP
Modularity. Encapsulation enables objects to be self-contained, making
troubleshooting and collaborative development easier.
Reusability. Code can be reused through inheritance, meaning a team does not have
to write the same code multiple times.
Productivity. Programmers can construct new programs quicker through the use of
multiple libraries and reusable code.
Easily upgradable and scalable. Programmers can implement system functionalities
independently.
Interface descriptions. Descriptions of external systems are simple, due to message
passing techniques that are used for objects communication.
Security. Using encapsulation and abstraction, complex code is hidden, software
maintenance is easier and internet protocols are protected.
Flexibility. Polymorphism enables a single function to adapt to the class it is placed
in. Different objects can also pass through the same interface.
Features Of JAVA
Simple
Java syntax is based on C++ (so easier for programmers to learn it
after C++).
Java has removed many complicated and rarely-used features, for
example, explicit pointers, operator overloading, etc.
There is no need to remove unreferenced objects because there is an
Automatic Garbage Collection in Java.
Features Of JAVA
Portable
Java is portable because it facilitates you to carry the Java bytecode to
any platform. It doesn't require any implementation.
High-performance
Java is faster than other traditional interpreted programming
languages because Java bytecode is "close" to native code.
It is still a little bit slower than a compiled language (e.g., C++). Java
is an interpreted language that is why it is slower than compiled
languages, e.g., C, C++, etc.
Features Of JAVA
Object-oriented :Java is an object-oriented programming language.
Basic concepts of OOPs are:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
Features Of JAVA
Platform Independent: Java is platform independent because it is
different from other languages like C, C++, etc.
which are compiled into platform specific machines while Java is a
write once, run anywhere language.
Java code can be executed on multiple platforms, for example,
Windows, Linux, Sun Solaris, Mac/OS, etc.
Java code is compiled by the compiler and converted into byte-code.
This byte-code is a platform-independent code because it can be run
on multiple platforms, i.e., Write Once and Run Anywhere (WORA).
Features Of JAVA
Secured: Java is best known for its security. Java is secured because:
• No explicit pointer
• Java Programs run inside a virtual machine sandbox.
Features Of JAVA
Robust: Robustness in Java is a quality of Java programs that ensures
they can handle unexpected events and errors without crashing or
causing harm.
Java is robust because:
It uses strong memory management.
There is a lack of pointers that avoids security problems.
Java provides automatic garbage collection which runs on the Java
Virtual Machine to get rid of objects which are not being used by a Java
application anymore.
There are exception handling and the type checking mechanism in Java.
All these points make Java robust.
Features Of JAVA
Distributed :Java is a distributed language which means that the
program can be design to run on computer networks.
Java provides an extensive library of classes for communicating ,using
TCP/IP protocols such as HTTP and FTP. This makes creating network
connections much easier than in C/C++.
Features Of JAVA
• Interpreted: Unlike most of the programming languages which are
either complied or interpreted, Java is both complied and interpreted.
• The Java compiler translates a java source file to byte codes and the
Java interpreter executes the translated byte codes directly on the
system that implements the Java Virtual Machine.
• These two steps of compilation and interpretation allow extensive
code checking and improved security .
Features Of JAVA
Architectural Neutral: One of the key feature of Java that makes it
different from other programming languages is architectural neutral (or
platform independent).
This means that the programs written on one platform can run on any
other platform without having to rewrite or recompile them.
In other words, it follows ‘Write-once-run-anywhere’ approach.
Components of JAVA
Java, a versatile and widely-used programming language, relies on three
key components to execute code:
Java Development Kit (JDK)
Java Runtime Environment (JRE)
Java Virtual Machine (JVM).
Execution of JAVA Program
Components of JAVA:JDK
Java Development Kit (JDK)
JDK is a set of java modules required for development and execution
of a java application.
The JDK provides us with the JAVAC compiler to compile java
programs.
We require the JDK to convert our source code into a specific format
that can be easily executed by the Java Runtime Environment(JRE).
JDK includes the JRE(to execute your java program), Java
development tools like compiler, jheap , javadoc etc.
JDK is a superset of JRE.
Components of JAVA:Key Components of JDK
JRE :The JDK includes a complete JRE, ensuring that developers
have the necessary runtime environment for testing and debugging
their applications.
Compiler (javac) : The JDK includes the Java compiler, javac, which
translates Java source code into bytecode. This step is crucial for
converting human-readable code into a format that the JVM can
execute.
Debugger (jdb) : JDK provides a debugger, jdb, that allows
developers to inspect and troubleshoot their code during runtime.
JavaDoc : JDK includes the JavaDoc tool for generating
documentation from Java source code comments.
Components of JAVA:JRE
JRE: JRE is Java Runtime Environment.
JRE is an implementation of the JVM which actually executes Java
programs.
It combines the Java Virtual Machine with core classes and libraries
required for running java applications.
JRE is required to execute a Java application, however it doesn't
contains necessary modules for development of java applications.
 JRE is a superset of JVM but subset of JDK.
Components of JAVA:JRE
Components of JRE
JVM : As mentioned earlier, the JVM is a crucial component of the
JRE, responsible for executing Java bytecode.
Class Libraries : JRE includes a set of class libraries, such as Java
Standard Edition (SE) libraries, that provide pre-written code for
common tasks. These libraries facilitate the development of Java
applications by offering ready-to-use components.
Java Archives (JARs) : JRE includes Java Archive files that bundle
class files, resources, and metadata into a single file, simplifying the
distribution and deployment of Java applications.
Components of JAVA:JVM
JVM
• Java Virtual Machine is a program that runs pre compiled Java
programs, which mean JVM converts each bytecode(.class files)
instruction into the machine language instruction that can be executed
by the microprocessor.
Components of JAVA:JVM
 .java source files are compiled into .class files by the javac compiler. This
.class file is given to the JVM which has Class Loader Sub System to
perform the following instructions:
Load the .class file into memory
Verify the bytecode. Any suspicious execution is rejected.
Executes the bytecode.
It also provides some additional features like memory management,
garbage collection, fatal error reporting.
JVM makes Java platform-independent. Because of the JVM, a .class file
can be executed in multiple platforms without any platform specific
changes.
Does JAVA Better than C/C++
HOW JAVA DIFFERENT FROM C
C Language:
–Major difference is that C is a structure oriented language and Java is an object
oriented language and has mechanism to define classes and objects.
–Java does not support an explicit pointer type.
–Java does not have preprocessor, so we cant use #define, #include and #ifdef
statements.
–Java does not include structures, unions and enum data types.
–Java does not include keywords like goto, sizeof and typedef.
–Java adds labeled break and continue statements.
–Java adds many features required for object oriented programming.
How JAVA Differ from C++
Java doesn’t support pointers to avoid unauthorized access of memory locations.
Java does not include structures, unions and enum data types.
Java does not support operator over loading.
Preprocessor plays less important role in C++ and so eliminated entirely in java.
Java does not perform automatic type conversions that result in loss of precision.
Java does not support global variables. Every method and variable is declared within a class and
forms part of that class.
Java does not allow default arguments.
Java does not support inheritance of multiple super classes by a sub class (i.e., multiple
inheritance). This is accomplished by using ‘interface’ concept.
It is not possible to declare unsigned integers in java.
In java objects are passed by reference only. In C++ objects may be passed by value or reference.
New features added in
Multithreading, that allows two or more pieces of the same program
to execute concurrently.
C++ has a set of library functions that use a common header file. But
java replaces it with its own set of API classes.
It adds packages and interfaces.
Java supports automatic garbage collection.
break and continue statements have been enhanced in java to accept
labels as targets.
The use of unicode characters ensures portability.
Objects
• Any entity that has state and behaviour is known as an object.
Brand
Price
Calling
Game
State
Behaviour
An object stores its information in attributes and
discloses its behaviour through methods
Java Classes
• A class in Java is a set of objects which shares common characteristics/ behavior and common
properties/ attributes.
• It is a user-defined blueprint or prototype from which objects are created. For example, Student is a
class while a particular student named Ravi is an object.
Properties of Java Classes
• Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are
created.
• Class does not occupy memory.
• Class is a group of variables of different data types and a group of methods.
• A Class in Java can contain:
• Data member
• Method
• Constructor
• Nested Class
• Interface
Structure Of JAVA Program
Lecture 4
Structure of JAVA Program
Documentation Section
The documentation section is an important section but optional for a Java program.
It includes basic information about a Java program.
• Single-line Comment: It starts with a pair of forwarding slash (//). For example:
//First Java Program
• Multi-line Comment: It starts with a /* and ends with */. We write between
these two symbols. For example:
/*It is an example of ……….multiline comment*/
• Documentation Comment: It starts with the delimiter (/**) and ends with */.
For example:
/**It is an example of documentation comment*/
Structure of JAVA Program
• Import statements
• This line indicates that if you want to use a class of another package,
then you can do this by importing it directly into your program.
import java.util.Scanner; //it imports the Scanner class only
import java.util.*; //it imports all the class of the java.util package
Structure of JAVA Program
Package statement
You can create a package with any name.
 A package is a group of classes that are defined by a name.
That is, if you want to declare many classes within one element, then
you can declare it within a package. It is an optional part of the program.
package package_name;
Structure of JAVA Program
Interface Section
• It is an optional section. We can create an interface in this section if
required. We use the interface keyword to create an interface.
• An interface is a slightly different from the class. It contains
only constants and method declarations.
Class Definition
• In this section, we define the class. It is vital part of a Java program.
• Without the class, we cannot create any Java program.
• A Java program may contain several class definitions. Classes are the main
and essential elements of any Java program
Structure of JAVA Program
Main Method Class
• Every Java stand-alone program requires the main method as the
starting point of the program.
• This is an essential part of a Java program. There may be many
classes in a Java program, and only one class defines the main method.
Structure of JAVA Program
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello Java");
}
}
Structure of JAVA Program
public class Hello
This creates a class called Hello.
All class names must start with a capital letter.
The public word means that it is accessible from any other classes.
Braces
Two curly brackets {...} are used to group all the commands, so it is
known that the commands belong to that class or method.
Structure of JAVA Program
public static void main
oWhen the main method is declared public, it means that it can also be
used by code outside of its class, due to which the main method is
declared public.
oThe word static used when we want to access a method without creating
its object, as we call the main method, before creating any class objects.
oThe word void indicates that a method does not return a value. main() is
declared as void because it does not return a value.
omain is a method; this is a starting point of a Java program.
Structure of JAVA Program
String[] args
oIt is an array where each element of it is a string, which has been
named as "args". If your Java program is run through the console, you
can pass the input parameter, and main() method takes it as input.
System.out.println();
oThis statement is used to print text on the screen as output, where the
system is a predefined class, and out is an object of the PrintWriter
class defined in the system.
2
0
Java Variables:
A variable is a container which holds the value while the Java program is
executed. A variable is assigned with a data type.
 Variable is a name of memory location. There are three types of variables in java:
 local variable : A variable declared inside the body of the
method is called local variable.
 instance variable: A variable declared inside the class but
outside the body of the method, is called an instance
variable.
 static variable : A variable that is declared as static is
called a static variable. It cannot be local. You can create a
single copy of the static variable and share it among all the
instances of the class.
public class A
{ static int m=100;
//static variable void method()
{
int n=90; //local variable
}
public static void main(String
args[])
{
int data=50; //instance variable
} } //end of class
2
1
Data Types in Java:
Data types specify the different sizes and values that can be stored in the variable.
 There are two types of data types in Java:
 Primitive data types: The primitive data
types include boolean, char, byte, short, int,
long, float and double.
 Non-primitive data types: The non-
primitive data types include Classes,
Interfaces, and Arrays.
Primitive data types: :
Data Type Characteristics Range
byte 8 bit signed integer -128 to 127
short 16 bit signed integer -32768 to 32767
int 32 bit signed integer -2,147,483,648 to 2,147,483,647
long 64 bit signed integer -9,223,372,036,854,775,808 to- 9,223,372,036,854,775,807
float 32 bit floating point number + 1.4E-45 to
+ 3.4028235E+38
double 64 bit floating point number + 4.9E-324 to
+ 1.7976931348623157E+308
boolean true or false NA, note Java booleans cannot be converted to or from other types
char 16 bit, Unicode Unicode character, u0000 to uFFFF Can mix with integer types
Unary Operator,
Arithmetic Operator,
Shift Operator,
Relational Operator,
Bitwise Operator,
Logical Operator,
Ternary Operator and
Assignment Operator.
2
6
Decision Making statements:
Decision-making statements decide which statement to execute and when. Decision-
making statements evaluate the Boolean expression and control the program flow
depending upon the result of the condition provided:
 If Statement:
 Simple if statement
 if-else statement
 if-else-if ladder
 Nested if-statement
public class Student
{
public static void main(String[] args)
{
int x = 10;
int y =
12;
if(x+y <
10)
{ System.
out.println
("x + y is
less than
10"); }
else { System.out.println("x + y is greater than
20"); }
}
}
2
8
Loop Statements:
loop statements are used to execute the set of instructions in a repeated order. The
execution of the set of instructions depends upon a particular condition.
for loop : In Java, for loop is similar to C and C++. It enables us to initialize the loop variable,
check the condition, and increment/decrement in a single line of code.
for(initialization, condition, increment/decrement)
{ //block of statements }
while loop : It is also known as the entry-controlled loop since the condition is checked at the
start of the loop. If the condition is true, then the loop body will be executed.
do-while loop : It is also known as the exit-controlled loop since the condition is not checked
in
advance. The syntax of the do-while loop is given below.
while(condition){
//looping statements
}
do {
//statements
} while (condition);
2
7
Decision-making statements decide which statement to execute and when. Decision-
making statements evaluate the Boolean expression and control the program flow
depending upon the result of the condition provided:
switch(expression) {
case value :
// Statements break; // optional case value :
// Statements
break; // optional
// You can have any number of case statements.
default : // Optional
// Statements
}
Switch Statement: In Java, Switch
statements are similar to if-else-if
statements. The switch statement
contains multiple blocks of code called
cases and a single case is executed
based on the variable which is being
switched
Decision Making statements:
Decision-making statements decide which statement to execute and
when. Decision- making statements evaluate the Boolean expression
and control the program flow depending upon the result of the
condition provided:
•Switch Statement: In Java, Switch statements are similar to if-else-if statements.
The switch statement contains multiple blocks of code called cases and a single
case is executed based on the variable which is being switched
Decision Making statements:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Arrays In JAVA
Arrays In JAVA
• Syntax:
dataType [] nameOfArray = new dataType [size]
The Java new keyword is used to create an instance of
the class.
In other words, it instantiates a class by allocating
memory for a new object and returning a reference to
that memory.
We can also use the new keyword to create the array
object
Arrays In JAVA
// Java Program to illustrate how to declare, instantiate,
// initialize and print the Java array.
class Testarray {
public static void main(String args[])
{
int a[]
= new int[5]; // declaration and instantiation
a[0] = 10; // initialization
a[1] = 20;
a[2] = 70;
a[3] = 40;
a[4] = 50;
// traversing array
for (int i = 0; i < a.length;
i++) // length is the property of array
System.out.println(a[i]);
}
}

Java Programming :Basics and Introduction

  • 1.
    Java Programming Ms SimmiS Assistant Professor Department of Computer Science UG Kristu Jayanti(Deemed To Be University
  • 2.
    What is JAVA???? • Java is a widely used object-oriented programming language and software platform that runs on billions of devices, including notebook computers, mobile devices, gaming consoles, medical devices and many others. • The rules and syntax of Java are based on the C and C++ languages. • With it, developers can “write once, run anywhere” (WORA), meaning that the compiled Java code will run on all Java-compatible platforms without the need for recompilation.
  • 3.
    • Java isa high-level, language-specific programming language developed by James Gosling and Green Team the early 1990s . • The members of Green Team: James Gosling, Patrick Naughton, Chris Warth, ED Frank and Mike Sheridan. • Firstly, it was called Greentalk by James Gosling and file extension was .gt. • After that, it was called Oak and was developed as a part of the Green project. History Of
  • 4.
    History Of • In1995, Oak was renamed as Java because it was already a trademark by Oak Technologies. • In 2006 Sun started to make Java available under the GNU operating system General Public License (GPL). • Oracle continues this project called Open JDK.
  • 5.
    Java Version History. •The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. • Versions 1.0 and 1.1 are named as JDK (Java Development Kit). • From versions 1.2 to 1.4, the platform is named as J2SE (Java 2 Standard Edition). • From versions 1.5, Sun introduces internal and external versions. From Java 6, the version name is Java SE X. • Major versions were released after every 2 years, however the Java SE 7 took 5 years to be available after its predecessor Java SE 6, and 3 years for Java SE 8 to be available to public afterward. • Since Java SE 10, new versions will be released very six months.
  • 6.
    Editions In JAVA Programmerscan learn any of these editions based on the application they want to make. • Java Standard Edition[J2SE] - Contains core libraries, like java.lang, java.util, etc. To develop client-side standalone applications or applets • Java Enterprise Edition [J2EE]- Includes Java APIs, like JMS, EJB, JSPs/servlets, etc. To develop server-side applications such as Java servlets and Java Server Pages. • Java Micro Edition [J2ME]- This edition is used to program Java in cell phones, set-top boxes, handhelds, and so on. • The most widely used edition in Java is Java SE (Standard Edition).
  • 7.
    What is Java-BasedUpon? • Java is based on C and C++. • The first Java compiler was developed by Sun Microsystems and was written in C using some libraries from C++. • Java files are converted to byte code format using a compiler that the Java interpreter then executes. • Java code runs on Java Virtual Machine (JVM)—the runtime environment.
  • 8.
    What is JavaIDE? Java IDE (Integrated Development Environment) is a software application that enables users to write and debug Java programs more easily. Most IDEs have features such as syntax highlighting and code completion that helps users to code more easily. Usually, Java IDEs include a code editor, a compiler, a debugger, and an interpreter that the developer may access via a single graphical user interface.
  • 9.
    • Eclipse • NetBeans •IntelliJ IDEA • BlueJ • JCreator • JDeveloper • MyEclipse • Greenfoot • DrJava • Xcode • Codenvy
  • 10.
    Types of JavaApplications Standalone Application It is also known as desktop application or window-based application. An application that we need to install on every machine such as media player, antivirus etc. AWT and Swing are used in Java for creating standalone applications. Web Application An application that runs on the server side and creates dynamic page, is called web application. Currently, servlet, jsp, struts, jsf etc. technologies are used for creating web applications in Java.  Enterprise Application An application that is distributed in nature, such as banking applications etc. It has the advantage of high level security, load balancing and clustering. In Java, EJB is used for creating enterprise applications. Mobile Application An application that is created for mobile devices. Currently Android and Java ME are used for creating mobile applications.
  • 12.
    What is object-orientedprogramming? • Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. • An object can be defined as a data field that has unique attributes and behaviour. • OOP focuses on the objects that developers want to manipulate rather than the logic required to manipulate them.
  • 13.
    What is thestructure of object-oriented programming? Classes Objects Methods Attributes/Properties
  • 14.
    Main principles ofOOP? • Encapsulation • Abstraction • Inheritance • Polymorphism
  • 15.
    Benefits OF OOP Modularity.Encapsulation enables objects to be self-contained, making troubleshooting and collaborative development easier. Reusability. Code can be reused through inheritance, meaning a team does not have to write the same code multiple times. Productivity. Programmers can construct new programs quicker through the use of multiple libraries and reusable code. Easily upgradable and scalable. Programmers can implement system functionalities independently. Interface descriptions. Descriptions of external systems are simple, due to message passing techniques that are used for objects communication. Security. Using encapsulation and abstraction, complex code is hidden, software maintenance is easier and internet protocols are protected. Flexibility. Polymorphism enables a single function to adapt to the class it is placed in. Different objects can also pass through the same interface.
  • 17.
    Features Of JAVA Simple Javasyntax is based on C++ (so easier for programmers to learn it after C++). Java has removed many complicated and rarely-used features, for example, explicit pointers, operator overloading, etc. There is no need to remove unreferenced objects because there is an Automatic Garbage Collection in Java.
  • 18.
    Features Of JAVA Portable Javais portable because it facilitates you to carry the Java bytecode to any platform. It doesn't require any implementation. High-performance Java is faster than other traditional interpreted programming languages because Java bytecode is "close" to native code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted language that is why it is slower than compiled languages, e.g., C, C++, etc.
  • 19.
    Features Of JAVA Object-oriented:Java is an object-oriented programming language. Basic concepts of OOPs are: • Object • Class • Inheritance • Polymorphism • Abstraction • Encapsulation
  • 20.
    Features Of JAVA PlatformIndependent: Java is platform independent because it is different from other languages like C, C++, etc. which are compiled into platform specific machines while Java is a write once, run anywhere language. Java code can be executed on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS, etc. Java code is compiled by the compiler and converted into byte-code. This byte-code is a platform-independent code because it can be run on multiple platforms, i.e., Write Once and Run Anywhere (WORA).
  • 21.
    Features Of JAVA Secured:Java is best known for its security. Java is secured because: • No explicit pointer • Java Programs run inside a virtual machine sandbox.
  • 22.
    Features Of JAVA Robust:Robustness in Java is a quality of Java programs that ensures they can handle unexpected events and errors without crashing or causing harm. Java is robust because: It uses strong memory management. There is a lack of pointers that avoids security problems. Java provides automatic garbage collection which runs on the Java Virtual Machine to get rid of objects which are not being used by a Java application anymore. There are exception handling and the type checking mechanism in Java. All these points make Java robust.
  • 23.
    Features Of JAVA Distributed:Java is a distributed language which means that the program can be design to run on computer networks. Java provides an extensive library of classes for communicating ,using TCP/IP protocols such as HTTP and FTP. This makes creating network connections much easier than in C/C++.
  • 24.
    Features Of JAVA •Interpreted: Unlike most of the programming languages which are either complied or interpreted, Java is both complied and interpreted. • The Java compiler translates a java source file to byte codes and the Java interpreter executes the translated byte codes directly on the system that implements the Java Virtual Machine. • These two steps of compilation and interpretation allow extensive code checking and improved security .
  • 25.
    Features Of JAVA ArchitecturalNeutral: One of the key feature of Java that makes it different from other programming languages is architectural neutral (or platform independent). This means that the programs written on one platform can run on any other platform without having to rewrite or recompile them. In other words, it follows ‘Write-once-run-anywhere’ approach.
  • 26.
    Components of JAVA Java,a versatile and widely-used programming language, relies on three key components to execute code: Java Development Kit (JDK) Java Runtime Environment (JRE) Java Virtual Machine (JVM).
  • 27.
  • 28.
    Components of JAVA:JDK JavaDevelopment Kit (JDK) JDK is a set of java modules required for development and execution of a java application. The JDK provides us with the JAVAC compiler to compile java programs. We require the JDK to convert our source code into a specific format that can be easily executed by the Java Runtime Environment(JRE). JDK includes the JRE(to execute your java program), Java development tools like compiler, jheap , javadoc etc. JDK is a superset of JRE.
  • 29.
    Components of JAVA:KeyComponents of JDK JRE :The JDK includes a complete JRE, ensuring that developers have the necessary runtime environment for testing and debugging their applications. Compiler (javac) : The JDK includes the Java compiler, javac, which translates Java source code into bytecode. This step is crucial for converting human-readable code into a format that the JVM can execute. Debugger (jdb) : JDK provides a debugger, jdb, that allows developers to inspect and troubleshoot their code during runtime. JavaDoc : JDK includes the JavaDoc tool for generating documentation from Java source code comments.
  • 30.
    Components of JAVA:JRE JRE:JRE is Java Runtime Environment. JRE is an implementation of the JVM which actually executes Java programs. It combines the Java Virtual Machine with core classes and libraries required for running java applications. JRE is required to execute a Java application, however it doesn't contains necessary modules for development of java applications.  JRE is a superset of JVM but subset of JDK.
  • 31.
    Components of JAVA:JRE Componentsof JRE JVM : As mentioned earlier, the JVM is a crucial component of the JRE, responsible for executing Java bytecode. Class Libraries : JRE includes a set of class libraries, such as Java Standard Edition (SE) libraries, that provide pre-written code for common tasks. These libraries facilitate the development of Java applications by offering ready-to-use components. Java Archives (JARs) : JRE includes Java Archive files that bundle class files, resources, and metadata into a single file, simplifying the distribution and deployment of Java applications.
  • 32.
    Components of JAVA:JVM JVM •Java Virtual Machine is a program that runs pre compiled Java programs, which mean JVM converts each bytecode(.class files) instruction into the machine language instruction that can be executed by the microprocessor.
  • 33.
    Components of JAVA:JVM .java source files are compiled into .class files by the javac compiler. This .class file is given to the JVM which has Class Loader Sub System to perform the following instructions: Load the .class file into memory Verify the bytecode. Any suspicious execution is rejected. Executes the bytecode. It also provides some additional features like memory management, garbage collection, fatal error reporting. JVM makes Java platform-independent. Because of the JVM, a .class file can be executed in multiple platforms without any platform specific changes.
  • 34.
    Does JAVA Betterthan C/C++
  • 35.
    HOW JAVA DIFFERENTFROM C C Language: –Major difference is that C is a structure oriented language and Java is an object oriented language and has mechanism to define classes and objects. –Java does not support an explicit pointer type. –Java does not have preprocessor, so we cant use #define, #include and #ifdef statements. –Java does not include structures, unions and enum data types. –Java does not include keywords like goto, sizeof and typedef. –Java adds labeled break and continue statements. –Java adds many features required for object oriented programming.
  • 36.
    How JAVA Differfrom C++ Java doesn’t support pointers to avoid unauthorized access of memory locations. Java does not include structures, unions and enum data types. Java does not support operator over loading. Preprocessor plays less important role in C++ and so eliminated entirely in java. Java does not perform automatic type conversions that result in loss of precision. Java does not support global variables. Every method and variable is declared within a class and forms part of that class. Java does not allow default arguments. Java does not support inheritance of multiple super classes by a sub class (i.e., multiple inheritance). This is accomplished by using ‘interface’ concept. It is not possible to declare unsigned integers in java. In java objects are passed by reference only. In C++ objects may be passed by value or reference.
  • 37.
    New features addedin Multithreading, that allows two or more pieces of the same program to execute concurrently. C++ has a set of library functions that use a common header file. But java replaces it with its own set of API classes. It adds packages and interfaces. Java supports automatic garbage collection. break and continue statements have been enhanced in java to accept labels as targets. The use of unicode characters ensures portability.
  • 39.
    Objects • Any entitythat has state and behaviour is known as an object. Brand Price Calling Game State Behaviour An object stores its information in attributes and discloses its behaviour through methods
  • 40.
    Java Classes • Aclass in Java is a set of objects which shares common characteristics/ behavior and common properties/ attributes. • It is a user-defined blueprint or prototype from which objects are created. For example, Student is a class while a particular student named Ravi is an object. Properties of Java Classes • Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created. • Class does not occupy memory. • Class is a group of variables of different data types and a group of methods. • A Class in Java can contain: • Data member • Method • Constructor • Nested Class • Interface
  • 41.
    Structure Of JAVAProgram Lecture 4
  • 43.
    Structure of JAVAProgram Documentation Section The documentation section is an important section but optional for a Java program. It includes basic information about a Java program. • Single-line Comment: It starts with a pair of forwarding slash (//). For example: //First Java Program • Multi-line Comment: It starts with a /* and ends with */. We write between these two symbols. For example: /*It is an example of ……….multiline comment*/ • Documentation Comment: It starts with the delimiter (/**) and ends with */. For example: /**It is an example of documentation comment*/
  • 44.
    Structure of JAVAProgram • Import statements • This line indicates that if you want to use a class of another package, then you can do this by importing it directly into your program. import java.util.Scanner; //it imports the Scanner class only import java.util.*; //it imports all the class of the java.util package
  • 45.
    Structure of JAVAProgram Package statement You can create a package with any name.  A package is a group of classes that are defined by a name. That is, if you want to declare many classes within one element, then you can declare it within a package. It is an optional part of the program. package package_name;
  • 46.
    Structure of JAVAProgram Interface Section • It is an optional section. We can create an interface in this section if required. We use the interface keyword to create an interface. • An interface is a slightly different from the class. It contains only constants and method declarations. Class Definition • In this section, we define the class. It is vital part of a Java program. • Without the class, we cannot create any Java program. • A Java program may contain several class definitions. Classes are the main and essential elements of any Java program
  • 47.
    Structure of JAVAProgram Main Method Class • Every Java stand-alone program requires the main method as the starting point of the program. • This is an essential part of a Java program. There may be many classes in a Java program, and only one class defines the main method.
  • 49.
    Structure of JAVAProgram public class Hello { public static void main(String[] args) { System.out.println("Hello Java"); } }
  • 50.
    Structure of JAVAProgram public class Hello This creates a class called Hello. All class names must start with a capital letter. The public word means that it is accessible from any other classes. Braces Two curly brackets {...} are used to group all the commands, so it is known that the commands belong to that class or method.
  • 51.
    Structure of JAVAProgram public static void main oWhen the main method is declared public, it means that it can also be used by code outside of its class, due to which the main method is declared public. oThe word static used when we want to access a method without creating its object, as we call the main method, before creating any class objects. oThe word void indicates that a method does not return a value. main() is declared as void because it does not return a value. omain is a method; this is a starting point of a Java program.
  • 52.
    Structure of JAVAProgram String[] args oIt is an array where each element of it is a string, which has been named as "args". If your Java program is run through the console, you can pass the input parameter, and main() method takes it as input. System.out.println(); oThis statement is used to print text on the screen as output, where the system is a predefined class, and out is an object of the PrintWriter class defined in the system.
  • 55.
    2 0 Java Variables: A variableis a container which holds the value while the Java program is executed. A variable is assigned with a data type.  Variable is a name of memory location. There are three types of variables in java:  local variable : A variable declared inside the body of the method is called local variable.  instance variable: A variable declared inside the class but outside the body of the method, is called an instance variable.  static variable : A variable that is declared as static is called a static variable. It cannot be local. You can create a single copy of the static variable and share it among all the instances of the class. public class A { static int m=100; //static variable void method() { int n=90; //local variable } public static void main(String args[]) { int data=50; //instance variable } } //end of class
  • 56.
    2 1 Data Types inJava: Data types specify the different sizes and values that can be stored in the variable.  There are two types of data types in Java:  Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.  Non-primitive data types: The non- primitive data types include Classes, Interfaces, and Arrays.
  • 57.
    Primitive data types:: Data Type Characteristics Range byte 8 bit signed integer -128 to 127 short 16 bit signed integer -32768 to 32767 int 32 bit signed integer -2,147,483,648 to 2,147,483,647 long 64 bit signed integer -9,223,372,036,854,775,808 to- 9,223,372,036,854,775,807 float 32 bit floating point number + 1.4E-45 to + 3.4028235E+38 double 64 bit floating point number + 4.9E-324 to + 1.7976931348623157E+308 boolean true or false NA, note Java booleans cannot be converted to or from other types char 16 bit, Unicode Unicode character, u0000 to uFFFF Can mix with integer types
  • 58.
    Unary Operator, Arithmetic Operator, ShiftOperator, Relational Operator, Bitwise Operator, Logical Operator, Ternary Operator and Assignment Operator.
  • 63.
    2 6 Decision Making statements: Decision-makingstatements decide which statement to execute and when. Decision- making statements evaluate the Boolean expression and control the program flow depending upon the result of the condition provided:  If Statement:  Simple if statement  if-else statement  if-else-if ladder  Nested if-statement public class Student { public static void main(String[] args) { int x = 10; int y = 12; if(x+y < 10) { System. out.println ("x + y is less than 10"); } else { System.out.println("x + y is greater than 20"); } } }
  • 64.
    2 8 Loop Statements: loop statementsare used to execute the set of instructions in a repeated order. The execution of the set of instructions depends upon a particular condition. for loop : In Java, for loop is similar to C and C++. It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of code. for(initialization, condition, increment/decrement) { //block of statements } while loop : It is also known as the entry-controlled loop since the condition is checked at the start of the loop. If the condition is true, then the loop body will be executed. do-while loop : It is also known as the exit-controlled loop since the condition is not checked in advance. The syntax of the do-while loop is given below. while(condition){ //looping statements } do { //statements } while (condition);
  • 65.
    2 7 Decision-making statements decidewhich statement to execute and when. Decision- making statements evaluate the Boolean expression and control the program flow depending upon the result of the condition provided: switch(expression) { case value : // Statements break; // optional case value : // Statements break; // optional // You can have any number of case statements. default : // Optional // Statements } Switch Statement: In Java, Switch statements are similar to if-else-if statements. The switch statement contains multiple blocks of code called cases and a single case is executed based on the variable which is being switched
  • 66.
    Decision Making statements: Decision-makingstatements decide which statement to execute and when. Decision- making statements evaluate the Boolean expression and control the program flow depending upon the result of the condition provided: •Switch Statement: In Java, Switch statements are similar to if-else-if statements. The switch statement contains multiple blocks of code called cases and a single case is executed based on the variable which is being switched
  • 67.
    Decision Making statements: switch(expression){ casevalue1: //code to be executed; break; //optional case value2: //code to be executed; break; //optional ...... default: code to be executed if all cases are not matched; }
  • 68.
  • 69.
    Arrays In JAVA •Syntax: dataType [] nameOfArray = new dataType [size] The Java new keyword is used to create an instance of the class. In other words, it instantiates a class by allocating memory for a new object and returning a reference to that memory. We can also use the new keyword to create the array object
  • 70.
    Arrays In JAVA //Java Program to illustrate how to declare, instantiate, // initialize and print the Java array. class Testarray { public static void main(String args[]) { int a[] = new int[5]; // declaration and instantiation a[0] = 10; // initialization a[1] = 20; a[2] = 70; a[3] = 40; a[4] = 50; // traversing array for (int i = 0; i < a.length; i++) // length is the property of array System.out.println(a[i]); } }