POINTS TO BE COVERED -
• History and Features of java
• Java compiler and JVM
• Garbage collection
• Data types,
• Control structures in java,
• Arrays in java,
• Concept of class and object,
• Array of objects.
HISTORY OF JAVA
• Developed by James Gosling and his team and was first released in
1995 at Sun Microsystems
• Named after island ‘java’ as the developer team was fan of coffee beans
produced in java
• The symbol of java is also a coffee mug with vapours
• The principles for creating Java programming
• Simple,
• Robust,
• Portable,
• Platform-independent,
• Secured,
• High Performance,
• Multithreaded,
• Architecture Neutral,
• Object-Oriented,
• Interpreted,
• Dynamic
HISTORY OF JAVA
• In 1995, Time magazine called Java one of the Ten Best Products of
1995
• The jdk 1.0 was first released on 23rd January 1996 at Sun
Microsystems which is currently owned by Oracle
• JDK 21 is the latest long-term support release of Java SE Platform
• In every release features are added to previous which makes java more
and more powerful language
• Now Java is being used in –
• Windows applications,
• Web applications,
• Enterprise applications,
• Mobile applications,
• Cards , etc
FEATURES OF JAVA
• Case Sensitive HLL
• Platform independent – JVM (Java Virtual Machine)
• Platform – hardware and software collectively defines platform
• Hardware – Processor, RAM
• Software – Operating System
• Portable
• Uses compiler and interpreter
• Multithreading
• Object Oriented Programming Paradigm
• Robust
• Garbage Collection
• Wide variety of data types
FILES CREATED AND GENERATED WHILE
WRITING PROGRAM
• Source Code –
• Code written in HLL
• Can be understood by programmer
• Executable code
• Translated code in machine language
• Cannot be understood by programmer
• Generates output
• Some more files are generated while compilation
process that depends on HLL and its compiler
WRITING FIRST PROGRAM IN JAVA
APPLICATIONS USED TO WRITE AND EXECUTE
JAVA PROGRAM
• Notepad –
• To write source code
• Save source code file with extension ‘java’
• Command Window
• To compile program
• To execute program
OR
• IDE – Integrated Development Environment
• Many IDEs are used to write java applications like intelliJ
IDEA, Netbeans, Ecllipse etc.
JAVA PROGRAM STRUCTURE
[package <package name>;]
[import <package name>;]
class <class name>
{
public static void main(String arg[])
{
<instructions to be executed>
}
}
FIRST JAVA PROGRAM
class FirstProgram
{
public static void main(String arg[])
{
[Link](“Welcome to the world of JAVA”);
}
}
COMPILING FIRST JAVA PROGRAM
• Setting environment for compiling program
• Check if java is installed on machine
• Search for folder named as ‘java’
• In folder ‘java’ search for ‘jdk’ stands for ‘java development kit’
• In ‘jdk’ folder search folder ‘bin’
• In ‘bin’ files named as ‘javac’ and ‘java’ which are executable
files
• Set path to the java compiler i.e. to command ‘javac’
• Compile java program using command ‘javac’ which will
generate new file known as ‘class file’
• Execute program with command ‘java’ by passing class name as
an argument
JVM – JAVA VIRTUAL MACHINE
• Is a part of JRE which acts as a runtime engine to run java
application
• JVM executes bytecode (.class files)
• It is platform independent
• It is responsible for –
• Loading class
• Allocating memory from Heap
• Maintaining frames for methods (functions) for managing
local variables using stack
• Acts as a virtual processor who interprets the bytecode also
uses JIT (Just In Time) compiler
JVM ARCHITECTURE
GARBAGE COLLECTION -
• Process which helps java programs in memory management
• Objects created by different programs may not be needed after
execution of program
• Garbage collector finds these unused objects and frees the
memory used by these objects
• It frees the memory by removing unreachable objects
• Unreachable objects are those whose references are not being
used by any program in run state
DATA TYPES IN JAVA
WHY DATA TYPES?
• Role of data types in programming
• How much memory?
• How to store value?
• What operations can be performed?
• Types of data types in any language
• Primitive data types
• Non-primitive
• Derived
• User defined
PRIMITIVE DATA TYPES IN JAVA
• Primitive data type allocates memory for variables where
value can be stored
• For every primitive datatype java provides ‘Wrapper Classes’
Data type SIze Type of value Default value
byte 1 byte integer 0
short 2 bytes integer 0
int 4 bytes integer 0
long 8 bytes integer 0
float 4 bytes Floating Point 0.0
double 8 bytes Floating Point 0.0
char 2 bytes Unicode character \u000
boolean 1 byte true or false false
NON PRIMITIVE DATATYPES IN JAVA
• What is non-primitive data types –
• These are datatypes which creates reference and
does not store actual value
• Derived Datatypes
• Arrays
• String
• User Defined Datatypes
• Class
• Interface
IDENTIFIERS IN JAVA
IDENTIFIERS -
• Used for identification by compiler and processor
• It can be variable name, method name, class name etc.
• Java has defined following rules for defining identifiers –
• Only alphanumeric characters ([A-Z],[a-z],[0-9]), ‘$‘(dollar sign)
and ‘_‘ (underscore) are allowed, other than these any special
character is not allowed
• It should not start with digits([0-9]).
• Those are case-sensitive.
• There is no limit on the length of the identifier but it is
advisable to use an optimum length of 4 – 15 letters only.
• Reserved Words can’t be used as an identifier.
NAMING CONVENTIONS IN JAVA
CODING CONVENTIONS IN JAVA
• These are undefined rules followed by programmers to improve
readability of program
• Coding conventions makes easy to read and understand the
code as well as debugging, maintaining software etc.
• Following table defines conventions in java
Naming Convention Application Examples
First Character Lower Variables and methods firstName, lastName
Case sum()
First Character Upper Classes, interfaces, String, MyClass,
Case annotations, enums, StudentData
records
All Characters in Upper constants RATE, MINIMUM_SAL
Case
OPERATORS IN JAVA
WHAT IS OPERATOR?
• A symbol assigned with some predefined process to
be carried out on operands
TYPES OF OPERATORS
• Unary Operators - ++, --
• Arithmetic Operators - +, -, *, /, %
• Assignment Operator - =, +=, -=, *=, /=, %=
• Relational Operators - <, >, <=, >=, !=
• Logical Operators - &&, ||, !
• Ternary Operator - ? :
• Membership operator - .
• ‘instanceof’ operator – instanceof()
• Bitwise Operators
• Shift Operators
• Memory Operators – new, delete
PROGRAMMING CONSTRUCTS IN JAVA
BRANCHING STATEMENT – IF…ELSE
• Used when code is executed based on output of
condition
• Syntax for if statement is given as –
if( < condition > )
{
<block executed when condition is true>
}
[else
{
<block executed when condition is true>
}]
ITERATIVE STATEMENTS
• Used when a block is to be executed iteratively till the condition is
satisfied
• Two types of iterative statements are there –
• Top tested
• Bottom tested
while Statement for Statement Do..while Statement
• Top tested iterative • Top tested iterative • Bottom tested
statement
statement iterative statement
• Syntax –
• Syntax – • Syntax –
for(<initialization>;<cond
while(<condition>) ition>;<increment>) do
•{ { {
• <block> <block> <block>
•} } } while(<condition>);
• If condition is false • If condition is false • Though the condition
donot execute the
donot execute the block is false execute the
block block atleast once
ARRAYS IN JAVA
WHAT IS ARRAY? WHY TO USE ARRAY?
• It is a non-primitive datatype
• Array is defined as a collection of homogeneous variables stored
in memory allocated continuously
• Every element in array shares same name
• Elements are addressed individually by using index of it
• The index of first element is ‘0’ while the index of last element in
array is ‘n-1’ where n is size of array
• Square brackets are used with array name while defining array
and accessing array elements
USING ARRAYS IN JAVA
• Syntax for Defining Array in java –
<data type> [] <array name>;
OR
<data type> <array name>[];
For Example –
int[] a;
OR
int a[];
• Allocating Array –
<data type> [] <array name>=new <data type>[<size>];
For example –
int [] a=new int[10];
int [] a= new int[n];
‘new’ AND ‘delete’ OPERATOR IN JAVA
• Dynamic memory allocation
• Used for non-primitive datatypes
• Operator ‘new’
The keyword ‘new’ is used to allocate memory runtime
It allocates required number of bytes and return reference to be
assigned to an identifier
• Operator ‘delete’
Used to free memory allocated for an identifier
INPUT IN JAVA
INPUT IN JAVA
• To accept input from user in java there are different options
• There are three methods to accept input in java
• As command line arguments
• Using class ‘Scanner’
• Using class ‘BufferedReader’
INPUT AS COMMAND LINE ARGUMENTS
• Values are passed while executing class with main method from
command prompt
• These values are passed as arguments to main method as String
Array
• Can be accessed from String array using array index
• As all values are stored as String, need to be converted to
desired datatype using Wrapper classes and conversion methods
• For example –
int a=[Link](arg[0])
• Above instruction converts first element of String array to intger
using ‘parseInt’ method in wrapper class ‘Integer’
INPUT USING ‘Scanner’ CLASS
• Enables user to accept input during execution of programme
• Steps to use class Scanner –
• Import package ‘util’, where the class Scanner is defined, as –
import [Link].*;
• Define object on class ‘Scanner’ and initialize the object to
‘[Link]’ as –
Scanner objInput = new Scanner([Link]);
• The same object can be used to accept input for different
variables of different types as –
int a=[Link]();
float b=[Link]();
String c=[Link]();
INPUT USING ‘BufferedReader’ CLASS
• Enables user to accept input during execution of programme
• Steps to use class BufferedReader –
• Import package ‘io’, where the class BufferedReader is defined, as –
import [Link].*;
• The class throws IOException which needs to be handled as –
public static void main(String s[]) throws IOException
• Define object on class ‘BufferedReader’ and initialize the object with
object of InputStreamReader which again is initialized with
‘[Link]’ as –
BufferedReader r=new BufferedReader(new
InputStreamReader([Link]));
• The same object can be used to accept input for different variables
of different types as –
int a=[Link]([Link]());
float b=[Link]([Link]());
String c=[Link]();
WRAPPER CLASSES -
• These are the classes defined in [Link] Primitive Wrapper Class
for each primitive datatype Type
• Allows programmer to treat a primitive char Character
datatype value as an object
byte Byte
• Some predefined data structures in java
(collections) and classes from selective short Short
packages like ‘util’ can only handle int Integer
objects in case wrapper classes are
useful which wraps primitive datatype long Long
values in an object
float Float
• Functions in java works with call by value
for primitive datatypes. When values double Double
passed are to be processed using boolean Boolean
reference then wrapper class object can
be used
• Allows programmer to carry out some
additional functionalities to be carried out
on primitive type values like conversion
from one type to other