2. Contents
⢠History,
⢠Overview of Java,
⢠Object Oriented Programming,
⢠A simple Program,
⢠Data types: Integers, Floating point, characters, Boolean,
⢠A closer look at Literals,
⢠Variables:
⢠Two control statements - if statement, for loop, using Blocks of codes,
⢠Lexical issues - White space, identifiers, Literals, comments, separators, Java Key words.
⢠Type conversion and casting,
⢠Automatic type promotion in Expressions Arrays.
⢠Operators: Arithmetic operators, The Bit wise operators, Relational Operators, Boolean Logical
operators, Assignment Operator, Operator Precedence.
⢠Control Statements: Selection Statements - if, Switch: Iteration Statements - While, Do-while, for Nested
loops, Jump statements.
Bhargavi Dalal 2
3. What is Java?
⢠Java is a
ď high level,
ď robust,
ď object-oriented and
ď secure programming language.
⢠Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the
year 1995.
⢠James Gosling is known as the father of Java.
⢠Before Java, its name was Oak.
⢠Since Oak was already a registered company, so James Gosling and his team changed the
name from Oak to Java.
⢠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.
Bhargavi Dalal 3
4. History of Java
⢠The history of Java is very interesting.
⢠Java was originally designed for interactive television, but it was too
advanced technology for the digital cable television industry at the time.
⢠The history of Java starts with the Green Team.
⢠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.
⢠However, it was best suited for internet programming.
⢠Later, Java technology was incorporated by Netscape.
⢠James Gosling and his team members started the project in the early '90s.
Bhargavi Dalal 4
5. Why Java was named as "Oak"?
⢠Java History from Oak to Java
⢠Why Oak? Oak is a symbol of strength and chosen as a national
tree of many countries like the U.S.A., France, Germany,
Romania, etc.
⢠In 1995, Oak was renamed as "Java" because it was already a
trademark by Oak Technologies.
Bhargavi Dalal 5
6. Java Version History
⢠JDK Alpha and Beta (1995)
⢠JDK 1.0 (23rd Jan 1996)
⢠JDK 1.1 (19th Feb 1997)
⢠J2SE 1.2 (8th Dec 1998)
⢠J2SE 1.3 (8th May 2000)
⢠J2SE 1.4 (6th Feb 2002)
⢠J2SE 5.0 (30th Sep 2004)
⢠Java SE 6 (11th Dec 2006)
⢠Java SE 7 (28th July 2011)
⢠Java SE 8 (18th Mar 2014)
⢠Java SE 9 (21st Sep 2017)
⢠Java SE 10 (20th Mar 2018)
⢠Java SE 11 (September 2018)
⢠Java SE 12 (March 2019)
⢠Java SE 13 (September 2019)
⢠Java SE 14 (Mar 2020)
⢠Java SE 15 (September 2020)
⢠Java SE 16 (Mar 2021)
⢠Java SE 17 (September 2021)
⢠Java SE 18 (to be released by March
2022)
Bhargavi Dalal 6
8. Platform Independent
⢠A platform is the hardware or software environment in
which a program runs.
⢠There are two types of platforms software-based and
hardware-based.
⢠Java provides a software-based platform.
⢠The Java platform differs from most other platforms in
the sense that it is a software-based platform that runs
on top of other hardware-based platforms.
⢠It has two components:
⢠Runtime Environment
⢠API(Application Programming Interface)
Bhargavi Dalal 8
9. Robust
⢠The English mining of Robust is strong.
⢠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.
Bhargavi Dalal 9
10. What is Garbage Collection?
⢠Java programs compile to bytecode that can be run on a Java Virtual
Machine, or JVM for short.
⢠When Java programs run on the JVM, objects are created on the heap,
which is a portion of memory dedicated to the program.
⢠Eventually, some objects will no longer be needed.
⢠The garbage collector finds these unused objects and deletes them to free
up memory.
Bhargavi Dalal 10
11. Memory Management in Java
⢠In Java, memory management is the process of allocation and de-
allocation of objects, called Memory management.
⢠Java does memory management automatically.
⢠Java uses an automatic memory management system called a garbage
collector.
⢠Thus, we are not required to implement memory management logic in
our application.
⢠Java memory management divides into two major parts:
ď JVM Memory Structure
ď Working of the Garbage Collector
Bhargavi Dalal 11
13. JVM Memory Structure
⢠JVM creates various run time data areas in a heap.
⢠These areas are used during the program execution.
⢠The memory areas are destroyed when JVM exits, whereas the data
areas are destroyed when the thread exits.
Bhargavi Dalal 13
14. JVM (Java Virtual Machine)
⢠JVM is the abbreviation for Java virtual machine which is basically
specification that provides a runtime environment in which Java byte code
can be executed i.e it is something which is abstract and its implementation
is independent to choose the algorithm and has been provided by Sun and
other companies.
⢠It is JVM which is responsible for converting Byte code to the machine
specific code.
⢠It can also run those programs which are written in other languages and
compiled to Java bytecode.
⢠The JVM performs the mentioned tasks: Loads code, Verifies code,
Executes code, Provides runtime environment.
Bhargavi Dalal 14
15. JRE(Java Runtime Environment)
⢠JRE is Java runtime environment which is the
implementation of JVM i.e the specifications which are
defined in JVM are implemented and creates
corresponding environment for the execution of code.
⢠JRE comprises mainly java binaries and other classes to
execute the program alike of JVM it physically exists.
⢠Along with Java binaries JRE also consist of various
technologies of deployment, user interfaces to interact
with code executed, some base libraries for different
functionalities and language and util based libraries.
Bhargavi Dalal 15
16. JDK(Java Development Kit)
⢠JDK is abbreviation for Java Development Kit
which includes all the tools, executable and
binaries required to compile, debug and execute a
Java Program.
⢠JDK is platform dependent i.e there is separate
installers for Windows, Mac, and Unix systems.
⢠JDK includes both JVM and JRE and is entirely
responsible for code execution. It is the version of
JDK which represent version of Java.
Bhargavi Dalal 16
18. Installation of JAVA
⢠Prerequisites
⢠A system running Windows 10.
⢠A network connection.
⢠Administrator privileges.
Bhargavi Dalal 18
19. Check if Java Is Installed
⢠Before installing the Java
Development Kit, check if a Java
version is already installed on
Windows. Follow the steps
below:
⢠Open a command prompt by
typing cmd in the search bar and
press Enter.
⢠Run the following command:
⢠java âversion
Bhargavi Dalal 19
20. Java Example
⢠Let's have a quick look at Java programming example.
⢠A detailed description of Hello Java example is available in next page.
⢠class Simple{
⢠public static void main(String args[]){
⢠System.out.println("Hello Java");
⢠}
⢠}
Bhargavi Dalal 20
21. 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.
Bhargavi Dalal 21
22. What happens at runtime?
⢠At runtime, the following steps are performed:
Bhargavi Dalal 22
23. Sr. No Programs
1 Write a program in java print Hello Students.
2 Write a program to find the Sum, Subtraction, Multiplication, Division of two digits for a given number.
3 Write a program to insert element in existing array.
4 Write a program in java to display the even numbers in an array.
5 Write a program to print values in reverse order.
6 Write a program to find if a number is palindrome or not.
7 Understanding the various loops in java programmatically.
8 Write a program to insert element in existing array and sort an existing array.
9 Write a program to show the working of Swith case in Java.
10 Write a program to find area of circle.
11 Write a program to demonstrate various boolean operators & its usage.
12 Demonstration of various arithmatic operators loops etc. Write a program to display a multiplication table.
13 Write a program to take input from User using scanner class in java. Write a program to find the sum of digits of a given
number.
14 Demonstration of Assessment - 1
15 Write a program to check whether two strings are equal or not.
16 Check whether given number is even or odd using bitwise operators.
17 Write a program to display a multiplication table.
Bhargavi Dalal 23
24. Sr. No Programs
18 Write a program to demostrate the concepts of classes in java
19
Write a program to copy a file to another file using Java to package classes. Get the file names at run time and if the target file
is existed then ask confirmation to overwrite and take necessary actions.
20 Write a program to demonstrate the use of constructors.
21 Write a program to understanding Over loading methods.
22
1. Write a program to list files in the current working directory depending upon a given pattern. 2. Write a program to get file
name at runtime and display number of lines and words in that file.
23 Write a program to demonstrate the working of Command line arguments.line arguments
24 Write a program to demonstrate Inheritance in java
25 Demonstration of Assessment - 2
26 Write a program to check all math class functions.
27 Demonstration of Packages & exceptions in Java
28
Create a textfield that allows only numeric value and in specified length. Create a Frame with 2 labels, at runtime display x and
y command-ordinate of mouse pointer in the labels.
29 Write a program to demonstrate Exceptions in Java
30 Write a program to demonstrate Multithreading progrmming in java.
31 Also demonstrate JDBC connections in Java
32 Practice Test Bhargavi Dalal 24
25. OOPs (Object Oriented Programming System)
Object means a real word entity such as pen, chair, table etc.
Object Oriented Programming is a methodology or paradigm to design a program using
classes and objects. It simplifies the software development and maintenance by providing
some concepts:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Bhargavi Dalal 25
26. Encapsulation
⢠Encapsulation is a process of wrapping of data and methods in a single unit is called
encapsulation. Encapsulation is achieved in java language by class concept.
⢠Combining of state and behavior in a single container is known as encapsulation. In java
language encapsulation can be achieve using class keyword, state represents declaration of
variables on attributes and behavior represents operations in terms of method.
Bhargavi Dalal 26
27. Procedural Programming VS Object-Oriented Programming
In procedural programming, the program is divided into
small parts called functions.
In object-oriented programming, the program is divided into
small parts called objects.
There is no access specifier in procedural programming.
Object-oriented programming has access specifiers like
private, public, protected, etc.
Adding new data and functions is not easy. Adding new data and function is easy.
Procedural programming does not have any proper way of
hiding data so it is less secure.
Object-oriented programming provides data hiding so it
is more secure.
In procedural programming, overloading is not possible. Overloading is possible in object-oriented programming.
In procedural programming, there is no concept of data
hiding and inheritance.
In object-oriented programming, the concept of data hiding
and inheritance is used.
Procedural programming is based on the unreal world. Object-oriented programming is based on the real world.
Procedural programming is used for designing medium-sized
programs.
Object-oriented programming is used for designing large and
complex programs.
Procedural programming uses the concept of procedure
abstraction.
Object-oriented programming uses the concept of data
abstraction.
Code reusability absent in procedural programming, Code reusability present in object-oriented programming.
Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.
Bhargavi Dalal 27
28. Java Keyword
⢠Java keywords are also known as reserved words.
⢠Keywords are particular words that act as a key to a code.
⢠These are predefined words by Java so they cannot be used as a variable or object name or class name.
⢠A Java keyword is one of the 50 reserved keywords for a programming language.
⢠Each keyword has a specific meaning in the language.
⢠These 50 keywords are already known in Java.
⢠Rules to follow for keywords:
⢠Keywords cannot be used as an identifier for class, subclass, variable, and methods.
⢠Keywords are case-sensitive.
Bhargavi Dalal 28
29. ⢠Here is a list of keywords in the Java programming language
Bhargavi Dalal 29
30. Some significant points about Java keywords
⢠const and goto are reserved words but not used now.
⢠True, false and null are literals, not keywords.
⢠All keywords are in lower-case.
Bhargavi Dalal 30
31. Identifiers in Java
⢠Identifiers in Java are symbolic names used for identification.
⢠They can be a class name, variable name, method name, package name, constant name, and more.
⢠However, in Java, there are some reserved words that can not be used as an identifier.
⢠For every identifier there are some conventions that should be used before declaring them.
⢠Let's understand it with a simple Java program:
Bhargavi Dalal 31
32. ⢠public class HelloJava {
⢠public static void main(String[] args) {
⢠System.out.println("Hello JavaTpoint");
⢠}
⢠}
Bhargavi Dalal 32
34. From the above example, we have the following Java
identifiers:
1. HelloJava (Class name)
2. main (main method)
3. String (Predefined Class name)
4. args (String variables)
5. System (Predefined class)
6. out (Variable name)
7. println (method)
⢠Public- main method is public so that JVM can access it from outside the class. If this method is not public
JVM will not be able to call it.
⢠Static- means that this method doesnât belong to an object. It belongs to a class and can be called without
any object. Normally any method is called by an object only but at the compile time, there is no object
that exists in the program so until and unless this method is declared static, JVM will not be able to call
this method.
⢠There are some rules and conventions for declaring the
identifiers in Java.
⢠If the identifiers are not properly declared, we may get a
compile-time error.
Bhargavi Dalal 34
35. JAVA Data Types
⢠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.
Bhargavi Dalal 35
36. Java Primitive Data Types
⢠In Java language, primitive data types are
the building blocks of data manipulation.
⢠These are the most basic data types
available in Java language.
⢠There are 8 types of primitive data types:
Bhargavi Dalal 36
39. 1. boolean data type Boolean one = false
It stores only 2 values: true and false.
2. byte data type â byte a = 10
byte b = -20
The byte data type is used to save memory in large arrays where the memory savings is most required. It
saves space because a byte is 4 times smaller than an integer. It can also be used in place of "int" data
type.
3. char data type â char letterA = 'Aâ
The char data type is used to store characters. It is because java uses Unicode system not ASCII code system.
The u0000 is the lowest range of Unicode system.
4. short data type - short s = 10000, short r = -5000
The short data type can also be used to save memory just like byte data type. A short data type is 2 times
smaller than an integer.
Bhargavi Dalal 39
40. 5. int data type - int a = 100000, int b = -200000
The int data type is generally used as a default data type for integral values unless if there is no problem
about memory.
6. long data type - long a = 100000L, long b = -200000L
The long data type is used when you need a range of values more than those provided by int.
7. float data type â float f1 = 234.5f
It is recommended to use a float (instead of double) if you need to save memory in large arrays
of floating point numbers. The float data type should never be used for precise values, such as
currency. Its default value is 0.0F.
8. double data type - double d1 = 12.3
Its value range is unlimited. The double data type is generally used for decimal values just like float.
The double data type also should never be used for precise values, such as currency. Its default value is
0.0d.
Bhargavi Dalal 40
41. Literals in Java
⢠In Java, literal is a notation that represents a fixed value in the source code. Or literals are the constant
values that appear directly in the program.
⢠In lexical analysis, literals of a given type are generally known as tokens.
⢠It can be assigned directly to a variable.
⢠Java has various types of literals.
⢠The following figure represents a literal.
Bhargavi Dalal 41
42. Types of Literals in Java
⢠The types of literals in Java:
⢠Integer Literal â
Integer literals are sequences of digits. There are three types of integer literals Decimal(Decimal integers use a base
ten and digits ranging from 0 to 9), Octal(Octal integers use a base eight and digits ranging from 0 to 7), Hexa-
Decimal(Hexa-decimal integers work with a base 16 and use digits from 0 to 9), Binary Integer(Binary integers uses a
base two, consisting of the digits â0â and â1.â)
⢠Character Literal â
A character literal is expressed as a character or an escape sequence, enclosed in a single quote ('') mark. It is always
a type of char. For example, 'a', '%', 'u000d', etc.
⢠Boolean Literal â
Boolean literals are the value that is either true or false. It may also have values 0 and 1. For example, true, 0, etc.
⢠String Literal â
⢠String literal is a sequence of characters that is enclosed between double quotes ("") marks. It may be alphabet,
numbers, special characters, blank space, etc. For example, "Jack", "12345", "n", etc.
Bhargavi Dalal 42
43. ⢠Floating Point Literals -
The values that contain decimal are floating literals. In Java, float and double primitive types fall into floating
point literals. Keep in mind while dealing with floating-point literals.
Bhargavi Dalal 43
44. Why use literals?
⢠To avoid defining the constant somewhere and making up a label for it.
⢠Instead, to write the value of a constant operand as a part of the instruction.
⢠How to use literals?
⢠A literal in Java can be identified with the prefix =, followed by a specific value.
⢠Let's create a Java program and use above discussed literals.
Bhargavi Dalal 44
46. Example of Object
public class CreateObjectExample1
{
void show()
{
System.out.println("Welcome to javaTpoint");
}
public static void main(String[] args)
{
//creating an object using new keyword
CreateObjectExample1 obj = new CreateObjectExample1();
//invoking method using the object
obj.show();
}
} Bhargavi Dalal 46
47. An object has three characteristics:
⢠State: represents the data (value) of an object.
⢠Behaviour: represents the behaviour (functionality) of an object such as deposit, withdraw, etc.
⢠Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to
the external user.
⢠However, it is used internally by the JVM to identify each object uniquely.
⢠For Example, Pen is an object. Its name is Reynolds; colour is white, known as its state. It is used to write,
so writing is its behaviour.
⢠An object is an instance of a class.
⢠A class is a template or blueprint from which objects are created.
⢠So, an object is the instance(result) of a class.
Bhargavi Dalal 47
48. ⢠Object Definitions:
⢠An object is a real-world entity.
⢠An object is a runtime entity.
⢠The object is an entity which has state and behavior.
⢠The object is an instance of a class.
Bhargavi Dalal 48
49. 3 Ways to initialize object
There are 3 ways to initialize object in Java.
⢠By reference variable
⢠By method
⢠By constructor
Bhargavi Dalal 49
50. 1. Initialization through reference
⢠Initializing an object means storing data into the object.
⢠Let's see a simple example where we are going to initialize the object through a reference variable.
class Student{
int id;
String name;
}
class TestStudent2{
public static void main(String args[]){
Student s1=new Student();
s1.id=101;
s1.name="Sonoo";
System.out.println(s1.id+" "+s1.name); //printing members with a white space
}
}
Bhargavi Dalal 50
51. 2. Initialization through method
⢠In this example, we are creating the two objects of Student class and
initializing the value to these objects by invoking the insert Record
method.
⢠Here, we are displaying the state (data) of the objects by invoking
the displayInformation() method.
Bhargavi Dalal 51
54. 3. By constructor
⢠We will learn about constructors in Java later.
Bhargavi Dalal 54
55. Example of Object
public class Puppy {
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public static void main(String []args) {
// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" );
}
}
Bhargavi Dalal 55
56. Classes
⢠A class is a group of objects which have common properties.
⢠It is a template or blueprint from which objects are created.
⢠It is a logical entity.
⢠It can't be physical.
⢠A Class is like an object constructor, or a "blueprint" for creating objects.
⢠A class in Java can contain:
⢠Fields
⢠Methods
⢠Constructors
⢠Blocks
⢠Nested class and interface
Bhargavi Dalal 56
57. Syntax of class
class MyClass {
// class methods
constructor() { ... }
method1() { ... }
method2() { ... }
method3() { ... }
...
}
In object-oriented programming, a class is an
extensible program-code-template for creating
objects, providing initial values for state (member
variables) and implementations of behavior
(member functions or methods).
class
<class_name>{
field;
method;
}
Bhargavi Dalal 57
59. ⢠//Java Program to illustrate how to define a class and fields
⢠//Defining a Student class.
⢠class Student{
⢠//defining fields
⢠int id; //field or data member or instance variable
⢠String name;
⢠//creating main method inside the Student class
⢠public static void main(String args[]){
⢠//Creating an object or instance
⢠Student s1=new Student(); //creating an object of Student
⢠//Printing values of the object
⢠System.out.println(s1.id); //accessing member through reference variable
⢠System.out.println(s1.name);
⢠}
⢠}
Student â Class name
Id, - Data Member
Name,
City,
Phone,
address
Display() - Method
S1.id
S1.Name
S1.Phone
S1.address
Bhargavi Dalal 59
60. ⢠We can have multiple
classes in different Java
files or single Java file.
⢠If you define multiple
classes in a single Java
source file, it is a good
idea to save the file name
with the class name
which has main()
method.
//Java Program to demonstrate having the main method in another
class
//Creating Student class.
class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains the main
method
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
Bhargavi Dalal 60
61. class Animal {
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move();//output: Animals can move
b.move();//output:Dogs can walk and run
}
}
Bhargavi Dalal 61
62. Assignments
⢠Write a Java program to print 'Hello' on screen and then print your name on a separate line.
Expected Output
Hello
Prini Rastogi
Write a Java program to print the result of the following operations.
Test Data:
a. -5 + 8 * 6
b. (55+9) % 9
c. 20 + -3*5 / 8
d. 5 + 15 / 3 * 2 - 8 % 3
Expected Output :
43
1
19
13
⢠Write a Java program of three numbers and calculate and print the average of the numbers.
Bhargavi Dalal 62
63. Example: Implement fibonacci series program in java without using recursion.
1.class FibonacciExample1{
2.public static void main(String args[])
3.{
4. int n1=0,n2=1,n3,i,count=10;
5. System.out.print(n1+" "+n2); //printing 0 and 1
6.
7. for(i=2;i<count;++i) //loop starts from 2 because 0 and 1 are already printed
8. {
9. n3=n1+n2;
10. System.out.print(" "+n3);
11. n1=n2;
12. n2=n3;
13. }
14.
15.}}
OUTPUT
0 1 1 2 3 5 8 13 21 34
Bhargavi Dalal 63