2. Introduction
• Java is a high-level object-oriented programming
language which was developed by James Ghosling, at
Sun Microsystems in 1991.
• The initial name was Oak but it was renamed to Java in
1995 as OAK was a registered trademark of another
Tech company.
• First commercial version of JAVA was JDK 1.0 released
in 1996. Latest version- Java17 (Sept 2021)
• Java is one of the most popular programming
languages in use, particularly for client-server web
applications.
• It is a open-source language which is freely available.
7. Major differences between C And
JAVA.
1. Java is a platform independent whereas C is platform
dependent
2. JAVA is Object-Oriented while C is procedural.
3. Java is an Interpreted language while C is a compiled
language.
4. Memory Management with JAVA & The User-Based
Memory Management in C.
5. JAVA supports Method Overloading while C does not
support overloading at all.
6. Java is robust. It is a strongly typed language having
strict type checking during compile and run time.
where as in case of C, it is not.
8. Java Development Kit
• The Java Development Kit (JDK) is a software
development environment used for
developing Java applications and applets. It
includes the Java Runtime Environment (JRE),
an interpreter/loader (Java), a compiler
(javac), an archiver (jar), a documentation
generator (Javadoc) and other tools needed in
Java development.
9. Java Run-time Environment
• JRE stands for “Java Runtime Environment”.
The Java Runtime Environment provides the
minimum requirements for executing a Java
application; it consists of the Java Virtual
Machine (JVM), core classes, libraries
and supporting files.
10.
11. 1. JDK – Java Development Kit provides the environment to develop
and execute(run) the Java program. It includes two things
• Development Tools(to provide an environment to develop your java
programs)
• JRE (to execute your java program).
2. JRE – Java Runtime Environment is an installation package which
provides environment to only run(not develop) the java program(or
application)onto your machine. JRE is only used by them who only
wants to run the Java Programs i.e. end users of your system.
3. JVM – Java Virtual machine(JVM) is a very important part of both
JDK and JRE because it is contained or inbuilt in both. Whatever
Java program you run using JRE or JDK goes into JVM and JVM is
responsible for executing the java program line by line hence it is
also known as interpreter.
12. • a Java source file saved as Example.java. The
file is compiled into a set of Byte Code that is
stored in a “.class” file. Here it will be
“Example.class“.
13. The following actions occur at runtime.
1. Class Loader- The Class Loader loads all necessary classes
needed for the execution of a program.
2. Byte Code Verifier- The JVM puts the code through the
Byte Code Verifier that checks the format and checks for
an illegal code.
• The Byte Code verifier ensures that the code adheres to
the JVM specification and does not violate system
integrity.
3. Intrepreter- At runtime the Byte Code is loaded, checked
and run by the interpreter. The interpreter has the
following two functions:
– Execute the Byte Code
– Make appropriate calls to the underlying hardware
14. Java Virtual Machine- Interpreter
• JVM(Java Virtual Machine) acts as a run-time engine to run
Java applications. JVM is the one that actually calls
the main method present in a java code. JVM is a part of
JRE(Java Run Environment).
• Java applications are called WORA (Write Once Run
Everywhere). This means a programmer can develop Java
code on one system and can expect it to run on any other
Java enabled system. This is all possible because of JVM.
• When we compile a .java file, a .class file(contains byte-
code) with the same filename is generated by the Java
compiler. This .class file goes into various steps when we
run it. These steps together describe the whole JVM.
15. Evolution of Java
• JDK Alpha and Beta (1995): Sun announced Java in September 23, 1995.
• JDK 1.0 (January 23, 1996): Originally called Oak (named after the oak tree outside James
Gosling's office). Renamed to Java 1 in JDK 1.0.2.
• JDK 1.1 (February 19, 1997): Introduced AWT event model, inner class, JavaBean, JDBC (Java
database connectivity), and RMI.
• J2SE 1.2 (JDK 1.2) (December 8, 1998): Re-branded as "Java 2" and renamed JDK to J2SE (Java
2 Standard Edition). Also released J2EE (Java 2 Enterprise Edition) and J2ME (Java 2 Micro
Edition). Included JFC (Java Foundation Classes - Swing, Accessibility API, Java 2D, Pluggable
Look and Feel and Drag and Drop).
• J2SE 1.4 (JDK 1.4) (February 6, 2002): Introduced assert, non-blocking IO (nio), logging API,
image IO, Java webstart, regular expression support.
• J2SE 5.0 (JDK 1.5) (September 30, 2004): Officially called 5.0 instead of 1.5. Introduced
generics, autoboxing/unboxing, annotation, enum, varargs, for-each loop, static import.
• Java SE 6 (JDK 1.6) (December 11, 2006): Renamed J2SE to Java SE (Java Standard Edition).
• Java SE 7 (JDK 1.7) (July 28, 2011): First version after Oracle purchased Sun (called Oracle
JDK).
• Java SE 8 (JDK 1.8) (March 18, 2014): included support for Lambda expressions, default and
static methods in interfaces, improved collection, and JavaScript runtime. Also integrated
JavaFX graphics subsystem.
• Java SE 9 (JDK 9) (September 21, 2017): introduced modularization of the JDK (module)
under project Jigsaw, the Java Shell (jshell), and more.
• Java SE 10 (JDK 10) (March, 2018): introduced var for type inference local variable (similar to
JavaScript).
16. Java Hotspot Client and Server
Machines
• The Java HotSpot Client Virtual Machine serves as a
replacement for both the "classic" virtual machine and
the Just-in-time (JIT) compilers used by previous
versions of the Java 2 SDK to offer improved runtime
performance for applications and applets.
• The Java HotSpot Client VM has been specially tuned
to reduce application start-up time and memory
footprint, making it particulary well suited for client
environments.
• On all platforms, the Java application launcher
(the java utility) will use Java HotSpot Client VM by
default when launching applications
17. Applications of Java
• Developing Desktop Applications
• Web Applications like Linkedin.com, Snapdeal.com etc
• Web Servers and Application Servers
• Enterprise Applications
• Scientific Applications- MATLAB
• Mobile Operating System like Android
• Embedded Systems
• Robotics and games etc.
18. Features of Java
1. Object Oriented − In Java, everything is an Object. Java can be easily extended since
it is based on the Object model. Object-oriented means we organize our software as
a combination of different types of objects that incorporates both data and behavior.
Basic concepts of OOPs are:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
2. Simple − Java is designed to be easy to learn. If you understand the basic concept of
OOP Java, it would be easy to master.
• Java syntax is based on 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.
19. 3. 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 interpreted by the Virtual Machine (JVM) on whichever
platform it is being run on.
• Java code can be run on multiple platforms, for example, Windows, Linux, Sun
Solaris, Mac/OS, etc. Java code is compiled by the compiler and converted into
bytecode.
• This bytecode is a platform-independent code because it can be run on multiple
platforms, i.e., Write Once and Run Anywhere(WORA).
20. 4. Secure − With Java's secure feature it enables to develop virus-free, tamper-free
systems. Authentication techniques are based on public-key encryption.
• Java is best known for its security. With Java, we can develop virus-free systems.
Java is secured because:
• No explicit pointer
• Classloader: Classloader in Java is a part of the Java Runtime Environment(JRE)
which is used to load Java classes into the Java Virtual Machine dynamically. It
adds security by separating the package for the classes of the local file system
from those that are imported from network sources.
• Bytecode Verifier: It checks the code fragments for illegal code that can violate
access right to objects.
• Security Manager: It determines what resources a class can access such as
reading and writing to the local disk.
5. 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.
• Java is architecture neutral because there are no implementation dependent
features, for example, the size of primitive types is fixed.
• In C programming, int data type occupies 2 bytes of memory for 32-bit
architecture and 4 bytes of memory for 64-bit architecture. However, it occupies
4 bytes of memory for both 32 and 64-bit architectures in Java.
21. 6. Portable − Being architecture-neutral and having no implementation dependent aspects
of the specification makes Java portable.
7. Robust − Java makes an effort to eliminate error prone situations by emphasizing mainly
on compile time error checking and runtime checking. It provides Exception Handling.
• Robust simply means strong. Java is robust because:
• It uses strong memory management.
• There is a lack of pointers that avoids security problems.
• There is automatic garbage collection in java which runs on the Java Virtual Machine to
get rid of objects which are not being used by a Java application anymore.
8. 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.
• A thread is like a separate program, executing concurrently. We can write Java programs
that deal with many tasks at once by defining multiple threads. The main advantage of
multi-threading is that it doesn't occupy memory for each thread. It shares a common
memory area. Threads are important for multi-media, Web applications, etc.
9. High Performance − With the use of Just-In-Time compilers, Java enables 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.
10. Distributed − Java is designed for the distributed environment of the internet.
22. First Java Program
class Hello
{
public static void main(String[] args)
{
System.out.println ("Hello World program");
}
}
23. • class : class keyword is used to declare classes in Java
• public : It is an access specifier. Public means this function is visible
to all and any other classes can call the main() method without any
restrictions.
• static : static is a keyword used to make a function static. To execute
a static function you do not have to create an Object of the class.
The main() method here is called by JVM, without creating any
object for class.
• void : It is the return type, meaning this function will not return
anything.
• main : main() method is the most important method in a Java
program. This is the method which is executed, hence all the logic
must be inside the main() method. If a java class is not having a
main() method, it causes compilation error.
• String[] args : This represents an array whose type is String and
name is args. It is used to access command line arguments.
• System.out.println : This is used to print anything on the console
like printf in C language.
24. • Case Sensitivity − Java is case sensitive, which
means identifier Hello and hello would have
different meaning in Java.
• 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 MyFirstJavaProgram
• 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()
26. • After writing your Java program, when you will try to
compile it. Compiler will perform some compilation
operation on your program.
• Once it is compiled successfully byte code(.class file) is
generated by the compiler.
• After compiling when you will try to run the byte
code(.class file), the following steps are performed at
runtime:-
• Class loader loads the java class. It is subsystem of JVM
Java Virtual machine.
• Byte Code verifier checks the code fragments for illegal
codes that can violate access right to the object.
• Interpreter reads the byte code stream and then
executes the instructions, step by step.
27. Data Types in Java
Data types specify size and the type of values that can be
stored in an identifier.
In java, data types are classified into two catagories :
• Primitive Data type
• Non-Primitive Data type
28. Primitive data type
A primitive data type can be of eight types :
• Char
• Boolean
• Byte
• Short
• Int
• Long
• Float
• Double
• Once a primitive data type has been declared its type can
never change. These eight primitive type can be put into
four groups
29. Integer
• This group includes byte, short, int, long
• byte : It is 1 byte(8-bits) integer data type. Value
range from -128 to 127. Default value zero.
example: byte b=10;
• short : It is 2 bytes(16-bits) integer data type.
Value range from -32768 to 32767. Default value
zero.
• int : It is 4 bytes(32-bits) integer data type. Value
range from -2147483648 to 2147483647. Default
value zero. example: int i=10;
• long : It is 8 bytes(64-bits) integer data type.
Value range from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807. Default value zero.
example: long l=100012;
30. Floating-Point Number
This group includes float, double
• float : It is 4 bytes(32-bits) float data type.
Default value 0.0f. example: float ff=10.3f;
• double : It is 8 bytes(64-bits) float data type.
Default value 0.0d. example: double
db=11.123;
31. Characters
This group represent char, which represent
symbols in a character set, like letters and
numbers.
• char : It is 2 bytes(16-bits) unsigned unicode
character. Range 0 to 65,535. example: char
c='a';
32. Boolean
• This group represent boolean, which is a
special type for representing true/false values.
They are defined constant of the language.
example: boolean b=true;
34. Identifiers in Java
• All Java components require names for classes, methods,
interfaces and variables which are called Identifier.
• Identifier must follow some rules. Here are the rules:
• All identifiers must start with either a letter( a to z or A to Z ) or
currency character($) or an underscore.
• After the first character, an identifier can have any combination
of characters.
• A Java keyword cannot be used as an identifier.
• Identifiers in Java are case sensitive, hello and Hello are two
different identifiers.
•
35. Type Casting
• Assigning a value of one type to a variable of
another type is known as Type Casting.
• Eg:
• int x = 10;
• byte y = (byte)x;
36. • In Java, type casting is classified into two
types,
• Widening Casting(Implicit)
• Narrowing Casting(Explicitly done)
37. Widening or Automatic type
conversion
• Automatic Type casting take place when,the
two types are compatible
• the target type is larger than the source type
38. Implicit Type conversion example
public class Program
{
public static void main(String[] args)
{
int i = 100;
long l = i; //explicit type casting is not required
float f = l; // explicit type casting is not required System.out.println("Int
value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
} }
Output:
Int value 100
Long value 100
Float value 100.0
39. Explicit type conversion
When you are assigning a larger type value to a variable of smaller type, then
you need to perform explicit type casting.
Example :
public class Program2
{
public static void main(String[] args)
{
double d = 100.04;
long l = (long)d; //explicit type casting is required
int i = (int)l; //explicit type casting is required
System.out.println("Double value "+d);
System.out.println("Long value "+l);
System.out.println("Int value "+i);
} }
• Output:
Double value 100.04
Long value 100
Int value 100
40. • String cannot be converted to int, float by Implicit or
Explicit method.
• The Integer.parseInt() java method is used primarily
in parsing a String method argument into an Integer object.
• Float.parseFloat() java for Float object.
Eg:
String name= “6”;
int h= (int) name; -> Error
S.O.Pln(h);
String name=“6”;
Int h= Integer.parseInt(name);
S.O.Pln(h);
41. Java Keywords
• The following list shows the reserved words in Java. These reserved words
may not be used as constant or variable or any other identifier names.
42. Comments in Java
Java supports single-line and multi-line comments very similar to C
and C++.
public class Program
{
/* This is my first java program.
This will print 'Hello World' as the output
This is an example of multi-line comments.
*/
public static void main(String []args)
{
// This is an example of single line comment
System.out.println("Hello World");
}
}
43. Variable
• When we want to store any information, we store it in an address
of the computer. Instead of remembering the complex address
where we have stored our information, we name that address.
• A variable is the name given to a memory location. It is the basic
unit of storage in a program.
• The value stored in a variable can be changed during program
execution.
• In Java, all the variables must be declared before they can be used.
• Java Programming language defines mainly three kind of variables.
- Instance variables
- Static Variables
- Local Variables
44. 1. Local variables
• Local variables are declared in method, constructor or block. Local
variables are initialized when method, constructor or block start
and will be destroyed once its end.
float getDiscount(int price)
{
float discount;
discount=price*(20/100);
return discount;
}
• Here discount is a local variable.
55. 2. Instance Variables
• Instance variables are variables that are declare inside
a class but outside any method , constructor or block.
• As instance variables are declared in a class, these
variables are created when an object of the class is
created and destroyed when the object is destroyed.
• Eg:
class Student
{
String name;
int marks;
}
56.
57. 3. Static variables
• Static are class variables declared with static keyword. Static variables are
initialized only once. Static variables are also used in declaring constant
along with final keyword.
class Student
{
String name;
static int collegeCode=430;
}
58. Instance variable Vs Static variable
Each object will have its own copy of instance
variable whereas We can only have one
copy of a static variable per class irrespective
of how many objects we create.
59.
60. Arrays
• An array is a collection of similar data types.
Array is a container object that hold values of
homogenous type. The size of an array must be
specified at the time of its declaration.
• It is always indexed. Index begins from 0.
• It is a collection of similar data types.
• It occupies a contiguous memory location.
62. Initialization of array
new operator is used to initialize an array.
Example :
int[] arr1 = new int[20]; //this creates an empty
array named ar1r of integer type whose size is
20.
or
int[] arr = {1,2,3,4,5}; //this creates an array
named arr whose elements are given.
63. Accessing array element
int[ ] arr = {1,2,3,4};
System.out.println("Element at 4th place" + arr[3]);
Multidemensional Array
int[ ][ ] arr = new int[10][10]; //10 by 10 is the size of
array.
Or
int[ ][ ] arr = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
// 3 by 5 is the size of the array.
64. Accessing array elements in
Multidimensional array
int arr[ ][ ] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
System.out.println("Element at (2,3) place" + arr[1][2]);
//as array indices start at 0 position, thus (2,3) 2 here
means [1] and 3 means [2]
65.
66.
67.
68.
69. Scanner Classes in Java
Scanner is a class in java.util package used for obtaining the
input of the primitive types like int, double etc. and strings.
It is the easiest way to read input in a Java program.
• To create an object of Scanner class, we usually pass the
predefined object System.in, which represents the standard
input stream. We may pass an object of class File if we
want to read input from a file.
• To read numerical values of a certain data type int, the
function to use is nextInt(). For example, to read a value of
type short, we can use nextShort()
• To read strings, we use nextLine().
• To read a single character, we use next().charAt(0).
• next() function returns the next token/word in the input as
a string and charAt(0) funtion returns the first character in
that string.
76. Java Packages
• Package in Java is a mechanism to encapsulate a
group of classes, sub packages and interfaces.
• Packages prevents naming conflicts.
For example there can be two classes with same
in two packages.
• Provides controlled access: protected and default
have package level access control. A protected
member is accessible by classes in the same
package and its subclasses. A default member
(without any access specifier) is accessible by
classes in the same package only.
• Packages can be considered as data
encapsulation (or data-hiding).
77. • Package in java can be categorized in two
form, built-in package and user-defined
package.
• There are many built-in packages such as
java, lang, awt, javax, swing, net, io, util, sql
etc.
78. Built-in Packages
These packages consist of a large number of classes which are a
part of Java API.
Some of the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed
which defines primitive data types, math operations). This
package is automatically imported.
2) java.io: Contains classed for supporting input / output
operations.
3) java.util: Contains utility classes which implement data
structures like Linked List, Dictionary and support ; for Date /
Time operations.
5) java.awt: Contain classes for implementing the components
for graphical user interfaces (like button , ;menus etc).
6) java.net: Contain classes for supporting networking
operations.
79. User-defined packages
•
These are the packages that are defined by
the user. First we create a
directory myPackage (name should be
same as the name of the package).
• Then create the MyClass inside the
directory with the first statement being
the package names.
80. Three ways to access a Package
1. import package.*;
• If you use package.* The import keyword will make the classes and
interface of another package accessible to the current package.
2. import package.classname; - If you import package.classname then
only declared class of this package will be accessible.
3. fully qualified name - If you use fully qualified name then only
declared class of this package will be accessible. Now there is no need
to import. But you need to use fully qualified name every time when
you are accessing the class or interface.
Eg: myPackage.A obj = new myPackage.A();