0% found this document useful (0 votes)
3 views159 pages

Module 1

The document provides an overview of Object-Oriented Programming (OOP) using Java, explaining its two paradigms: process-oriented and object-oriented models. It covers fundamental concepts such as class structure, data types, variable declaration, and the use of comments, along with examples of basic Java programs. Additionally, it discusses lexical issues, including identifiers, literals, and the importance of Java being a strongly typed language.

Uploaded by

4mt24cs232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views159 pages

Module 1

The document provides an overview of Object-Oriented Programming (OOP) using Java, explaining its two paradigms: process-oriented and object-oriented models. It covers fundamental concepts such as class structure, data types, variable declaration, and the use of comments, along with examples of basic Java programs. Additionally, it discusses lexical issues, including identifiers, literals, and the importance of Java being a strongly typed language.

Uploaded by

4mt24cs232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object Oriented

Programming with
JAVA
– 23CSPC210
Object-Oriented Programming
• Two Paradigms
– All computer programs consist of two elements: code
and data.
• Furthermore, a program can be conceptually
organized around its code or around its data. That
is, some programs are written around “what is
happening” and others are written around “who
is being affected.”
• These are the two paradigms that govern how a
program is constructed.
Object-Oriented Programming
• process-oriented model
– The process-oriented model can be thought of as code
acting on data.
– C employ this model.
• object-oriented programming
– Object-oriented programming organizes a program
around its data (that is, objects) and a set of well-
defined interfaces to that data.
– An object-oriented program can be characterized as
data controlling access to code.
Object-Oriented Programming
Object-Oriented Programming
class
{
//member data
int x;
int y;

//member method
public void getxy()
{
//code
}
public void putxy()
{
//code
}
}
A First Simple Program

• Enter the program and save as [Link]


• Compile the Program
c:\myjava\javac [Link]
• Run the Program
c:\myjava\javac Example
Comments
• The contents of a comment are ignored by the
compiler. Instead, a comment describes or explains
the operation of the program to anyone who is
reading its source code.
• Java supports three styles of comments.
1. multiline comment (/* */)
2. single-line comment (// )
3. documentation comment (/** */)
– This type of comment is used to produce an HTML file
that documents your program.
public static void main(String args[])
• The public keyword is an access specifier, which allows the
programmer to control the visibility of class members.
• When a class member is preceded by public, then that
member may be accessed by code outside the class in which
it is declared.
• The keyword static allows main( ) to be called without
having to instantiate a particular instance of the class.
• This is necessary since main( ) is called by the Java Virtual
Machine before any objects are made.
• The keyword void simply tells the compiler that main() does
not return a value.
• In main( ), there is only one parameter, String args[ ]
declares a parameter named args, which is an array of
instances of the class String.
[Link]();
• println( ) displays the string which is passed to it.
• System is a predefined class that provides access
to the system, and out is the output stream that is
connected to the console.
• All statements in Java end with a semicolon.
• A variable is a named memory location that may
be assigned a value by your program. The value of
a variable may be changed during the execution of
the program.
class Example2 {
public static void main(String args[]) {
int num; // this declares a variable called num
num = 100; // this assigns num the value 100

[Link]("This is num: " + num);

num = num * 2;
[Link]("The value of num * 2 is ");
[Link](num);
}
}

type var-name;
type specifies the type of variable being declared,
and var-name is the name of the variable.
Using Blocks of Code
• Java allows two or more statements to be grouped
into blocks of code, also called code blocks.
• This is done by enclosing the statements between
opening and closing curly braces.
• Once a block of code has been created, it becomes
a logical unit that can be used any place that a
single statement can.
if(x < y) { // begin a block
x = y;
y = 0;
} // end of block
/* Demonstrate a block of code. */
class BlockTest {
public static void main(String args[]) {
int x, y;
y = 20;
// the target of this loop is a block
for(x = 0; x<10; x++) {
[Link]("This is x: " + x);
[Link]("This is y: " + y);
y = y - 2;
}
}
} This is x: 3
Output: This is y: 14
This is x: 4 This is x: 7
This is x: 0
This is y: 12 This is y: 6
This is y: 20
This is x: 5 This is x: 8
This is x: 1
This is y: 10 This is y: 4
This is y: 18
This is x: 6 This is x: 9
This is x: 2
This is y: 8 This is y: 2
This is y: 16
Lexical Issues
• Java programs are a collection of whitespace,
identifiers, literals, comments, operators, separators,
and keywords.
• Whitespace
– Java is a free-form language. This means that you do not
need to follow any special indentation rules.
– In Java, whitespace is a space, tab, or newline.
• Identifiers
– Identifiers are used for class names, method names, and
variable names.
– An identifier may be any descriptive sequence of uppercase
and lowercase letters, numbers, or the underscore and
dollar-sign characters.
– They must not begin with a number.
– Java is case-sensitive.
Lexical Issues
• Literals
– A constant value in Java is created by using a literal
representation of it.
• Comments
– Three types of comments defined by Java.
Lexical Issues
• Separators
– In Java, there are a few characters that are used as
separators.
– The most commonly used separator in Java is the
semicolon.
Lexical Issues
• The Java Keywords
– There are 50 keywords currently defined in the Java
language
– These keywords cannot be used as names for a variable,
class, or method.
– In addition to the keywords, Java reserves the following:
true, false, and null. These are values defined by Java.
The Java Class Libraries
• println( ) and print( ), these methods are
members of the System class.
Java Is a Strongly Typed Language
• Java is a strongly typed language.
• First, every variable has a type, every expression has
a type, and every type is strictly defined.
• Second, all assignments, whether explicit or via
parameter passing in method calls, are checked for
type compatibility.
• There are no automatic coercions or conversions of
conflicting types as in some languages.
• The Java compiler checks all expressions and
parameters to ensure that the types are compatible.
• Any type mismatches are errors that must be
corrected before the compiler will finish compiling
the class.
Data Types in Java
• Data types specify the different sizes and values that can be
stored in the variable.
• There are two types of data types in Java:
1. Primitive data types: The primitive data types include boolean,
char, byte, short, int, long, float and double.
2. Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays.
• Integers: This group includes byte, short, int, and long,
which are for whole-valued signed numbers.
• Floating-point numbers: This group includes float and
double, which represent numbers with fractional precision.
• Characters :This group includes char, which represents
symbols in a character set, like letters and numbers.
• Boolean: This group includes boolean, which is a special
type for representing true/false values.
Data Default Default Example Range
Type Value size
boolean false 1 bit Boolean true and false
one = false
char '\u0000' 2 byte char ch = '\u0000' (or 0) to '\uffff' (or 65,535)
'A' (or) 0 to 65,536
byte 0 1 byte byte a = 10 -128 to 127
Short 0 2 byte short s = –32,768 to 32,767
10000
int 0 4 byte int a = –2,147,483,648 to 2,147,483,647
100000
long 0L 8 byte long a = –9,223,372,036,854,775,808 to
100000L 9,223,372,036,854,775,807
float 0.0f 4 byte float f1 = 1.4e–045 to 3.4e+038
234.5f
double 0.0d 8 byte double d1 4.9e–324 to 1.8e+308
= 12.3

char uses 2 byte in java and \u0000, java uses Unicode system not ASCII code system.
// Compute the area of a circle.
class Area {
public static void main(String args[]) {
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area
[Link]("Area of circle is " + a);
}
}

// Demonstrate char data type. class CharDemo {


public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
[Link]("ch1 and ch2: ");
[Link](ch1 + " " + ch2);
}
}
output:
ch1 and ch2: X Y
// char variables behave like integers.
class CharDemo2 {
public static void main(String args[]) {
char ch1;
ch1 = 'X';
[Link]("ch1 contains " + ch1);
ch1++; // increment ch1
[Link]("ch1 is now " + ch1);
}
}

Output:
ch1 contains X
ch1 is now Y
Exercises
• Addition of two numbers.
• Convert Fahrenheit to Celsius.
• Area of circle
• Simple Interest.

int x = 10;
int y = 9;
[Link](x > y); // returns true, because 10 is higher than 9
Integer Literals
• Any whole number value is an integer literal. Examples are
1, 2, 3, and 42. These are all decimal values, meaning they
are describing a base 10 number.
• There are two other bases which can be used in integer
literals, octal (base eight) and hexadecimal (base 16).
• Octal values are denoted in Java by a leading zero. Ex: 05
• Normal decimal numbers cannot have a leading zero.
• Hexadecimal constant with a leading zero-x, (0x or 0X). Ex:
0x5
• The range of a hexadecimal digit is 0 to 15, so A through F
(or a through f ).
• For binary, leading 0b or 0B
• Ex: 0b1000001; (‘A’)
public class Test
{
public static void main(String args[])
{
char c = 0b1000001; // Binary
[Link](c);

c=0101; //Octal
[Link](c);

c=0x41; //Hexadecimal
[Link](c);
}
}
Floating-Point Literals
• Floating-point numbers represent decimal values with
a fractional component.
• They can be expressed in either standard or scientific
notation.
• Standard notation consists of a whole number
component followed by a decimal point followed by a
fractional component (ex, 2.0, 3.14159, and 0.6667).
• Scientific notation uses a standard-notation, floating-
point number plus a suffix that specifies a power of 10
by which the number is to be multiplied.
• The exponent is indicated by an E or e followed by a
decimal number, which can be positive or negative.
• Examples include 6.022E23, 314159E–05,and 2e+100.
Floating-Point Literals
• Floating-point literals in Java default to double
precision.
• To specify a float literal, you must append an F or f
to the constant.
• Ex: 3.4f
• explicitly specify a double literal by appending a D
or d.
• Ex: 2.5d
Boolean Literals
• Boolean literals are simple.
• There are only two logical values that a boolean
value can have, true and false.
• The values of true and false do not convert into
any numerical representation.
• The true literal in Java does not equal 1, nor does
the false literal equal 0.
Character Literals
• Characters in Java are indices into the Unicode
character set.
• They are 16-bit values that can be converted into
integers and manipulated with the integer
operators, such as the addition and subtraction
operators.
• A literal character is represented inside a pair of
single quotes.
• Ex: ‘a’, ‘z’, and ‘@’.
• For characters that are impossible to enter
directly, escape sequences, ‘\’’ for the single-quote
character and ‘\n’ for the newline character.
Character Literals
• There is also a mechanism for directly entering the
value of a character in octal or hexadecimal.
• For octal notation, use the backslash followed by
the three-digit number.
• For example, ‘\141’ is the letter ‘a’.
• For hexadecimal, enter a backslash-u (\u), then
exactly four hexadecimal digits.
• For example, ‘\u0061’ is the ISO-Latin-1 ‘a’
because the top byte is zero. ‘\ua432’ is a
Japanese Katakana character.
TABLE 3-1 Character Escape Sequences
public class EscapeExample
{
public static void main(String args[])
{
String str = new String ("My name is \'abcd\'");
[Link](str);

str = "My Lab is \"JAVA LAB\"";


[Link](str);

str = "My work files are in D:\\Work Projects\\java";


[Link](str);

str = "First Line \nSecond Line";


[Link](str);

str = "Ravindra\b\b" + "College";


[Link](str);

str = "Ravindra \rCollege";


[Link](str);

str = "Ravindra \fCollege";


[Link](str);
}
}
String Literals
• String literals in Java are specified by enclosing a
sequence of characters between a pair of double
quotes.
• One important thing to note about Java strings is
that they must begin and end on the same line.
• There is no line-continuation escape sequence as
there is in some other languages.
• Ex: “Hello World”
“two\nlines”
“\”This is in quotes\”“
Variables
• The variable is the basic unit of storage in a Java program.
• A variable is defined by the combination of an identifier, a
type, and an optional initializer.
• In addition, all variables have a scope, which defines their
visibility, and a lifetime.
Declaring a Variable
• In Java, all variables must be declared before they can be
used.
syntax
type identifier [ = value][, identifier [= value] ...] ;
• The type is one of Java’s atomic types, or the name of a
class or interface.
• The identifier is the name of the variable.
• Initialize the variable by specifying an equal sign and a value
Variables
• To declare more than one variable of the specified
type, use a comma separated list.

int a, b, c; // declares three ints, a, b, and c.


int d = 3, e, f = 5; // declares three more ints, initializing
// d and f.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation of pi.
char x = 'x'; // the variable x has the value 'x'.
Dynamic Initialization
• Java allows variables to be initialized dynamically, using
any expression valid at the time the variable is declared.

// Demonstrate dynamic initialization.


class DynInit {
public static void main(String args[]) {
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = [Link](a * a + b * b);
[Link]("Hypotenuse is " + c);
}
}
The Scope and Lifetime of Variables
• A block defines a scope.
• A scope determines what objects are visible to other parts of
your program.
• It also determines the lifetime of those objects.
• In Java, the two major scopes are
– those defined by a class and
– those defined by a method. The scope defined by a method begins
with its opening curly brace.
• As a general rule, variables declared inside a scope are not
visible (that is, accessible) to code that is defined outside that
scope.
• Thus, when you declare a variable within a scope, you are
localizing that variable and protecting it from unauthorized
access and/or modification.
• Scopes can be nested.
• Objects declared within the inner scope will not be visible
outside it.
// Demonstrate block scope.
class Scope {
public static void main(String args[]) {
int x; // known to all code within main
x = 10;
if(x == 10) { // start new scope
int y = 20; // known only to this block
// x and y both known here.
[Link]("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
[Link]("x is " + x);
}
}
// Demonstrate lifetime of a variable.
class LifeTime {
public static void main(String args[]) {
int x;
for(x = 0; x < 3; x++) {
int y = -1; // y is initialized each time block is entered
[Link]("y is: " + y); // this always prints -1
y = 100;
[Link]("y is now: " + y);
}
}
}
Output:
y is: -1 // This program will not compile
y is now: 100 class ScopeErr {
y is: -1 public static void main(String args[]) {
y is now: 100 int b = 1;
y is: -1 { // creates a new scope
y is now: 100 int b = 2; // Compile-time error – b already defined!
}
}
}
Type Conversion and Casting
• If the two types are compatible, then Java will perform the
conversion automatically.
• Ex: assign an int value to a long variable
• no automatic conversion defined from double to byte.
• Conversion between incompatible types, use a cast, which
performs an explicit conversion between incompatible
types.
Java’s Automatic Conversions
• When one type of data is assigned to another type
of variable, an automatic type conversion will take
place if the following two conditions are met:
– The two types are compatible.
– The destination type is larger than the source type.
• Java also performs an automatic type conversion
when storing a literal integer constant into
variables of type byte, short, long, or char.
Casting Incompatible Types
• To create a conversion between two incompatible types,
you must use a cast.
• A cast is simply an explicit type conversion.
Syntax:
(target-type) value
• Assign an int value to a byte variable, byte is smaller than an
int, This kind of conversion is sometimes called a narrowing
conversion
• A different type of conversion will occur when a floating-
point value is assigned to an integer type: truncation.
// Demonstrate casts.
class Conversion {
public static void main(String args[]) {
byte b;
int i = 257;
double d = 323.142;
[Link]("\nConversion of int to byte.");
b = (byte) i;
[Link]("i and b " + i + " " + b);
[Link]("\nConversion of double to int.");
i = (int) d;
[Link]("d and i " + d + " " + i);
[Link]("\nConversion of double to byte.");
b = (byte) d;
[Link]("d and b " + d + " " + b);
} OUTPUT:
} Conversion of int to byte.
i and b 257 1
Conversion of double to int.
d and i 323.142 323
Conversion of double to byte.
d and b 323.142 67
Automatic Type Promotion in
Expressions
• In an expression, the precision required of an
intermediate value will sometimes exceed the range
of either operand.
byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b / c;
• The result of the intermediate term a * b easily
exceeds the range of either of its byte operands. To
handle this kind of problem, Java automatically
promotes each byte, short, or char operand to int
when evaluating an expression.
Automatic Type Promotion in
Expressions
byte b = 50;
b = b * 2; // Error! Cannot assign an int to a byte!
• The operands were automatically promoted to int
when the expression was evaluated, the result has
also been promoted to int.
byte b = 50;
b = (byte)(b * 2);
• which yields the correct value of 100.
The Type Promotion Rules
• Java defines several type promotion rules that
apply to expressions. They are as follows:
• First, all byte, short, and char values are promoted
to int.
• Then, if one operand is a long, the whole
expression is promoted to long.
• If one operand is a float, the entire expression is
promoted to float.
• If any of the operands is double, the result is
double.
class Promote {
public static void main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
[Link]((f * b) + " + " + (i / c) + " - " + (d * s));
[Link]("result = " + result);
}
}
OUTPUT:
238.14 + 515 - 126.3616
result = 626.7784146484375
Scanner class
• The Scanner class in Java is used for taking
input from the user.
• The Scanner class can take input of all the
data types.
• Scanner splits the input after every
whitespace.
• This class is present in [Link]
package.
Scanner class
• There are two constructors of the Scanner class
that are used. One is the InputStream object and
other takes a FileReader object.
• Syntax:
Scanner in = new Scanner([Link]); //InputStream
Scanner inFile = new Scanner(new FileReader(“File_Object”));
• If file is not found “FileNotFoundException” is
thrown.
Scanner class Methods
• public String nextLine() :-Moves the scanner to the next
line and returns the skipped input.
• public String next() :- Returns the token before delimiter
• public byte nextByte() :- Scans next token as byte value
• public short nextShort() :- Scans next token as short value
• public int nextInt() :- Scans next token as integer value
• public long nextLong() :- Scans next token as long value
• public float nextFloat() :- Scans next token as float value
• public double nextDouble() :- Scans next token as double
value
• void close :- Scanner is closed
Arrays
• An array is a group of like-typed variables that are
referred to by a common name.
• Arrays of any type can be created and may have one
or more dimensions.
• A specific element in an array is accessed by its index.
• Arrays offer a convenient means of grouping related
information.
• If you are familiar with C/C++, be careful. Arrays in
Java work differently than they do in those languages.
• Normally, an array is a collection of similar type of
elements which have a contiguous memory location.
One-Dimensional Arrays
• A one-dimensional array is, essentially, a list
of like-typed variables.
type var-name[ ]; (or)
dataType[] arr; (or)
dataType []arr;
• Here, type declares the base type of the
array
int month_days[];
myarray = new int[10];
One-Dimensional Arrays
int month_days[];
• This declaration establishes the fact that
month_days is an array variable, no array actually
exists. In fact, the value of month_days is set to
null, which represents an array with no value.
• To link month_days with an actual, physical array
of integers, you must allocate one using new and
assign it to month_days.
• new is a special operator that allocates memory.
array-var = new type[size];
month_days = new int[12];
int month_days[];
month_days = new int[12];
• After this statement executes, month_days will
refer to an array of 12 integers.
• Further, all elements in the array will be initialized
to zero.
• Obtaining an array is a two-step process.
– First, you must declare a variable of the desired array
type.
– Second, you must allocate the memory that will hold
the array, using new, and assign it to the array variable.
• Thus, in Java all arrays are dynamically allocated.
• Once you have allocated an array, you can access a
specific element in the array by specifying its index
within square brackets.
• All array indexes start at zero.
month_days[1] = 28;
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation

a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;

//traversing array
for(int i=0;i<[Link];i++) //length is the of array
[Link](a[i]);
}
}
• Arrays can be initialized when they are declared.
• The process is much the same as that used to
initialize the simple types.
• An array initializer is a list of comma-separated
expressions surrounded by curly braces. The
commas separate the values of the array
elements.
• The array will automatically be created large
enough to hold the number of elements you
specify in the array initializer.
• There is no need to use new.
int a[]={1,2,3,4,5};
For-each Loop for Java Array
• We can also print the Java array using for-each
loop.
• The Java for-each loop prints the array elements
one by one.
• It holds an array element in a variable, then
executes the body of the loop.
for(data_type variable : array){
//body of the loop
}
public class PrintArray {
public static void main(String [] args){
String[] array = { "hi", "hello", "java"};

for(String str : array) {


[Link](str);
}
}
}
//Print month name along with no of days
class MonthArray {
public static void main(String args[]) {
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
String month_names[] = {"Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

for(int i=0;i<12;i++)
{
[Link](month_names[i] + " is having " + month_days[i] + " days.");
}
}
}
• Print array elements
• Print array elements in reverse order
• Print odd elements
• Merge two arrays into third array
• Copy even elements to even array and odd
elements to odd array from the original array.
• Sort array elements
Multidimensional Arrays
• In Java, multidimensional arrays are actually arrays of
arrays.
• To declare a multidimensional array variable, specify
each additional index using another set of square
brackets.
int twoD[][] = new int[4][5];
• This allocates a 4 by 5 array and assigns it to twoD.
Syntax:
dataType[][] arrayRefVar; (or)
dataType [][]arrayRefVar; (or)
dataType arrayRefVar[][]; (or)
dataType []arrayRefVar[];
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
[Link](twoD[i][j] + " ");
[Link]();
}
}
}
Jagged Array in Java
• Creating odd number of columns in a 2D array, it
is known as a jagged array.
• In other words, it is an array of arrays with
different number of columns.
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) { This program generates the following output:
int twoD[][] = new int[4][]; 0
twoD[0] = new int[1]; 12
345
twoD[1] = new int[2];
6789
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<i+1; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<i+1; j++)
[Link](twoD[i][j] + " ");
[Link]();
}
}
}
• It is possible to initialize multidimensional arrays.
To do so, simply enclose each dimension’s
initializer within its own set of curly braces.
int arr[][]={
{1,2,3},
{2,4,5},
{4,4,5}
};
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
[Link](arr[i][j]+" ");
}
[Link]();
}
}} Output:
123
245
445
// Demonstrate a three-dimensional array.
class ThreeDMatrix {
public static void main(String args[]) { output:
int threeD[][][] = new int[3][4][5]; 00000
int i, j, k; 00000
for(i=0; i<3; i++) 00000
for(j=0; j<4; j++)
00000
for(k=0; k<5; k++)
threeD[i][j][k] = i * j * k;
00000
for(i=0; i<3; i++) { 01234
for(j=0; j<4; j++) { 02468
for(k=0; k<5; k++)
0 3 6 9 12
[Link](threeD[i][j][k] + " ");
[Link]();
} 00000
[Link](); 02468
} •3 layers
•Each layer has 4 rows 0 4 8 12 16
}
•Each row has 5 columns 0 6 12 18 24
} So total elements = 3 × 4 × 5 = 60
elements
Alternative Array Declaration Syntax
• There is a second form used to declare an array:
type[ ] var-name;
int a1[] = new int[3];
int[] a2 = new int[3];

char twod1[][] = new char[3][4];


char[][] twod2 = new char[3][4];

int[] nums, nums2, nums3; // create three arrays


String
• Java’s string type, called String, is not a simple type.
Nor is it simply an array of characters. Rather, String
defines an object.
• The String type is used to declare string variables.
• Also declare arrays of strings.
• A quoted string constant can be assigned to a String
variable.
• A variable of type String can be assigned to another
variable of type String.
String str = "this is a test";
[Link](str);
class StrDemo {
public static void main(String args[]) {
String str1 = new String();
str1 = "Hello World";
[Link](str1);

String str2 = "Bye Bye";


[Link](str1+str2);
}
}
Pointers
• Java does not support or allow pointers.
• Java cannot allow pointers, because doing so would
allow Java programs to breach the firewall between
the Java execution environment and the host
computer.
• (Remember, a pointer can be given any address in
memory—even addresses that might be outside the
Java run-time system.)
• Java is designed in such a way that as long as you stay
within the confines of the execution environment, you
will never need to use a pointer, nor would there be
any benefit in using one.
• Operators: Arithmetic Operators, The Bitwise
Operators, Relational Operators, Boolean Logic
operators, The assignment operator, The ?
Operator, Operator Precedence, Using
Parentheses.
Operators
• Java provides a rich operator environment. Most
of its operators can be divided into the following
four groups:
– arithmetic,
– bitwise,
– relational, and
– logical.
• Java also defines some additional operators that
handle certain special situations.
Arithmetic Operators
The operands of the
arithmetic operators
must be of a numeric
type.
You cannot use
them on boolean types,
but you can use them
on char types, since the
char type in Java is,
essentially, a subset of
int.
// Demonstrate the basic arithmetic operators.
class BasicMath {
public static void main(String args[]) {

// arithmetic using integers


[Link]("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3; // arithmetic using doubles
int c = b / 4; [Link]("\nFloating Point
int d = c - a; Arithmetic");
int e = -d; double da = 1 + 1;
[Link]("a = " + a); double db = da * 3;
[Link]("b = " + b); double dc = db / 4;
[Link]("c = " + c); double dd = dc - a;
[Link]("d = " + d); double de = -dd;
[Link]("e = " + e); [Link]("da = " + da);
[Link]("db = " + db);
[Link]("dc = " + dc);
[Link]("dd = " + dd);
[Link]("de = " + de);
}
}
Integer Arithmetic
a=2
b=6
c=1
d = -1
e=1
Floating Point Arithmetic
da = 2.0
db = 6.
dc = 1.5
dd = -0.5
de = 0.5
• The Modulus Operator (%)
• Arithmetic Compound Assignment Operators (+=)
• Increment and Decrement (++, --)

public class IncOp


{
public static void main(String args[])
{
int x=1;
int r = --x + x++ + ++x + x--;
[Link](x + " " + r);
}
}
//Guess the output
public class Test {
public static void main(String[] args)
{
int a = 10;
int b = ++(++a);
[Link](b);
}
}
//final variable
public class Test {
public static void main(String[] args)
{
final int a = 10;
int b = ++a;
[Link](b);
}
}
// Can not be applied boolean data type
public class Test {
public static void main(String[] args)
{
boolean b = false;
b++;
[Link](b);
}
}
// Demonstrate the % operator.
class Modulus {
public static void main(String args[]) {
int x = 42;
double y = 42.25;
[Link]("x mod 10 = " + x % 10);
[Link]("y mod 10 = " + y % 10);
}
}
output:
x mod 10 = 2
// Demonstrate several assignment operators.
class OpEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a += 5;
b *= 4;
c += a * b;
c %= 6;
[Link]("a = " + a);
[Link]("b = " + b);
[Link]("c = " + c);
}
}
The Bitwise Operators
• Java defines several bitwise operators that can be applied to
the integer types, long, int, short, char, and byte.
• These operators act upon the individual bits of their
operands.
The Bitwise Operators
• The byte value for 42 in binary is 00101010, where each position
represents a power of two, starting with 20 at the rightmost bit.
• All of the integer types (except char) are signed integers. This
means that they can represent negative values as well as positive
ones.
• Java uses an encoding known as two’s complement, which means
that negative numbers are represented by inverting (changing 1’s
to 0’s and vice versa) all of the bits in a value, then adding 1 to the
result.
• Example, –42 is represented by inverting all of the bits in 42, or
00101010, which yields 11010101, then
adding 1, which results in 11010110, or –42.
The Bitwise Logical Operators
● The bitwise logical operators are &, |, ^, and ~.
Bitwise OR (|) –
This operator is binary operator, denoted by ‘|’. It returns bit by bit OR of input values,
i.e, if either of the bits is 1, it gives 1, else it gives 0.
For example,
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise OR Operation of 5 and 7


0101
| 0111

0111 = 7 (In decimal)

Bitwise AND (&) –


This operator is binary operator, denoted by ‘&’. It returns bit by bit AND of input values, i.e,
if both bits are 1, it gives 1, else it gives 0.
For example,
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise AND Operation of 5 and 7
0101
& 0111

0101 = 5 (In decimal)


Bitwise XOR (^) –
This operator is binary operator, denoted by ‘^’. It returns bit by bit XOR of input
values, i.e, if corresponding bits are different, it gives 1, else it gives 0.
For example,
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise XOR Operation of 5 and 7


0101
^
0111

0010 = 2 (In decimal)

Bitwise Complement (~) –


Bitwise Complement (~) is a unary operator that inverts all bits of a number.
In Java, integers are stored in 32-bit two’s complement form.
For example,
a = 5 = 00000000 00000000 00000000 00000101
Bitwise Compliment Operation of 5

Therefore, ~5 results in -6
public class Bitoperators {
public static void main(String[] args) {
int a = 5;
int b = 7;
[Link]("a&b = " + (a & b));
[Link]("a|b = " + (a | b));
[Link]("a^b = " + (a ^ b));
// bitwise not ~0101=1010
// will give 2's complement of 1010 = -6
[Link]("~a = " + ~a); Output :
a &= b; a&b = 5
[Link]("a= " + a); a|b = 7
} a^b = 2
~a = -6
} a= 5
Relational Operators

The outcome of these operations is a boolean value.


Boolean Logical Operators
Short-Circuit Logical Operators
• Java provides two interesting Boolean operators, These are secondary versions of
the Boolean AND and OR operators, and are known as short-circuit logical
operators.

• The OR operator results in true when A is true, no matter what B is. Similarly,
the AND operator results in false when A is false, no matter what B is.

• If you use the || and && forms, rather than the | and & forms of these operators,
Java will not bother to evaluate the right-hand operand when the outcome of the
expression can be determined by the left operand alone.

• This is very useful when the right-hand operand depends on the value of
the left one in order to function properly.
Short-Circuit Logical Operators

• if (denom != 0 && num / denom > 10)


• Since the short-circuit form of AND (&&) is used,
there is no risk of causing a run-time exception
when denom is zero.
• If this line of code were written using the single &
version of AND, both sides would be evaluated,
causing a run-time exception when denom is zero.
import [Link].*;
import [Link];
public class AndopEx
{
public static void main(String[] args)
{
int x, y;
int sum=0; ;
Scanner sobj = new Scanner([Link]);
[Link]("Enter x & y vals: ")
x = [Link]();
y = [Link]();
if(x>0 && ++y>0)
sum = x+y;

[Link]("x=" + x + " and y=" + y + "\nx+y=" + sum);


}
}
The Assignment Operator
var = expression;
int x, y, z;
x = y = z = 100; // set x, y, and z to 100
• This fragment sets the variables x, y, and z to 100 using a single
statement.
• This works because the = is an operator that yields the value of the
right-hand expression.
• Thus, the value of z = 100 is 100, which is then assigned to y,
which in turn is assigned to x.
• Using a “chain of assignment” is an easy way to set a group of
variables to a common value.
The ? Operator
• Java includes a special ternary (three-way) operator that can
replace certain types of if-then- else statements. This operator is the
?.
expression1 ? expression2 : expression3

• Here, expression1 can be any expression that evaluates to a boolean


value. If expression1 is true, then expression2 is evaluated;
otherwise, expression3 is evaluated. The result of the ? operation is
that of the expression evaluated. Both expression2 and expression3
are required to return the same type, which can’t be void.
k = i < 0 ? -i : i; // get absolute value of i
public class TernaryEx
{
public static void main(String[] args)
{
int A = 10;
int B = 20;
String result = A> B ? "A is greater" : "B is greater";
[Link](result);
}
}

For three variables:


String result = a > b ? a > c ? "a is greatest" : "c is greatest" : b > c ? "b is greatest" : "c is greatest“;
Shift Operators
• These operators are used to shift the bits of a number left or right thereby
multiplying or dividing the number by two respectively.

• They can be used when we have to multiply or divide a number by two.

• Syntax

number shift_op number_of_shift;

• Signed Right shift operator (>>)

• Unsigned Right shift operator (>>>)

• Left shift operator (<<)


public class Shiftoperators {
public static void main(String[] args)
{
int a = 5;
int b = -10;

// left shift operator


// 0000 0101<<2 =0001 0100 (20)
// similar to 5*(2^2)
[Link]("a<<2 = " + (a << 2));

// right shift operator


// 0000 0101 >> 2 =0000 0001 (1)
// similar to 5/(2^2)
[Link]("b>>2 = " + (b >> 2));

// unsigned right shift operator


[Link]("b>>>2 = " + (b >>> 2)); Output :
} a<<2 = 20
} b>>2 = -3
b>>>2 = 1073741821
Operator Precedence
• Java conditions
Java’s Selection Statements

• if
• if-else
• if-else-if ladder
• nested if
• switch
• Java’s program control statements can be put into
the following categories:
– Selection
• Selection statements allow your program to choose
different paths of execution based upon the outcome of an
expression or the state of a variable.
– Iteration
• Iteration statements enable program execution to repeat
one or more statements (that is, iteration statements form
loops).
– jump
• Jump statements allow your program to execute in a
nonlinear fashion.
Control Statements
• The if Statement : The Java if
statement tests the condition.
• It executes the if block if
condition is true.

if(condition) {
// statement;
}

if(num < 100)


[Link](“less than 100");
//Java Program to demonstrate the use of if statement.

public class IfExample {


public static void main(String[] args) {
//defining an 'age' variable
int age=20;

//checking the age


if(age>18){
[Link](“Eligible for voting");
}
}
}
if - else
• The Java if-else statement also tests the condition.
• It executes the if block if condition is true
otherwise else block is executed.

Syntax:
if(condition){
//code if condition is true
}
else{
//code if condition is false
}
//It is a program of odd and even number.

public class IfElseExample {


public static void main(String[] args) {
int number=13;

//Check if the number is divisible by 2 or not


if(number%2==0){
[Link]("even number");
}
else{
[Link]("odd number");
}
}
}
//A year is leap, if it is divisible by 4 and 400. But, not by 100.

public class LeapYearEx {


public static void main(String[] args) {
int year=2020;

if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0))


{
[Link]("LEAP YEAR");
}
else
{
[Link]("COMMON YEAR");
}
}
}
if-else-if ladder
• The if-else-if ladder statement executes one
condition from multiple statements.
Syntax:

if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...

else{
//code to be executed if all the conditions are false

}
//It is a program of grading system for fail, D grade, C grade, B grade, A grade and A+.

public class IfElseIfExample {


public static void main(String[] args) {
int marks=65;

if(marks<35){
[Link]("fail");
}
else if(marks>=35 && marks<60){
[Link]("D grade");
}
else if(marks>=60 && marks<70){
[Link]("C grade");
}
else if(marks>=70 && marks<80){
[Link]("B grade");
}
else if(marks>=80 && marks<90){
[Link]("A grade");
}else if(marks>=90 && marks<100){
[Link]("A+ grade");
}else{
[Link]("Invalid!");
}
}
}
//Find out given no is +ve or –ve or zero

public class PositiveNegativeExample {


public static void main(String[] args) {
int number=-13;

if(number>0){
[Link]("POSITIVE");
}else if(number<0){
[Link]("NEGATIVE");
}else{
[Link]("ZERO");
}
}
}
Nested if
• The nested if statement represents
the if block within another if block.
• Here, the inner if block condition
executes only when outer if block
condition is true.

Syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed
}
}
//Java Program to demonstrate the use of Nested If Statement.

public class JavaNestedIfExample {


public static void main(String[] args) {
int age=20;
int weight=80;
//applying condition on age and weight
if(age>=18){
if(weight>50){
[Link]("You are eligible to donate blood");
}
else{
[Link]("You are not eligible to donate blood");
}
}
else{
[Link]("Age must be greater than 18");
}
}
}
Switch Statement
• The Java switch statement executes one statement from multiple
conditions.
• It is like if-else-if ladder statement.
• The switch statement works with byte, short, int, long, enum
types, String and some wrapper types like Byte, Short, Int, and
Long.
• There can be one or N number of case values for a switch
expression.
• The case value must be of switch expression type only. The case
value must be literal or constant. It doesn't allow variables.
• The case values must be unique. In case of duplicate value, it
renders compile-time error.
• Each case statement can have a break statement which is
optional. When control reaches to the break statement, it jumps
the control after the switch expression. If a break statement is
not found, it executes the next case.
• The case value can have a default label which is optional.
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......

default:
code to be executed if all cases are
not matched;
}
public class SwitchExample {
public static void main(String[] args) {
int number=2;

//Switch expression
switch(number){
//Case statements
case 1: [Link](“ONE");
break;
case 2: [Link](“TWO");
break;
case 3: [Link](“THREE");
break;
//Default case statement
default:[Link]("Not in 1, 2 or 3");
}
}
}
case 5: monthString="5 - May";
//Name of the month break;
case 6: monthString="6 - June";
public class SwitchMonthExample { break;
public static void main(String[] args) { case 7: monthString="7 - July";
//Specifying month number break;
int month=7; case 8: monthString="8 - August";
String monthString=""; break;
case 9: monthString="9 - September";
//Switch statement break;
switch(month){ case 10: monthString="10 - October";
//case statements within the switch block break;
case 11: monthString="11 - November";
case 1: monthString="1 - January"; break;
break; case 12: monthString="12 - December";
case 2: monthString="2 - February"; break;
break;
case 3: monthString="3 - March"; default:[Link]("Invalid Month!");
break;
case 4: monthString="4 - April"; }
break; //Printing month of the given number
[Link](monthString);
}
}
switch w/o break
• It executes all statements after the first match, if a
break statement is not present.
//without break statement
public class SwitchExample2 {
public static void main(String[] args) {
int num=2;
//switch expression with int value
switch(num){
//switch cases without break statements
Output:
case 1: [Link](“1");
2
case 2: [Link](“2"); 3
case 3: [Link](“3"); Not in 1, 2 or 3
default:[Link]("Not in 1, 2 or 3");
}
}
}
// In a switch, break statements are optional.
class MissingBreak {
public static void main(String args[]) {
for(int i=0; i<12; i++)
switch(i) {
case 0:
case 1: OUTPUT:
case 2: i is less than 5
case 3: i is less than 5
case 4: i is less than 5
[Link]("i is less than 5"); i is less than 5
break; i is less than 5
case 5: i is less than 10
case 6: i is less than 10
case 7: i is less than 10
case 8: i is less than 10
case 9: i is less than 10
[Link]("i is less than 10"); i is 10 or more
break; i is 10 or more
default:
[Link]("i is 10 or more");
}
}
}
Nested Switch Statement
• We can use switch statement inside other switch
statement in Java.
• It is known as nested switch statement.
switch(expression1){
case value1:
//code to be executed;
break;
case value2:
switch(expression2){
case value21:
//code to be executed;
break; //optional
case value22:
break; //optional
default:
default code to be executed
}
break;
default:
code to be executed if all cases are not matched;
}
Example: -
You are searching for a department in a university and you’re asked to select a
school from a choice of three schools namely:
1. School of Arts
2. School of Business
3. School of Engineering
Having selected a school you are again provided with a list of departments that fall
under the department namely:
1. School of Arts
A. Department of finearts
B. Department of sketcharts
2. School of Business
A. Department of Commerce
B. Department of purchasing
3. School of Engineering
A. CSE
B. ECE
The initial choices for Computer Science, Business and Engineering schools would
be inside as a set of switch statements. Then various departments would then be
listed within inner switch statements beneath their respective schools.
import [Link].*;
import [Link];

class NestedExample
{
public static void main(String args[])
{
int a,b;
[Link]("[Link] of Arts\n");
[Link]("[Link] of Business\n");
[Link]("[Link] of Engineering\n");
[Link]("make your selection\n");
Scanner sobj = new Scanner([Link]);
a=[Link]();
switch (a)
{
case 1:
[Link]("You chose Arts\n" ); break;
case 2:
[Link]("You chose Business\n" ); break;
case 3:
[Link]("You chose Engineering\n" );
[Link]("[Link]\n" );
[Link]("[Link]\n" );
[Link]("make your selection\n"); b=[Link]();
switch(b)
{
case 1:
[Link]("You chose CSE\n" ); break;
case 2:
[Link]("You chose ECE" ); break;
defalut: [Link](“Invalid choice of department" );
}
break;
defalut: [Link](“Invalid choice of School" );

}
}
}
Iteration Statements
• Java’s iteration statements are for, while, and
do-while.
• These statements are called loops.
• A loop repeatedly executes the same set of
instructions until a termination condition is
met.
while
• The while loop is Java’s most fundamental loop
statement. It repeats a statement or block while its
controlling expression is true.
while(condition) {
// body of loop
}
• The condition can be any Boolean expression. The
body of the loop will be executed as long as the
conditional expression is true.
• When condition becomes false, control passes to the
next line of code immediately following the loop.
• The curly braces are unnecessary if only a single
statement is being repeated.
// Demonstrate the while loop.
class While {
public static void main(String args[]) {
int n = 10;
while(n > 0) {
[Link](n);
n--;
} // The target of a loop can be empty.
} class NoBody {
} public static void main(String args[]) {
int i, j;
i = 100;
j = 200;
// find midpoint between i and j
while(++i < --j) ; // no body in this loop
[Link]("Midpoint is " + i);
}
}
do-while
• The do-while loop always executes its body at
least once, because its conditional expression is at
the bottom of the loop. Its general form is
do {
// body of loop
} while (condition);
• Each iteration of the do-while loop first executes
the body of the loop and then evaluates the
conditional expression. If this expression is true,
the loop will repeat. Otherwise, the loop
terminates.
// Demonstrate the do-while loop.
class DoWhile {
public static void main(String args[]) {
int n = 10;
do {
[Link](n);
n--;
} while(n > 0);
}
} do {
[Link]("Help on:");
[Link](" 1. if");
[Link](" 2. switch");
[Link](" 3. while");
[Link](" 4. do-while");
[Link]( ) is used to
[Link](" 5. for\n");
read characters from standard [Link]("Choose one:");
input. choice = (char) [Link]();
} while( choice < '1' || choice > '5');
for
• There are two forms of the for loop.
• The first is the traditional form of Java.
for(initialization; condition; iteration) {
// body
}
• The second is the new “for-each” form. The for-
each loop is essentially read-only.
for(type itr-var : collection) {
statement-block
}
for Loop Variations
for(int a=1, b=4; a<b; a++, b--)
{
}

for(int i=1; !done; i++) // Parts of the for loop can be empty.
class ForVar {
{ public static void main(String args[]) {
} int i;
boolean done = false;
i = 0;
for( ; !done; ) {
//infinite loop [Link]("i is " + i);
if(i == 10) done = true;
for( ; ; ) { i++;
// ... }
}
} }
// Use a for-each style for loop.
class ForEach {
public static void main(String args[]) {
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
// use for-each style for to display and sum the values
for(int x : nums) {
[Link]("Value is: " + x);
sum += x;
}
[Link]("Summation: " + sum);
}
}
// Use for-each style for on a two-dimensional array.
class ForEach3 {
public static void main(String args[]) {
int sum = 0;
int nums[][] = new int[3][5];
// give nums some values
for(int i = 0; i < 3; i++)
for(int j=0; j < 5; j++)
nums[i][j] = (i+1)*(j+1);

// use for-each for to display and sum the values


for(int x[] : nums) {
for(int y : x) {
[Link]("Value is: " + y);
sum += y;
}
}
[Link]("Summation: " + sum);
}
}
// Search an array using for-each style for.
class Search {
public static void main(String args[]) {
int nums[] = { 6, 8, 3, 7, 5, 6, 1, 4 };
int val = 5;
boolean found = false;

// use for-each style for to search nums for val


for(int x : nums) {
if(x == val) {
found = true;
break;
}
}
if(found)
[Link]("Value found!");
}
}
Nested Loops
• Java allows loops to be nested. That is, one loop
may be inside another.

// Loops may be nested.


class Nested {
public static void main(String args[]) {
int i, j;
for(i=0; i<10; i++) {
for(j=i; j<10; j++)
[Link](".");
[Link]();
}
}
}
• Try all types of printing pyramid programs.
Jump Statements
• Java supports three jump statements: break,
continue, and return.
• These statements transfer control to another part
of your program.
• Java supports one other way that you can change
your program’s flow of execution: through
exception handling. Exception handling provides a
structured method by which run-time errors can
be trapped and handled by your program. It is
supported by the keywords try, catch, throw,
throws, and finally.
Using break
• In Java, the break statement has three uses.
• First, it terminates a statement sequence in a switch
statement.
• Second, it can be used to exit a loop.
• Third, it can be used as a “civilized” form of goto.
break label;
// Using break to exit a loop.
class BreakLoop {
public static void main(String args[]) {
for(int i=0; i<100; i++) {
if(i == 10) break; // terminate loop if i is 10
[Link]("i: " + i);
}
[Link]("Loop complete.");
}
}
// Using break as a civilized form of goto.
class Break {
public static void main(String args[]) {
boolean t = true;
first: {
second: {
third: {
[Link]("Before the break.");
if(t)
break second; // break out of second block
[Link]("This won't execute");
}
[Link]("This won't execute");
}
[Link]("This is after second block.");
}
}}
OUTPUT
Before the break.
This is after second block.
class BreakErr {
public static void main(String args[]) {
one: for(int i=0; i<3; i++) {
[Link]("Pass " + i + ": ");
}
for(int j=0; j<100; j++) {
if(j == 10) break one;
[Link](j + " ");
}
}
}
Since the loop labeled one does not enclose the break
statement, it
is not possible to transfer control out of that block.
Using continue
• Sometimes it is useful to force an early iteration of a
loop. That is, you might want to continue running the
loop but stop processing the remainder of the code in
its body for this particular iteration.
• In while and do-while loops, a continue statement
causes control to be transferred directly to the
conditional expression that controls the loop.
• In a for loop, control goes first to the iteration portion
of the for statement and then to the conditional
expression.
• For all three loops, any intermediate code is bypassed.
• As with the break statement, continue may specify a
label to describe which enclosing loop to continue.
class ContinueExample {
public static void main(String args[]) {

for(int i = 1; i <= 5; i++) {

if(i == 3)
continue;
[Link](i);
}
}
}
// Using continue with a label.
class ContinueLabel {
public static void main(String args[]) {
outer: for (int i=0; i<10; i++) {
for(int j=0; j<10; j++) {
if(j > i) {
[Link]();
continue outer;
}
[Link](" " + (i * j));
}
}
[Link]();
}
}
return
• The return statement is used to explicitly
return from a method.
• That is, it causes program control to transfer
back to the caller of the method.
• The return statement immediately terminates
the method in which it is executed.
// Demonstrate return.
class Return {
public static void main(String args[]) {
boolean t = true;
[Link]("Before the return.");
if(t)
return; // return to caller
[Link]("This won't execute.");
}
}
class Search {
public static void main(String args[]) {

int nums[] = {6, 8, 3, 7, 5, 6, 1, 4};


int val = 5;
boolean found = false;
int pos = 0; // to store position

// use for-each style for to search nums for val


for(int x : nums) {
if(x == val) {
found = true;
break;
}
pos++; // increment position
}

if(found)
[Link]("Value found at position: " + pos);
else
[Link]("Value not found!");
}
}

You might also like