4
Programming Concept in Java
In this chapter...
Here are the topics we'll cover
Control Flow
Control flow statements in Java enable us to control the flow of execution in a program. The main control flow statements in Java are —
Conditional Statements
There are two sorts of decision making constructs in Java.They are —
- if constructs
- switch constructs
if
: The if
statement is Java's conditional branch statement. It can be used to route program execution through two different paths. Here is the general from of the if
statement.
- Here, each
statement
may be single statement or a compound statement enclosed in curly braces i.e. block. Thecondition
is any expression that returns aboolean
valu. Theelse
clause is optional.
- Here, if
a
is less thenb
, thena
is set to zero. In no case are they both set to zero.
Nested ifs: A nested if
is an if
statement that is the target of another if
or else
. Nested if
s are very common in programming.
The if-else-if
Ladder: A common programming construct that is based upon a sequence of nested if
s is the if-else-if ladder. It looks like this.
- The
if
statements are executed from the top down. As soon as one of the conditions controlling theif
istrue
, the statement associated with thatif
is executed, and the rest of the ladder is bypassed. If the none of the conditions is true, then the finalelse
statement will be executed.
switch
: The switch
statement is Java's multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. It provides a better alternative than a large series of if-else-if
statements. It looks like this.
- Here is a simple example that uses a
switch
statement.
Looping Statements
Looping is a common programming situation that you can expect to encounter rather regularly. Loop can simply be described as a situation in which you may need to execute the same block of code over and over, Java supports three looping constructs, which are as follows —
- for Loop
- do...while Loop
- while Loop
while: The while
loop is Java's most fundamental loop statement. It repeats a statement or block while its controlling expression is true
. Here is the general form.
- The
condition
can be any Boolean expression. The body of the loop will be executed as long as the conditional expression is true. Whencondition
becomes false, control passes to the next line of the code immediately following the loop.
do-while: The do-while statement is similar to the while, with one difference, the continuation condition is evaluated after executing the code block. This causes for the code block to be executed at least once, unless there is a an exit condition embedded in it. Here is the general form.
for:
Branching Statements
- break statement
- continue statement
- return statement
- throw statement
Method Invocation
Exception Handling
- try-catch block
- finally block
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.
The new
keyword can also be used to create arrays. In a similar manner, it creates objects. An array is a data structure that holds a group of variables together. Its size is defined when it is created, and it cannot be changed.
Each variable can be accessed using an index that starts at 0 and goes up to the length of the array to -1. Arrays can hold primitive and refernce values.
- Declare array variables: To create an array, we first must create an array variable of the desired type. as
type var-name[];
ordatatype[] myarray;
.
- Let's declare first an array field and check what happening with it when an object is created.
- Making Arrays: We can make an exhibit by utilizing the new operator with the accompanying statement as
myarray = new datatype[sizeOfArray];
- Handling Arrays: At the point when handling components of an array, we frequently utilize either for or foreach.
- The forech Loops: JDK 1.5 presented another for construct, which is known as foreach loop or extended for loop. This construct empowers us to cross the complete array successively without utilizing an extra varable.
Strings
Strings are generally utilized as a part of Java, for writing computer programs, are a grouping of characters. String is not a primitive type. Nor is it simply an array of characters. Rather, String defines an object, and a full description of it requires an understanding of several object-related features.
String instances model texts and perform all kinds of operations o them. String type is a special type, because object of this type are given special treatment by the JVM.
- The most appropriate approach to make a string is to use the following statement.
String myString = "My World";
.
- The String class is immutable (changeless), so that once it is made a String object, its type can't be changed. This means that the JVM can reuse existing values to from new String values, without consuming additional memory. This process is called interning. One copy of each text value (literal) is saved to a special memory region called String Pool.
Determining String Length: Routines used to get data about an object are known as accessor methods. One accessor technique that you can use with strings is the length()
function.