Chapter 2 Problem Solving Part 1 Classes, Iterations and Arrays, String, Enum
Chapter 2 Problem Solving Part 1 Classes, Iterations and Arrays, String, Enum
Declaration of Classes
A class is declared by the use of the “class” keyword. Class body is enclosed between curly
braces { and }. The data or variables defined within the class are called instance variables. The
code is contained within methods. Collectively, the methods and variables defined in a class are
called members of the class.
Local variables: Variables defined inside methods, constructors or blocks are called local
variables. The variable will be declared and initialized within the method and the variable will be
destroyed when the method has completed.
Instance variables: Instance variables are variables within a class but outside any method.
These variables are instantiated when the class is loaded. Instance variables can be accessed
from inside any method, constructor or blocks of that particular class.
Class variables: Class variables are variables declared with in a class, outside any method, with
the static keyword.
Declaration of Instance Variables
Variables defined within a class are called instance variables because each instance of the class
(ie, each object of the class) contains its own copy of these variables. Thus, the data for one
object is seperate and unique from the data of another. Instance variables can be declared public
or private or default (no modifier). When we do not want our variables value to be changed
outside our class we should declare them private. Public variables can be accessed and
changed from outside the class. We will have more information about this on OOP tutorial.
Declaration of Methods
A method is a program module that contains a series of statements that carry out a task. To
execute a method, you invoke or call it from another method; the calling method makes a
method call, which invokes the called method. Any class can contain an unlimited number of
methods, and each method can be called an unlimited number of times.
The syntax for method declaration is as given below. Java main method is given as an example.
Objects or Instances
To understand this in a better way, let's take an example of Mobile as an object. Mobile has
characteristics like model, manufacturer, cost, operating system etc. So if we create “Samsung”
mobile object and “iPhone” mobile object, we can distinguish them from their characteristics. The
values of the attributes of an object are also referred to as the object's state.
Objects need not be physical objects. It can be any entity. It can be account,an
organization etc.
Consider an object Car. What we know about a car defines its attributes. What a car can do
defines its behaviours/methods.
Always operations can alter the state of the attributes.
Consider the above start operation for a Car. When the car is started ,it is affecting the state of
the engine.
Creating an Object:
As mentioned previously, a class provides the blueprints for objects. So basically an object is
created from a class. In Java, the new key word is used to create new objects.
Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the
new object.
We are creating three objects truck, bike and a car from Vehicle class.
Accessing Instance Variables and Methods using Objects:
/* call a variable as follows */
ObjectReference.variableName;
Java constructors are special methods which are used to initialize objects. Constructor method
has the same name as that of class, they are called or invoked when an object of class is created
and can't be called explicitly. They are designed to perform initializing actions such as initializing
the data fields or objects.
A java constructor has the same name as the name of the class to which it belongs.
Constructor’s syntax does not include a return type, since constructors never return a value.
Constructors can be classified into two types, default constructors and parametrized
constructors.
If you don't define a constructor, then the compiler creates a default constructor. Default
constructors do not contain any parameters. Default constructors are created only if there are no
constructors defined by us. Java provides a default constructor which takes no arguments, when
no explicit constructors are provided. Parametrized constructors are required to pass parameters
on creation of objects.
Access Specifiers
Each object has members (members can be variables or methods) which can be declared to
have specific access.
Access Modifier - private:
Methods, Variables and Constructors that are declared private can only be accessed
within the declared class itself. Private access modifier is the most restrictive access level.
Variables that are declared private can be accessed outside the class if public getter
methods are present in the class. Using the private modifier is the main way that an object
encapsulates itself and hide data from the outside world.
If there is ambiguity between the instance variable and parameter, this keyword resolves the
problem of ambiguity.
Let's understand the problem if we don't use this keyword by the example given below:
In the above example, parameter (formal arguments) and instance variables are same that is
why we are using this keyword to distinguish between local variable and instance variable.
There would only be one copy of each class variable per class, regardless of how many objects
are created from it.Static variables are stored in static memory.
Static variables are created when the program starts and destroyed when the program stops.
Visibility is similar to instance variables. However, most static variables are declared public since
they must be available for users of the class.
Default values are same as instance variables. For numbers, the default value is 0; for Booleans,
it is false; and for object references, it is null. Values can be assigned during the declaration or
within the constructor.
Static variables can be accessed by calling with the class name . ClassName.VariableName.
Consider the below class Vehicle, here manufacturteName is declared as static and
getManufactureName() method also declared as static.
OUTPUT is:
A package is a namespace that organizes a set of related classes and interfaces, providing
access protection and name space management. Conceptually packages can be thought of as
similar to different folders on the computer. Packages are used in Java to prevent naming
conflicts, to control access, and to make searching/locating and usage of classes easier.
Java platform provides a very large library of classes and interfaces organized into different
packages known as “Application Programming Interface” or API. Some of the packages in Java
API Library are:
java.io - classes for input, output functions are bundled in this package
Because software written in Java can be composed of hundreds of individual classes,
programmers can define their own packages to group related classes together. It is a good
practice to organize by grouping related classes and interfaces into packages so that a
programmer can easily locate and find the classes he needs.
Since the package creates a new namespace there won't be any class name conflicts with other
class names in other packages.
Eg: Both java.awt and java.util packages have a class called “List”. There is no naming conflict
because the two “List” classes are part of two different packages.
Using packages, it is easier to provide access control and it is also easier to locate the related
classes.
Eg: Consider a scenario where a bank has many employees. New employees are added, some
may resign from bank, so those employees need to be removed from bank. Many customers
come to the bank to get services. Customers open account, deposit money to account, withdraw
money from account etc.
Here we have different objects and so different classes need to be created. If it is going to be a
large application, it will be difficult to manage all the files without using packages. Also if we want
to set different visibility, like Customer should not see the details of Employee, packages can be
used to organize and set visibility using access modifiers.
The package statement should be the first line in the source file. There can be only one package
statement in each source file, and it applies to all types in the file.
Step1: Create a package in eclipse and provide a name for the package as shown below.
Package name can be a one word name or multiple words separated by a dot.
Eg: com.tcs.employees (or) employee
Step 2: Create classe(s) inside the employee package. The java class file should have package
statement as the first line. In the screenshot below you can see the employee package.
The classes Employee.java and TestEmployee.java are created under this package. You can
see that the first line in Employee.java is the package statement.
Import Statement
A package is to group related classes or other type of files together. The classes and other types
under a package are called as package members. So as we saw above, we have grouped all
files into different packages.
If a class wants to access/use another class/member from the same package, the class need not
be imported. Classes/members within the same package can access each other without
importing them.
But in the scenario where the classes belonging to different packages need to access each
other, the class belonging to the other package must be imported into the class that wants to use
it.
The classes are imported using import statements. Import keyword is used to import the classes
from different packages. Import keyword is followed by the fully qualified name of the class which
includes the package name under which the class is present.
Eg: import java.util.Date ;
In the example below the Employee class is imported into the TestEmployee class.
You can see in the above example, the TestEmployee class is under the test package. You can
also see the import statement to import Employee in TestEmployee class. This is there because
TestEmployee is using Employee class, which is part of a different package employee. However
even after the Employee class is imported into TestEmployee there are still errors in
Testemployee class. This is because the Employee class is not visible to classes outside its
package. Earlier when the TestEmployee class was inside employee package, it knew the
Employee class. But when it was moved into a different package, and even after Employee
class was imported the Employee class and its constructor are not visible. This is because only
public members of a package/class are visible to classes from other packages. So specify public
access specifier to the class, constructor and method which need to be accessed from outside.
In the below screen, you can see the changes made to access specifiers of Employee class and
its constructor and methods. They are all declared with public access modifier.
To use a public package member from outside its package any one of the following can be used
a) Refer to the class/member by its fully qualified name
b) Import specific class/member in a package
c) Import all members/classes in a package
If a class in a package has same name as another class in another package and both packages
are imported then you must refer to each class by its qualified name to resolve any name
ambiguities.
More on Access Modifiers and Java Libraries
We have discussed about 2 access specifiers in the previous courses. Which are they?
Variables that are declared private can be accessed outside the class if public getter methods
are present in the class. Using the private modifier is the main way that an object encapsulates
itself and hide data from the outside world.
A variable or method declared without any access control modifier is available to any other class
in the same package. Such members can be considered as package private.
We have already seen such a scenario when we discussed about import statements.
Eg: The constructor method in the following Java class is in default access.
class Employee {
private String name;
private String empNo;
private int age;
private double basicPay;
// Constructor
Employee(String name, String empNo, int age) {
this.name = name;
this.empNo = empNo;
this.age = age;
}
}
Protected Access Modifier – protected
Protected is related to inheritance (a parent child relation). Protected members in a class can be
accessed only by its child classes from same/different package as well as by any other classes in
the same package.
The child class can be anywhere, in other package or within the same package of the parent
class.
The protected access modifier can be applied only to methods or fields, not to class. We can see
more on it when we go through inheritance.
Java Libraries
Java differs from most other languages in that the number of classes and interfaces in its
standard libraries is very large. Many common tasks have already been implemented by these
libraries.
They are generally of high quality and are widely used. Implementing something which already
exists in the libraries is probably wasted effort. Significant changes and additions to the standard
libraries occur in each major release of Java, and it pays to keep current.
The most widely used packages are java.lang and java.util. There are other packages used for
working with data such as java.sql, javax.sql, java.io etc.
For graphical applications, see the Swing classes (javax.swing, and so on).
You should go through JDK documentation just to get an idea of what is available. Later, when a
specific need arises, you will often know which packages might be helpful.
There are many scenarios in our everyday life, where we need to take some decisions based on
some criteria or condition. Decision making is one of the major part of Java also.
Conditional logic is involved when different operations are to be performed based on whether a
decision is true or not. Conditional expressions consists of conditions that results in a boolean
value and performs actions based on the boolean result. There are three types of decision
making statements in Java. They are:
1. if – This statement is the most basic of all control flow statements. “if” statement has a certain
condition expression. It executes a certain section of code only if that particular condition is true.
If the condition is false, control jumps to the end of the if statement.
if(condition)
{
/* statement(s) will execute if the condition is true.
For example, a=5
Checking condition -
if(a>3)
{
This code will be executed because 'a' is greater than
5.
}
*/
}
2. If ... else – This statement provides a secondary path of execution when the condition
becomes false. There can be nested if then else statement also.
if(condition)
{
/* statement(s) will execute if the condition is true.*/
}
else
{
/* statement(s) will execute if the boolean condition is
false */
}
In the above example, if a=5 and condition is to check if a>6 then it will go to else block.
Flow Diagram:
3. switch – A switch statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable being given as input is checked for each case.
This statement helps to select one out of the many choices based on an integer or String value.
The conditions will accept only integer, character or String. The syntax is different from if else
statements. Here we are comparing the input value with different cases. We can set a default
case for inputs which does not satisfy any case.
The syntax of the switch statement is as follows.
switch (expression) {
case value_1 :
statement(s);
break;
case value_2 :
statement(s);
break;
.
.
.
case value_n :
statement(s);
break;
default:
statement(s);
}
Failure to add a break statement after a case will not generate a compile error but may have
more serious consequences because the statements on the next case will be executed.
1. The variable used in a switch statement can only be a byte, short, int, char or String(It is
allowed from jdk 1.7 version.).
2. You can have any number of case statements within a switch. Each case is followed by the
value to be compared to and a colon.
3. The value for a case must be the same data type as the variable in the switch and it must be a
constant or a literal.
4. When the variable is found equal to a case, the statements following that case will execute
until a break statement is reached. When a break statement is reached, the switch terminates,
and the flow of control jumps to the next line following the switch statement. Not every case
needs to contain a break. If no break appears, the flow of control will fall through to subsequent
cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the
switch. The default case can be used for performing a task when none of the cases is true. No
break is needed in the default case.
Java supports another conditional operator that is known as the ternary operator "?:" and
basically is used for an if-then-else as shorthand
The "?:" operator evaluates an expression which may also be an operand and returns operand1
if the expression is true; otherwise returns operand2, if the expression is false. We can
understand this thing with the help of a diagram shown as:
If we analyze this diagram then we find that, operand1 is returned, if the expression is true;
otherwise operand2 is returned in case of false expression.
Lets have an example implementing some Logical operators:
Output of the Program:
value of x is 5
value of y is greater than the value of x
The returned value is 5
Thus we can conclude the ternary operator can be considered as the short hand of if then else
statement.
Conditional operators return a true or a false value based on the state of the variables i.e. the
operations using conditional operators are performed between the two boolean expressions.
In case of "&" operator,if both operands or expressions are true,the result is always true,
otherwise it is false if either left-hand or right-hand operand is false.
In case of "&&" operator, the result is also true, if both operands or expressions are true.
But this operator evaluates only the left-hand operand. It doesn't evaluate the right-hand operand
if the left-hand operand is false then it will not jump to the right-hand operand to evaluate it, and
will come out from that statement and read the next statement. That's why this mechanism is
known as short-circuiting. Consider the following statements where the second expression
returns false after evaluating only the left-hand operand.
But the "&" operator always evaluates both of its operands whether the first operand is true or
false. Consider the following statements where the second expression returns false after
evaluating the right-hand operand.
Likewise, the OR operator(|) is similar to the Conditional-OR operator (||) and returns true, if one
or another of its operand is true.
Lets use a Truth Table to know the status of an output that works alike for"|" and "||" operators.
If we analyze the table then we find that, result is always false only if both operands or
expression are false. On the other hand, result is always true in rest of the other conditions.
In case of "||" the result is also true, if one of the both operands is true. Otherwise it evaluates to
falseif both operands are false. But this operator conditionally evaluates the right-hand operand
only if the left-hand operands is false. Like the Conditional-AND operator, this mechanism is also
known as short-circuiting. Consider the following statements where the first expression returns
true after evaluating the right-hand operand.
We have seen how to declare variable in the previous section. int x; Here x is a variable of type
int, so x will be capable of storing an integer value. At any point of time a variable can hold only a
single value.
int age=20;
int score=70;
Say, I need to store scores of last 5 matches. What kind of data type do I take? May be I will
take 5 variables of type int.
int scoreMatch1;
int scoreMatch2;
int scoreMatch3;
int scoreMatch4;
int scoreMatch5;
Similarly, I need to record no. of kms I drove each day in last 5 days.
int kmsDay1;
int kmsDay2;
int kmsDay3;
int kmsDay4;
int kmsDay5;
Similarly, I need to record the amount spent for each day in last 5 days.
double expenseDay1;
double expenseDay2;
double expenseDay3;
double expenseDay4;
double expenseDay5;
The above approach will not be efficient if the requirement(of recording data for a specific
period) changes from just last 5 days to last 30 days.
Array:
It is a single variable, capable of storing multiple values (of same data type). But an array
does not store the multiple values directly. It is a collection of more than one variable, of same
data type.
Declaring arrays:
The general syntax to declare an array is : data type variable name[]; The square brackets
indicate that the variable is an array of the data type declared.
Example:
Also, all the above arrays are just declared, and have not yet been initialized(not defined the
size).
Here scores is not just an int type variable. scores is an array of type int i.e. It is capable of
storing more than one int value.
int score = 70; Here the variable score holds a value 70.
Here the array scores do not hold the values directly. It is just a reference (pointing to) to the 5
variables score1 … score5. Hence arrays are called references or objects in Java.
If we do not initialize an int variable, its default value is 0. In case of array (as it does not
store value directly), if we do not initialize(do not say to which list of variables it refers to) it, the
default value is null. i.e. it does not point to any list of variables.
Similarly, expenses is not just a double type variable. expenses is an array of type double
i.e. it is capable of storing more than one double value.
Similarly, grades is not just a char type variable. grades is an array of type char i.e. it is
capable of storing more than one char value.
Once an array is declared, you can think of multiple variables being generated automatically in a
sequential order i.e. if an array double[] expense is declared, the variable that stores the first
value will be expense[0], the variable that stores the second value will be expense[1] and so on.
Note: While talking about arrays, always remember two key things. One, about the array
itself, whether it is initialized or not. Two, about the element inside the array – whether it is
initialized or not.
Arrays have a specific length(or size). The length of the array cannot be changed after it has
been initialized. The elements in the array are put at index of the array. The first element in the
array is at index 0, and the last element is at the index length-1.
To declare and initialize an array at the same time, refer to the below syntax:
Example 1:
{} is called an initializer block. The length (or size) of an array is equal to the no. of values
declared in the initializer block.
scores is an int array. Its size is 5. The value of first element is 55, value of second element
is 15, value of third element is 92, value of fourth element is 107 and value of fifth element is 67.
Example 2:
All the above examples, declare an array, initialize the array and also insert elements into
the array.
int scores[];
The above statement just declares an array. It has not been initialized yet i.e. scores is null.
Let us initialize the array.
int scores[] = new int[5];
The above statement now initializes the array to size 5. But no elements are put into array.
Hence the elements of the array will have their default value. As the array is of type int, all the 5
elements in the array have the value of 0.
You don't need to mention the length(or size) of an array in [] if you are using the initializer
block.
The above statement declares an array, initializes an array (of size 5) and also puts
elements into the array. The value of first element is 55, the value of second element is 15, the
value of third element is 92, the value of fourth element is 107 and the value of fifth element is
67.
Once the array is initialized, the length(or size) of an array can be known by the length
property. It is used after the dot operator on the array variable. Refer the syntax below:
array.length;
eg.
Step 1:
int scores[] = new int[5];
The above statement will declare an int array of length 5. The values in the array will be
initialized to the default values as per their data type.
Step 2:
The first value in the array is accessed by the code : scores[0]. To assign values to the first
element, use the assignment operator (=). The below statement assigns a value 55 to the first
element in the array. The remaining elements still have the default value.
scores[0] = 55;
To retrieve the value of the elements at index 0, use scores[0] which will return the value 55.
Similarly,
We can use a variable to store the value we retrieve from the array,
int x = scores[0]; as scores is an int array, scores[0] is going to return an int.
Consider the case of library where it has list of books as inventory. So library will have
attributes like library name, address. It will also have list of books as an attribute. We could
model the Book class and Library class as follow.
The library class has an array of Book, i.e. the array will have multiple objects of Book
representing the real books in the library.
Book[] listOfBooks;
The above statement just declares the array variable. It is not initialized so the listOfBooks
is still null. Let us initialize the array.
listOfBooks = new Book[3];
The above statement just initialized the array itself, the elements have not yet been
initialized. Let us initialize the elements of the array by assigning the book object value.
listOfBooks[0] = b1;
listOfBooks[1] = b2;
listOfBooks[2] = b3;
The above 3 statement, initializes all the 3 elements to the value of objects b1, b2 and b3
respectively.
We can use a variable to store the value we retrieve from the array.
Points to remember
You create an object of array either by using the initializer block {} or by using the new
operator.
Arrays have a constant length, i.e. the length cannot increase dynamically at run time.
The first index of an array is 0 and the last index is length-1. eg. if the array declared is of
size 4, the values are stored at index 0 to 3.
If the elements in an array are not assigned any value, the value will be the default value
depending on the type of array. eg. int[] will have value 0 at index where the value is not
initialized, String[] will have value null at index where the value is not initialized.
Let us take a scenario where a teacher has to perform task of giving grades to the students
according to their percentage. He/she checks the percentage of first student and enters the
grade, then checks the percentage of second student and enters the grade, same procedure for
the next student as well. Here, the teacher has to perform the same task again and again. Don't
you think this is a laborious and error prone work? Yes it is.
One of the things computers are often used for is the automation of repetitive tasks.
Repeating identical or similar tasks without making errors is something that computers do well
and humans do poorly. In this chapter, we shall study in detail how the repeated tasks can be
automated using Java programming.
Program:
Output:
12345678910
In the above program, we wrote the same line of code 10 times to obtain output. This is nothing
but code redundancy(repetition) which makes our program an inefficient one. To eliminate this
we use loops and iterations in Java.
Loop is a block of code executed repeatedly until some condition is satisfied and one pass
through(execution of) the loop is called iteration.
Here condition is a boolean expression. A boolean expression is an expression written
using conditional operators(<, >, ==, <=,>= etc) and the result of it will always be either true or
false.
1. 'while' loop
2. 'do while' loop
3. 'for' loop
While loop:
A while loop is a control structure that allows you to repeat a task certain number of times.
In simple words, using while loop we can execute a set of statements several times based on a
condition.
Syntax:
while(condition)
{
//Statements
}
Here, “while” is a keyword and “condition” is a boolean expression and as long as this
condition yields true, the statements within the braces({}) are executed. You can almost read a
while statement as if it were English- “while condition is true, continue executing the Statements”.
Let us now write the same program(print first 10 natural numbers) using while loop.
Program:
Output:
12345678910
In the above code snippet, we eliminated those 9 repeated lines, clubbed them into a single
line and obtained the output using while. Here n is also called counter variable and it is used to
iterate through the loop.
Example:
/*Program to automate the grade generation of five students based on their percentage using
while loop */
Output:
Roll no 1: Grade is B
Roll no 2: Grade is B
Roll no 3: Grade is C
Roll no 4: Grade is A
Roll no 5: Grade is A
Flow of the above program:
1. Initialize an integer array of percentage.
2. Initialize an integer variable rollNo=0; which is used to iterate through the array.
3. Since we need to generate grades for only those students whose percentage is given, we are
specifying the condition in while as rollNo<percentage.length. We have learned in the previous
topic that .length property gives the size of array which is 4 in the above program. rollNo points to
the index of the array. So before entering while loop, the value of rollNo is checked.
4. If the condition is satisfied(if rollNo<percentage.length) go to step 5 else go to step 6.
5. Now control is sent to the body of loop
• In the body of while loop, if-else conditions are specified to check the percentage of student
marks. Based on those conditions respective grade is printed.
• Increment rollNo.
• Go to Step 4.
6. End.
Syntax:
do
{
//statements
}while(condition);
Here, “while” and “do” are keywords and “condition” is a boolean expression. Each iteration
of the do-while loop first executes the statements and then evaluates the condition. If the result of
this condition is true, the loop will repeat. Otherwise, the loop terminates. Notice, there is
semicolon at the end of while(); in do... while loop.
Let us now write the same program(print first 10 natural numbers) using do... while loop.
Program:
Output:
12345678910
Now you may get a question as why “do.. while” when we already have “while” and when
both are giving the same output? We shall study about it in detail.
Fortunately, Java supplies a loop that does just that: the do-while. The do-while loop always
executes its body at least once, because its condition is at the bottom of the loop. Therefore,
while is called entry-called loop whereas do...while is called exit-controlled loop.
The difference is best illustrated using the programs below:
Do-while example:
Output:
do-while: 11
While example:
Output:(no output)
Example:
/*Program to generate the grade for five students of a class based on their percentage using do...
while loop */
Output:
Roll no 1: Grade is B
Roll no 2: Grade is B
Roll no 3: Grade is C
Roll no 4: Grade is A
Roll no 5: Grade is A
For loop:
A for loop is a repetition control structure that allows you to efficiently write a loop that needs
to execute a specific number of times. A for loop is useful when you know how many times a task
is to be repeated.
Syntax:
for(initialization; condition; increment/decrement)
{
//Statements
}
Here,the initialization step is executed first, and only once. This step allows you to declare
and initialize any loop control variables. Next, the condition is evaluated. If it is true, the
statements are executed, else control comes out of for loop and the next statement after the
loop is executed.
Program:
Output:
12345678910
4. If the result is true, execute the set of statements within the curly braces, and then go to
increment/decrement statement.
5. Go to step 2.
Example:
/*Program to generate the grades for five students based on their percentage using for loop */
Output:
Roll no 1: Grade is B
Roll no 2: Grade is B
Roll no 3: Grade is C
Roll no 4: Grade is A
Roll no 5: Grade is A
This is an enhanced for loop which mainly used for arrays. Advanced for loop is also called
'for each' loop.
Syntax:
for(declaration : expression)
{
//Statements
}
Declaration: The newly declared block variable, which is of a type compatible with the elements
of the array you are accessing. The variable will be available within the for block and its value
would be the same as the current array element.
Expression: This evaluates to the array you need to loop through. The expression can be an
array variable or method call that returns an array.
Let us take the same example of grade generation and see how it works in advanced for loop.
Example:
/*Program to generate the grades of 5 students based on their percentage using advanced for
loop */
Output:
Percentage is 50: Grade is B
Percentage is 46: Grade is B
Percentage is 32: Grade is C
Percentage is 98: Grade is A
Percentage is 75: Grade is A
In the above program, eachPercentage is a variable that is of the same data type as array of
percentage. In the previous program rollNo takes the index value whereas here eachPercentage
takes value at index. Therefore, initially the value of eachPercentage is percentage[0] which is 50
and in the second iteration eachPercentage will take the value percentage[1] which is 46 and so
on.
Nested loops:
We have seen the advantages of using various methods of iteration, or looping. Now let's
take a look at what happens when we combine looping procedures. The placing of one loop
inside the body of another loop is called nesting.
When you "nest" two loops, the outer loop takes control of the number of complete
repetitions of the inner loop. While all types of loops may be nested, the most commonly nested
loops are for loops.
Output:
12
123
1234
12345
In the above program, outer loop is used to determine the no.of digits to be
printed in a line whereas the inner loop is used to print the digits. We have used print() within the
body of inner loop because we want the digits to be displayed in the same line and after the inner
loop is executed, we go to the next line using println() statement.
A finite loop ends itself i.e control comes out of the loop at certain point of time. For example, the
for loop in the previous program that displays multiples of 3 is a finite loop because the loop exits
if n>10.
In other case,a loop becomes an infinite loop if a condition never becomes false. The for
loop is traditionally used for this purpose. Since none of the three expressions that form the for
loop are required, you can make an endless loop by leaving the conditional expression empty.
Output:
Tata Consultancy Services
Tata Consultancy Services
Tata Consultancy Services
.
.
.
.
This goes on printing until we forcefully stop the execution. The above program is an
example of implementing infinite loop using for loop. Similarly, you can implement an infinite loop
using while and do... while as follows.
} while(true);
The break keyword is used to stop the entire loop. Using break we can leave a loop even if the
condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end
before its natural end.
Syntax:
break;
Program:
Output:
x is 1
x is 2
Break the loop
Exit
In the above example if the value of x is 3 then the control enters into the loop and when
break statement is encountered, it comes out of the for loop and executes the first statement
after the for loop. The break statement can be used in terminating all three loops for, while and
do...while.
In a for loop, the continue keyword causes flow of control to immediately jump to the
increment/decrement statement.
In a while loop or do/while loop, flow of control immediately jumps to the condition.
Syntax:
continue;
Output:
x is 1
x is 2
Continue the loop
x is 4
x is 5
Exit
In the above example if the value of x is 3 then the control enters into the loop and when
continue statement is encountered, it jumps to the next iteration without executing the rest of the
statements within the loop.
Working of continue statement:
2./*Program to copy elements from one array to the other using for loop*/
Output:
Points to remember:
Iterations
Condition in the while,do... while and for should give result as false after several
iterations to terminate the loop.
A loop control variable used to count the iterations is called counter variable.
In for each loop, declared variable and array should be of same data type.
String str= "Hello";
And, when we create another object with same string, then a reference of the string literal
already present in string pool is returned.
String str2=str;
str2=str2.concat("world");
Java has provided a special mechanism for keeping the String literals - in a so-called
string common pool. If two string literals have the same contents, they will share the
same storage inside the common pool. This approach is adopted to conserve storage for
frequently-used strings. On the other hand, String objects created via the new operator
and constructor are kept in the heap. Each String object in the heap has its own storage
just like any other object. There is no sharing of storage in heap even if two String objects
have the same contents.
For Example:
String s1="Hello"; // String literal
String s2="Hello"; // String literal
String s3 = s1; // same reference
String s4 = new String("Hello"); // String object
String s5 = new String("Hello"); // String object
String is immutable:
Since string literals with the same contents share storage in the common pool, Java's
String is designed to be immutable. That is, once a String is constructed, its contents
cannot be modified. Otherwise, the other String references sharing the same storage
location will be affected by the change, which can be unpredictable and therefore is
undesirable.
Because String is immutable, it is not efficient to use String if you need to modify your
string frequently (that would create many new Strings occupying new storage areas).
For example,
// inefficient codes
String str = "Hello";
for (int i = 1; i < 1000; ++i) {
str = str + i;
Note: Immutable objects have a very compelling list of positive qualities, they are among
the simplest and most robust kinds of classes you can possibly build. When you create
immutable classes, entire categories of problems simply disappear.
Java also has its share of immutable classes which are primarily String class and
wrapper classes. In this post, we will understand the need of immutability for String class.
1) Security : The first and undeniably most important reason is security. Well, its not only
about your application, but even for JDK itself. Java class loading mechanism works on
class names passed as parameters, then these classes are searched in class path.
Imagine for a minute, Strings were mutable, then anybody could have injected its own
class-loading mechanism with very little effort and destroyed or hacked in any application
in a minute.
2) Performance : As already discussed Java uses string pool for achieving better
performance of Strings. But at the same time if string is not immutable, changing the
string with one reference will lead to the wrong value for the other references which could
lead to erroneous results. Hence indirectly immutability is required to achieve better
performance.
3) Thread safety: The word thread safe, means multiple threads trying to change the
properties of a single object, To make this happen with String(which is immutable) is
highly impossible. You can't change the properties of string object once it is created. If
you want to do any operations over String, it will create a new string. So strings are
thread safe(Immutable objects are safe when shared between multiple threads in mulch-
threaded applications).
String Functions:
The following methods are some of the most commonly used methods of String class:
a. charAt(int index) : It
a. returns the character at the specified index.
b. An index ranges from 0 to length() - 1.
c.The first character of the sequence is at index 0, the next at index 1, and so on, as for
array indexing.
Example: String str = "Strings are Immutable";
System.out.println(str.charAt(2));
Output: r
d. replace(char oldChar, char newChar): returns a new string resulting from replacing
all occurrences of oldChar in this string with newChar.
Example : String str = "Change me";
System.out.println(str.replace('m','M'));
Output: Change Me
e. substring(): method returns a part of the string. substring() method has two forms,
public String substring(int begin);
public String substring(int begin, int end);
The first argument represents the starting point of the subtring. If the substring() method
is called with only one argument, the subtring returned, will contain characters from
specified starting point to the end of original string.
But, if the call to substring() method has two arguments, the second argument specify the
end point of substring.
Note:If end parameter is missing, then the extraction starts from the starting position to
the rest of the string.
Example : String str = "0123456789";
System.out.println(str.substring(4));
Output: 456789
Example-1 : System.out.println(str.substring(4,7));
Output : 456
g. valueOf() : function is used to convert primitive data types into Strings. Overloaded
version of valueOf() method is present in String class for all primitive data types and for
type Object
The java toString() method is used when we need a string representation of an object. It
is defined in Object class. This method can be overridden to customize the String
representation of the Object.
Example : public class Car {
public static void main(String args[])
{
Car c=new Car();
System.out.println(c);
}
public String toString()
{
return "This is my car object";
}
}
Output : This is my car object
Whenever we will try to print any object of class Car, its toString() function will be called.
toString() can also be used with normal string objects.
I. toUpperCase() : This method returns string with all lowercase character changed to
uppercase.
Example : String str = "abcdef";
System.out.println(str.toLowerCase());
Output : ABCDEF
j. trim() : This method returns a string from which any leading and trailing white spaces
has been removed.
Example: String str = " hello ";
System.out.println(str.trim());
Output : hello
k. equals() - The equals() method compares two objects for equality and returns true if
they are equal and the result is true if and only if the argument is not null and is a String
object that represents the same sequence of characters as this object.
Example:
String obj1 = new String("xyz");
String obj2 = new String("xyz");
if(obj1.equals(obj2))
System.out.printlln("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE")
“==” operator:
In Java, when the “==” operator is used to compare 2 objects, it checks to see if the
objects refer to the same place in memory. In other words, it checks to see if the 2 object
names are basically references to the same memory location. A very simple example will
help clarify this:
Example:
Conclusion: == Operator compares the references. Though the objects, obj1 and obj2
are same internally, they differ on using this operation as we compare references.
equals() on the other hand compares the values. Hence, the comparison between obj1
and obj2 would pass.
Output:0 1 -1
String Constructors:
String class provides many constructors to create a new String object from character
array, byte array, String Buffer, String Builder, etc.
A number of operations (for example, append(), insert(), or setLength()) can increase the
length of the character sequence in the string builder so that the resultant length() would
be greater than the current capacity(). When this happens, the capacity is automatically
increased.
For example, the following code
// creates empty builder, capacity 16
StringBuilder sb = new StringBuilder();
// adds 9 character string at beginning
sb.append("Greetings");
Example: public class StringBuilderDemo {
public static void main(String[] args) {
String palindrome = "Dot saw I was Tod";
StringBuilder sb = new StringBuilder(palindrome);
sb.reverse(); // reverse it
System.out.println(sb);
}
}
Output: doT saw I was toD
Choosing the wrong string class can cause undesirable results. For example, you can
easily append the letter X to a String object: String s = s + "X". But this concatenation
operation inside a loop is inefficient. That is because the String class happens to create a
StringBuilder (or StringBuffer) object to do the concatenation work. Creating this object
every time is costly.
A better choice for this example is the StringBuilder class. For example, we can simply
append the letter X to the StringBuilder object: sbd.append('X'). This is much faster than
the previous example. In sample testing (200,000 iterations), the previous example took
almost one minute to complete while StringBuilder took less than a second.
If you add a requirement for multiple threads, then StringBuffer becomes the better
choice. But in sample testing (one million iterations), StringBuffer was five times slower
than StringBuilder. However StringBuffer guarantees thread safety, whereas
StringBuilder does not. In other words, you can get unpredictable results when using
StringBuilder objects in multithreaded programs.
The processing of text often consists of parsing a formatted input string. Parsing is the division of
text into a set of discrete parts, or tokens, which in a certain sequence can convey a semantic
meaning. The StringTokenizer class provides the first step in this parsing process, often called
the lexer (lexical analyzer) or scanner. StringTokenizer implements the Enumeration interface.
Therefore, given an input string, you can enumerate the individual tokens contained in it using
StringTokenizer.
To use StringTokenizer, you specify an input string and a string that contains delimiters.
Delimiters are characters that separate tokens. Each character in the delimiters string is
considered a valid delimiter—for example, ",;:" sets the delimiters to a comma, semicolon, and
colon. The default set of delimiters consists of the whitespace characters: space, tab, newline,
and carriage return.
StringTokenizer Constructors:
a.countTokens() Returns the next number of tokens in the String using the current deliminter set.
b.hasMoreElements() Returns true if the Enumeration has more elements.
c. hasMoreTokens() Returns true if more tokens exist.
d.nextElement() Returns the next element in the Enumeration.
e. nextToken() Returns the next token of the String.
f. nextToken(String) Returns the next token, after switching to the new delimiter set.
Example usage:
String s = "this is a test";
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
println(st.nextToken());
}
Note: StringTokenizer is a legacy class that is retained for compatibility reasons although its use
is discouraged in new code. It is recommended that anyone seeking this functionality use the
split() method of String or the java.util.regex package instead.
Best Practices :
Create strings as literals instead of creating String objects using 'new' key word
whenever possible
+ operator gives best performance for String concatenation if Strings resolve at compile
time
StringBuffer with proper initial size gives best performance for String concatenation if
Strings resolve at run time.
When performing String transformation operations such as removing, inserting, replacing
or appending characters, concatenating or splitting Strings use either the StringBuilder or
the StringBuffer class. The StringBuilder class is introduced in Java 1.5 and is the non–
synchronized counterpart of the StringBuffer class. Thus if only one Thread will be
performing the String transformation operations then favor the StringBuilder class
because is the best performer
The Java String, StringBuilder and StringBuffer classes are not interchangeable. Pick the
string class that addresses your needs, or else your programs will be inefficient or even
incorrect.
Favor the creation of literal strings and string–valued constant expressions rather than
creating new String Objects using one of the String constructor methods
Utilizing character arrays to perform String transformation operations yields the best
performance results but is the less flexible approach
1. The String class is commonly used for holding and manipulating strings of text in Java
programs. It is found in the standard java.lang package which is automatically imported, so you
don't need to do anything special to use it.
2. The String class is immutable, so that once it is created a String object cannot be changed.
The String class has a number of methods, that appear to modify strings. Since strings are
immutable, what these methods really do is create and return a new string that contains the
result of the operation.
3. Strings are more efficient if they are not modified (because they are shared in the string
common pool). However, if you have to modify the content of a string frequently (such as a status
message), you should use the StringBuffer class (or the StringBuilder described below) instead.
4. For performance reason, Java's String is designed to be in between a primitive and a class.
5. String is immutable, it is not efficient to use String if you need to modify your string frequently
6. String class provides many constructors to create a new String object from character array,
byte array, StringBuffer, StringBuilder, etc.
7. StringBuffer class is used to create a mutable string object.
8. StringBuffer class is used when we have to make lot of modifications to our string. It is also
thread safe i.e multiple threads cannot access it simultaneously.
9. StringBuilder is not synchronized.
2.11. Enums
Enums are lists of constants. When you need a predefined list of values which do not represent
some kind of numeric or textual data, you should use an enum.
For instance, in a chess game you could represent the different types of pieces as an enum:
enum ChessPiece {
PAWN,
ROOK,
KNIGHT,
BISHOP,
QUEEN,
KING;
}
You should always use enums when a variable (especially a method parameter) can only take
one out of a small set of possible values. Examples would be things like type constants (contract
status: “permanent”, “temp”, “apprentice”), or flags (“execute now”, “defer execution”).
Introduction
Enumerations already existed in other languages like C, C++, SmallTalk etc. Hence Java Enums
are not something very new to programmers. Enums are used primarily to handle a collection of
logically grouped constants. In Java, for ages we have been handling constants (number
constants here) through separate Java files, sometimes by encapsulating those in relevant
POJOs, and even using a wrong design style of implementing an interface containing all
constants.
We used these constants in conditions implemented using ‘if’ statement or switch cases also.
First thing you can notice about all these approaches is that this made the code look clumsy.
Usage of interfaces made the code unnecessarily complex. Next, though the constants had
something common, the language hardly provided anything to treat them as an object. List of
drawbacks doesn’t end here. Let us take an example below and see what other drawbacks are
An enum type, also called enumeration type, is a type whose fields consist of a fixed set of
constants.
Detailed Explanation:
The Java programming language contains the enum keyword, which represents a special data
type that enables for a variable to belong to a set of predefined constants. The variable must be
equal to one of the values that have been predefined for it.
The values defined inside an enum are constants and shall be typed in uppercase letters. Also,
these values are implicitly static and final and cannot be changed, after their declaration. If an
enum is a member of a class, then it is implicitly defined as static. Finally, you should use an
enum, when you need to define and represent a fixed set of constants.
Syntax:
The most common example of a simple enum includes the days of the week, as shown below
Some Examples:
public final String name(); // Returns the name of this enum constant, exactly as declared in its
enum declaration.
// You could also override the toString() to provide a more user-friendly description.
public String toString(); // Returns the name of this enum constant, as contained in the
declaration.
// This method may be overridden.
public final int ordinal(); // Returns the ordinal of this enumeration constant.
Defining Enums
The enum can be defined within or outside the class because it is similar to a class.
class EnumExample2{
public static void main(String[] args) {
Season s=Season.WINTER;
System.out.println(s);
}}
Output:WINTER
class EnumExample2{
enum Season { WINTER, SPRING, SUMMER, FALL; }//semicolon(;) is optional here
}}
Output:WINTER
Note: Every time you create enum in java you are creating a fully-fledged enum type or class
and by default they provide basic implementation of all Object class methods. In addition to
getting a complete enum type in your code, they are also Comparable and Serializable.
After your enum is created you can declare variables of enum type to store one of the enum
predefined variable.
The enum constants have initial value that starts from 0, 1, 2, 3 and so on. But we can initialize
the specific value to the enum constants by defining fields and constructors. As specified earlier,
Enum can have fields, constructors and methods.
Output:WINTER 5
SPRING 10
SUMMER 15
FALL 20
If you need to iterate through all the constant fields in a Java enum, you can do this pretty
easily in a Java 5 for loop. You just need to use the values method of the enum type in the for
loop, as shown in this Java enum for loop example:
You can also use a Java enum type in an if/then statement. While the example below might be
better implemented as a switch/case statement, it does provide an enum if/then example:
Benefits of enum:
1) Enums in Java are type-safe, It means your enum will have a type for example "Currency" in
below example and you can not assign any value other than specified in Enum Constants.
2) Enum has its own name-space,we can use enum field only with class name(refer to the below
Currency example)
3) Best feature of Enum is you can use Enum in Java inside Switch statement like int or char
primitive data type.We will also see example of using java enum in switch statement in this java
enum tutorial.
4) Adding new constants on Enum in Java is easy and you can add new constants without
breaking existing code.
1/Enum in Java are reference type like class or interface and you can define constructor,
methods and variables inside java Enum which makes it more powerful than Enum in C and C++
.
2.You can specify values of enum constants at the creation time as shown in below example:
But for this to work you need to define a member variable and a constructor because PENNY (1)
is actually calling a constructor which accepts int value , see below example.
Code Snippets:
1.
2.The following example defines a string Enums values and use switch expression to control
decision based on Enum values
The output of the above ENUM example
java code
?
1
This type is new
The java.lang.Enum.compareTo() method compares this enum with the specified object for
order.Enum constants are only comparable to other enum constants of the same enum type.
This method returns a negative integer, zero, or a positive integer as this object is less than,
equal to, or greater than the specified object.
The output is : The two enum values are different and parameter is less ordered
Note that the Enum's constructor has to be private , and you can't access it , this private
constructor defines the constants listed in the beginning of the Enum body:
You may need to know how to access a value of Enum.For example the following code snippet
shows you how to access car Enum in the main class:
public class EnumTest {
static CarEnum mycar;
public static void main(String args[])
{
System.out.println(mycar.BMW.getCarType());
}
}
For each ENUM class type a values functions is auto generated which can be used to print all
the ENUMs available.
enum CarEnum {
BMW("BMW"), TOYOTA("TOYOTA"), FIAT("FIAT");
private String CarType;
private CarEnum(String CarType) {
this.CarType = CarType;
}
public String getCarType() {
return CarType;
}
}
Return enum constant type if a specific declared Enum constant identifier matches the string
passed to valueOf() function.
System.out.print(CarEnum.valueOf("BMW"));
}
}
enum CarEnum {
BMW("Bavarian Motor Works"),
TOYOTA("Too Often Yankees Overprice This Auto"),
FIAT("Fabbrica Italiana Automobili Torino");
private String CarType;
private CarEnum(String CarType) {
this.CarType = CarType;
}
public String getCarType() {
return CarType;
}
}
Best Practices:
You should use enum types any time you need to represent a fixed set of constants. That
includes natural enum types such as the planets in our solar system and data sets where you
know all possible values at compile time—for example, the choices on a menu, command line
flags, and so on.
Take Aways :
enum may implement many interfaces but cannot extend any class because it internally
extends Enum class
Historically in Java (and other languages), these were often stored as constants.
In its simplest form, it contains a comma separated list of names representing each of the
possible options.