Unit 1
Unit 1
JDK 1.0 was released on January 23, 1996. After the first release of Java, there have
been many additional features added to the language. Now Java is being used in
Windows applications, Web applications, enterprise applications, mobile applications,
cards, etc. Each new version adds new features in Java.The versions of java are
jdk1.0,jdk 1.1,jdk 1.2,jdk1.4 etc.
Simple
Secure
Portable
Object-oriented
Robust
Architecture-neutral (or) Platform Independent
Multi-threaded
Interpreted
High performance
Distributed
Dynamic
Simple
Java programming language is very simple and easy to learn,
understand, and code. Most of the syntaxes in java follow C language
and object-oriented programming concepts are similar to C++. In a java
programming language, many complicated features like pointers,
operator overloading, structures, unions, etc. have been removed. One of
the most useful features is the garbage collector it makes java more
simple.
Secure
Java is said to be more secure programming language because it does not
have pointers concept, java provides a feature "applet" which can be
embedded into a web application. The applet in java does not allow
access to other parts of the computer, which keeps away from harmful
programs like viruses and unauthorized access.
Portable
Portability is one of the core features of java which enables the java
programs to run on any computer or operating system. For example, an
applet developed using java runs on a wide variety of CPUs, operating
systems, and browsers connected to the Internet.
Object-oriented
Java is said to be a pure object-oriented programming language. In java,
everything is an object. It supports all the features of the object-oriented
programming paradigm. The primitive data types java also implemented
as objects using wrapper classes, but still, it allows primitive data types
to archive high-performance.
Robust
Java is more robust because the java code can be executed on a variety of
environments, java has a strong memory management mechanism
(garbage collector), java is a strictly typed language, it has a strong set of
exception handling mechanism, and many more.
Architecture-neutral (or) Platform Independent
Java has invented to archive "write once; run anywhere, any time,
forever". The java provides JVM (Java Virtual Machine) to to archive
architectural-neutral or platform-independent. The JVM allows the java
program created using one operating system can be executed on any
other operating system.
Multi-threaded
Java supports multi-threading programming, which allows us to write
programs that do multiple operations simultaneously.
Interpreted: Java enables the creation of cross-platform programs by
compiling into an intermediate representation called Java bytecode. The
byte code is interpreted to any machine code so that it runs on the native
machine.
High performance
Java provides high performance with the help of features like JVM,
interpretation, and its simplicity.
Distributed
Java programming language supports TCP/IP protocols which enable
the java to support the distributed environment
of the Internet. Java also supports Remote Method Invocation (RMI), this
feature enables a program to invoke methods across a network.
Dynamic
Java is said to be dynamic because the java byte code may be dynamically
updated on a running system and it has a dynamic memory allocation
and deallocation (objects and garbage collector).
Mantissa e exponent
int x=10;
float f=2.4f;
char c=’e’;
Java allows variables to be initialized dynamically using any expression.
Example
Int c=a+b // ‘c’ is dynamically assigned ( ie run time)
The scope and life time of variables:
The scope of a variable refers to the areas or the sections of the program in which
the variable can be accessed, and the lifetime of a variable indicates how long the variable
stays alive in the memory.
A variable’s scope is, it is accessible only within the block in which it is declared. A block
begins with a left curly brace { and ends with a right curly brace }.
In java there are three major scopes,
1) Class Level Scope
2) Method Level Scope
3) Block level scope
Class level scope
Variables declared inside a class have class level scope and can be accessed by all the
functions in that class.
Method level scope (local variables):
Variables declared inside a method have method level scope and can be accessed inside
the method.
Block level scope
A variable declared inside a block(pair of brackets “{” and “}” ) has block level scope
and can be accessed within the block(brackets) only.
Q.No.7 TYPE CONVERSION AND CASTING
The process of converting the value of one data type (int, float, double, etc.) to another data
type automatically is known as type conversion. The compiler does this automatic conversion at
compile time
Example:
int a= 10;
System.out.println("The integer value: " + a);
float b = a; // convert int into float type
System.out.println("The float value: " + b);
Output
The integer value: 10
The float value: 10.0
Type Casting
It is a process in which the programmer manually converts one data type into another data
type. For this, the casting operator parenthesis () is used.
Example:
float num = 10.99;
System.out.println("The float value: " + num);
// convert into int type
int data = (int)num;
System.out.println("The integer value: " + data);
Output:
The float value: 10.99
The integer value: 10
Automatic type promotion in Expressions
While evaluating expressions, the intermediate value may exceed the range of operands and
hence the expression value will be promoted. Some conditions for type promotion are:
1. Java automatically promotes each byte, short, or char operand to int when evaluating an
expression.
2. If one operand is long, float or double the whole expression is promoted to long, float, or
double respectively.
byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b / c;
The result of a * b exceeds the range of byte. To handle this kind of problem, Java automatically
promotes each byte or short operand to int. a * b is performed using integers.
While evaluating expressions, the result is automatically updated to a larger data type of
the operand. But if we store that result in any smaller data type it generates a compile-time
error, due to which we need to typecast the result.
Automatic promotions can cause compile-time errors.
b = b * 2; // Error! Cannot assign an int to a byte!
In this case we should use an explicit cast,
byte b = 50;
b = (byte) (b * 2);
which yields the correct value of 100
Q.no 8 ARRAYS
An array is a type of variable that can store multiple values in a single
variable.(ie)Array is a collection of related data items of same type that share a common
name.
Types of Array in java
There are three types of array.
i. Single Dimensional Array
Examples:
int[][] a = new int[2][2];
a[1][2] =20;
a[2][1] = 15;
a[2][2] = 25;
or
int[][] a = { {10, 20}, {15, 25} };
10 20
15 25
22 44
iii)Multidimensional Array
Arrays with more than two index values is called multidimensional array.
Syntax:
datatype arrayname[][][]=new
datatype[siz1][size2][size3];
size1 indicates nos.of tables
size2 indicates nos. of rows in each table
size3 indicates nos. of columns in each table
Examples:
int[][] a = new int[2][2][2];
Initialization of multidimensional array in Java:
Assigning values to multidimensional array is called as initialization.
datatype arrayName[][][] = {list of values};
int[][][] a = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
12
34
56
78
Q.No.9 OPERATORS:
Operators are used to perform operations on data.
Types
1)Arithmetic operators
2)Relational operators
3)Logical operators
4)Bitwise operators
5)Assignment operators
Arithmetic operator
Arithmetic are used to perform arithmetic/mathematical operations on operands(variables).
Assume variable A holds 10 and variable B holds 20, then −
Operator Description Example
2) Relational operators
Relational Operators are the operators used to compare the relationship between two
operands. The relational operators and their meanings are shown in below table.
Operator Meaning Example a=10,b=20 Output
Logical operators
Logical Operators are the operators used to compare the relationship between two or
more relational expressions.
Operator Meaning Description Example Output
a=10,b=20
) Assignment operators
Assignment operator is used to assign the result of an expression to a variable.(ie) it assigns
values from the right operands to the left operand. C has a collection of shorthand assignment
operators. The format of assignment operator is,
variablename operator=expression;
Operator Description Example
Operator Description
++ Increment
−− Decrement
Syntax:
Increment operator:
++variblename; (or) variablename++;
Decrement operator: --
--variblename; (or) variablename – -;
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;
Conditional operators
Conditional operators return one value if condition is true and returns another value if the
condition is false.
This operator is also called as ternary operator.
Syntax:
Expression1 ? expression2 : Expression3;
Expression1 is evaluated first. If it is true, then the expression exp2 is evaluated and its value
becomes the value of the expression. If expresssion1 is false, expression3 is evaluated and its value
becomes the value of the expression.
Example:
a=10,b=15,c
c=(a>b) ? a : b
Q.No 10 DECISION MAKING STATEMENTS OR CONTROL STATEMENTS OR
SELECTION STATEMENTS
Decision making statements are used to execute or skip a block of statements based on the
result of condition.(i.e) If the condition is "true" statement block will be executed, if condition is
"false" then statement block will not be executed. These statements control the flow of program
execution, so they are also known as control statements..
Decision making statements are
1)If statement
2)if… else….statement
3)Nested if statement
4)Switch statement
1)If statement
Syntax
If (condition)
{
statement block;
}
Statement-x
Where,
if -- keyword
Condition---relational expression
The statement block may be a single statement or a group of statements. The condition is checked
first. If the condition is true, the statement block will be executed .If the condition is false, the
statement block will be skipped and the execution will jump to the statement-x.
Example
If(a>b)
{
System.out.println(“a is big”);
}
Flow chart of if statement:
2)if… else….statement
The if…..else statement is an extension of the simple if statement. The
general form is,
Syntax
If (condition)
{
statement block1;
}
else
{
statement block2;
}
Statement-x
The condition is checked first. If the condition is true, the statement block1 is executed. If the
condition is false, the statement block2 is executed
Example
int a,b;
if(a>b)
{
System.out.println(“a is big”);
}
else
{
System.out.println(“b is big”);
}
3)Nested if statement or Nesting of if….. else statement
Syntax
The condition1 is checked first. If the condition1 is true, again condition2
is checked. If the condition2 is true, the statement block1 will be executed. If the
condition2 is false, the statement block2 will be executed. If the condition1 is
false, the statement block3 will be executed.
Example
int a,b,c;
If(a>b)
{
if(a>c)
{
System.out.println(“a is big”);
}
else
{
System.out.println (“c is big”);
}
}
else
{
System.out.println (“b is big”);
}
4. else if ladder statement
Syntax:
If (condition1)
{
statement block1;
}
else if(condition2)
{
statement block2;
}
else if (condition3)
{
statement blocl3;
}
..
..
else if(condition n)
{
Statement blockn
}
else
{
default statement block;
}
5)Switch statement
Switch statement is used for complex programs when the number of
alternatives increases. The switch statement tests the value of the given
variable against the list of case values and when a match is found, a block of
statements associated with that case is executed.
Syntax:
switch(expression)
{
case label1:
statement block1;
break;
case label2:
statement block2;
break;
case label3:
statement block3;
break;
……….
………
……….
case labeln:
statement blockn;
break;
default:
default block;
break;
}
case 5:
System.out.println (“Thursday”);
break;
case 6:
System.out.println (“Friday”);
break;
case 7:
System.out.println (“Saturday”);
break;
default:
System.out.println (“invalid day);
break;
}
Q.No 12 JUMP STATEMENTS
Continue
The continue statement used to skip the current iteration of a loop, if a specified condition
occurs, and continues with the next iteration in the loop. We can use continue statement inside any
types of loops such as for, while, and do-while loop.
Syntax:
continue;
for (int i = 1; i <= 10; i++)
{
if (i == 4)
{
continue;
}
System.out.println(i);
}
The above example skips the value of 4
Break Statement
break is a statement that is used to immediately terminate the loop without executing the
remaining part of the body of the loop.
Syntax:
break;
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
System.out.println(i);
}
Types of Loops.
• while loop
• for loop
• do..while
1) While loop
Syntax
while(condition)
{
statement block
}
Example:
class Example
{
public static void main(String args[])
{
int i;
i=1;
while(i<=5)
{
System.out.println("i value"+i);
i++;
}
}
}
Output
1
2
3
4
5
do..while
It is also called an exit-controlled loop. The do…while statement is same as while loop
statement except the condition is specified at the end of the body
Syntax
do
{
statement block;
} while(condition);
In the do-while loop, the body of a loop is always executed at least once. After the
body is executed, then it checks the condition. If the condition is true, then it will again
execute the body of a loop otherwise control is transferred out of the loop.
Example
class Example
{
public static void main(String args[])
{
int i;
i=1;
do
{
System.out.println("i value"+i);
i++;
} while(i<=5);
}}
Output
1
2
3
4
5
iii) for loop
It executes a block of statements as a specified number of
times.
Syntax
for(initilization;condition;increment/decrement)
{
body of statment
}
Steps:
1.Initialize value to variable
2.The condition is checked .If the condition is true, the statement block is
executed . If the condition is false, it exit from the loop
3.Increment/decrement value
4.Goto step 2.
Example:
class Example
{
public static void main(String args[])
{
int i;
for(i=1;i<=5;i++)
{
System.out.println("i value"+i);
}
}
}
Output
1
2
3
4
5