UNIT 1
UNIT 1
UNIT 1
Constructs in Java
Mr. S. P. Kholambe
Paradigms of programming Language
B Language
COBOL
Pascal
C
Compiler Based
Procedure Oriented
C++
Compiler Based
Object Oriented
Java (Oak) Compiler & Interpreter Based
Basics
Removed many confusing and/or rarely-used features e.g., explicit pointers, operator
overloading etc.
No need to remove unreferenced objects because there is Automatic Garbage Collection in java.
Object-oriented:
Object-oriented means we organize our software as a combination of different types of objects
that incorporates both data and behavior.
Object-oriented programming(OOPs) is a methodology that simplify software development and
maintenance by providing some rules.
Basic concepts of OOPs are:
Robust:
Robust simply means strong. Java uses strong memory management. There are
lack of pointers that avoids security problem. There is automatic garbage
collection in java. There is exception handling and type checking mechanism in
java. All these points makes java robust.
Architecture-neutral:
There is no implementation dependent features e.g. size of primitive types is
set.
Portable:
We may carry the java byte code to any platform.
Java Features
High-performance:
Java is faster than traditional interpretation since byte code is "close" to native
code still somewhat slower than a compiled language (e.g., C++)
Distributed:
We can create distributed applications in java. RMI and EJB are used for creating
distributed applications. We may access files by calling the methods from any
machine on the internet.
Multi-threaded:
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 shares the same memory. Threads are
important for multi-media, Web applications etc.
Java Features
Dynamic:
Java was designed to adapt to an evolving environment:
Even after binaries have been released, they can adapt to a changing environment
Java loads in classes as they are needed, even from across the network It defers
many decisions to runtime, which solves many of the version problems that C++
has Dynamic linking .
Interpreted:
The Java compiler generates byte-codes, rather than native machine code. To
actually run a Java program, you use the Java interpreter to execute the compiled
byte-codes. Java byte-codes provide an architecture-neutral object file format. The
code is designed to transport programs efficiently to multiple platforms.
Software author is protected, since binary byte streams are downloaded and not
the source code
Java Support System
What is JVM?
A specification where working of Java Virtual Machine is specified. But
implementation provider is independent to choose the algorithm. Its
implementation has been provided by Sun and other companies.
An implementation Its implementation is known as JRE.
Runtime Instance Whenever you write java command on the command
prompt to run the java class, and instance of JVM is created.
Java Virtual Machine(JVM)
The JVM performs following operation:
Loads code ,Verifies code, Executes code, Provides runtime environment
Process of Compilation:
Process of Interpretation:
Classes
Name of class
Variable Declaration
Methods Declaration
22
Defining a class
class Circle
{
public double x, y; // centre coordinate
public double r; // radius of the circle
}
Method Declaration
Methods are declared inside the body of the class but immediately after
the declaration of data fields.
The general form of a method declaration is:
In java block of memory that contains space to store the instance variable.
Objects are created dynamically using the new keyword.
There are three steps when creating an object from a class −
Declaration − A variable declaration with a variable name with an object type.
Instantiation − The 'new' keyword is used to create the object.
ObjectName.VariableName=Value
ObjectName.MethodName(Parameter_List)
Example:
c.x=10;
c.area(int radius)
Java Tokens
Token -Smallest individual units in a program is known as Token.
1. Identifiers :
The first category of token is an Identifier.
Identifiers are used by programmers to name things in Java: things such as
variables, methods, fields, classes, interfaces, exceptions, packages, etc.
The rules for identifiers define:
1. Identifier have alphabets, digits, and underscore etc.
2. Identifier not begins with digit.
3. Upper and Lower case are distinct.
4. No limitation of length.
For Example:
area, TOTAL, F_MAX, sum
Java Tokens
2. Keywords :
The second category of token is a Keyword, sometimes called a reserved word.
Keywords are identifiers that Java reserves for its own use.
We can also not attempt to use Boolean value true and false or null in program.
Java Tokens
3. Separators:
The third category of token is a Separator (also known as a punctuator).
There are exactly nine, single character separators in Java.
Seperator are symbols used to indicate where groups of code is divided
and arranged.
separator :- <= ; | , | . | ( | ) | { | } | [ | ]
Java Tokens
4. Operators:
The fourth category of token is an Operator. Java includes 37 operators
that are listed in the table below; each of these operators consist of 1, 2,
or at most 3 special characters.
The keywords instanceof and new are also considered operators in
Java.
Java Tokens
5. Literals :
All values that we write in a program are literals: each belongs to one of
Java's four primitive types (int, double, boolean, char) or belongs to the
special reference type String.
A value written in a Java
program is called a literal;
and, each written literal
belongs in exactly one type.
Java Tokens
6. Comments :
Comments allow us to place any
form of documentation inside our
Java code.
They can contain anything that we
can type on the keyboard: English,
mathematics, even low-
resolution pictures.
Line-Oriented:
begins with // and continues until
the end of the line.
Block-Oriented:
begins with /* and continues
(possibly over many lines) until */ is
reached.
Java Tokens
1. byte:
Byte is a 8 bit data type
It's range is -128 to 127.
It's default value is zero.
It is the smallest java integer type. Byte is four times smaller
than an int.
Example :
byte x = 90,
byte y =- 60.
Primitive Data Types
2. short:
Short is 16 bit signed type.
It's range is 32,768 to 32,767.
Default value is 0.
Short is 2 times smaller than an int.
Example :
short s= 20000 ,
short r = -30000
Primitive Data Types
3. int :
Int is a 32-bit data type.
It's range is from 2,147,483,648 to 2,147,483,647.
For integral value int is generally used.
Default value is 0.
Example :
int a = 200000,
int b = -300000.
Primitive Data Types
4. long:
Long is 64 bit data type.
It's range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Long is used where we need to store a wider range data than int.
It's default value is 0L.
Example :
int a = 100000L,
int b = -200000L.
Primitive Data Types
5. double:
Double is used ,when fractional precision calculation is
required.
It is 64 bit double precision data type.
It's default value is Default value is 0.0d.
Example :
double d = 123.4.
Primitive Data Types
6. float :
float is 32 bit single precision type.
It is used when fractional precision calculation is required.
Default value is 0.0f.
Example :
float f1 = 234.5f.
Primitive Data Types
7. Boolean:
When we need to represent one bit of information, we use Boolean
data type.
It takes two values true and false.
It is generally used where we need to track true or false conditions.
Default value is false.
Example :
Boolean one = true.
Primitive Data Types
8. char:
char is 16 bit type and used to represent Unicode characters.
Range of char is 0 to 65,536.
Example:
char letter ='A' .
What is a Constant?
Character Constants:
Character Constant: A single character constant contain a single character
enclosed with single quotes.
e.g. ‘5’, ‘A’.
String Constants: A sequence of character enclosed with double quotes.
byte y = ( byte ) x;
Implicit Casting (Widening Conversion)
A data type of lower size (occupying less memory) is assigned to a data type of
higher size. This is done implicitly by the JVM.
The lower size is widened to higher size. This is also named as automatic type
conversion.
int x = 10; // occupies 4 bytes
In the above code 4 bytes integer value is assigned to 8 bytes double value.
public class Implicitdemo {
Java Provide default value to the variable for initialize them. The
standard value of different variable listed below:
Operator & Expression
1. Arithmetic
2. Relational
3. Logical
4. Assignment
5. Increment & decrement
6. Conditional
7. Bitwise
8. Special
Arithmetic Operator
Basic arithmetic operators
class ArithmeticOperatorDemo
are: +, -, *, /, %. {
+ is for addition. public static void main(String args[ ])
{
– is for subtraction. int n1 = 100;
* is for multiplication. int n2 = 20;
num2 = num1;
System.out.println("= Output: "+num2);
num2 += num1;
System.out.println("+= Output: "+num2);
num2 -= num1;
System.out.println("-= Output: "+num2);
num2 *= num1;
System.out.println("*= Output: "+num2);
num2 /= num1;
System.out.println("/= Output: "+num2);
num2 %= num1;
System.out.println("%= Output: "+num2);
}
}
Increment & Decrement
Increment and Decrement Operators are Unary Operators.
Operates on One Operand.
Increment Operator is Used to Increment Value Stored inside Variable on which
it is operating.
Decrement Operator is used to decrement value of Variable by 1 (default).
Types:
Pre Increment / Pre Decrement Operator
Post Increment / Post Decrement Operator
class OperatorDemo {
public static void main(String args[])
{
int x=10;
System.out.println(x++); //10 (11)
System.out.println(++x); //12
System.out.println(x--); //12 (11)
System.out.println(--x); //10
}
}
Output:
10
12
12
10
Ternary (Conditional) Operator
2. Dot operator(.) :
This operator is used to access the variables and methods of a class.
Example: student.mark
Here we are accessing the variable “mark” of the “student” object
Arithmetic Expression
Decision-making statements:
1. if statement
2. switch statement
3. Conditional operator statement
4. goto statement
Decision making if statement
The Java if-else statement also tests the public class IfElseExample {
condition. It executes the if block if public static void main(String[] args) {
condition is true otherwise else int number=13;
block is executed.
if(number%2==0){
Syntax:
System.out.println(“Even number");
if(condition){
}else{
//code if condition is true
System.out.println(“Odd number");
}
}
else{
}
//code if condition is false
}
}
Output: Odd number
The Nested if statement
The nested if statement represents the public class JavaNestedIfExample {
if block within another if block. public static void main(String[] args)
Here, the inner if block condition {
executes only when outer if block
int age=20, weight=80;
condition is true.
if(age>=18){
if(condition)
if(weight>50){
{
System.out.println("You are
//code to be executed eligible to donate blood");
if(condition){ }
//code to be executed }
} }}
} Output: You are eligible to donate blood
The if-else-if ladder statement
int number=20;
switch(number){
Java provide following three looping mechanisms. You can use one of the
following three loops:
1. for Loop
2. while Loop
3. do...while Loop
The for Statement
A simple for loop is the same as C/C++. We can initialize the variable, check condition and
increment/decrement value. It consists of four parts:
1. Initialization: It is the initial condition which is executed once when the loop starts.
2. Condition: It is the second condition which is executed each time to test the condition of
the loop. It continues execution until the condition is false.
3. Statement: The statement of the loop is executed each time until the second condition is
false.
4. Increment/Decrement: It increments or decrements the variable value. It is an optional
condition.
Syntax: for (initialization; termination; increment)
{
statement(s);
}
public class ForExample {
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
System.out.println(i);
}
}
}