Thanks to visit codestin.com
Credit goes to www.scribd.com

100% found this document useful (1 vote)
146 views30 pages

Important Notes On JAVA

Chapter 2 discusses data types in Java, categorizing them into fundamental, derived, and user-defined types, with a focus on eight primitive types: byte, short, int, long, char, float, double, and boolean. It also covers variable declarations, arrays, operators, and control structures, including decision-making and looping constructs such as if statements, switch statements, while loops, and for loops. The chapter emphasizes the importance of these concepts for managing data and controlling program flow in Java.

Uploaded by

Dipesh Adhikari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
146 views30 pages

Important Notes On JAVA

Chapter 2 discusses data types in Java, categorizing them into fundamental, derived, and user-defined types, with a focus on eight primitive types: byte, short, int, long, char, float, double, and boolean. It also covers variable declarations, arrays, operators, and control structures, including decision-making and looping constructs such as if statements, switch statements, while loops, and for loops. The chapter emphasizes the importance of these concepts for managing data and controlling program flow in Java.

Uploaded by

Dipesh Adhikari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter:2

Tokens,Expressions and Control Structures


Data types in java:
Data type is a special keyword used to allocate sufficient memory space for the data, in
other words Data type is used for representing the data in main memory(RAM) of the
computer.They are of three types.
1. Fundamental or primitive type
2. Derived data types
3. Userdefined datatypes
1. Fundamental data type:
Java defines eight simple types of fundamental data: byte, short, int, long, char, float,
double, and Boolean. These can be put in four groups:
• 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
numberswith 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 types
in java

1.Class
2.interfaces
• byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value
of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving
memory in large arrays, where the memory savings actually matters.

• short: The short data type is a 16-bit signed two's complement integer. It has a minimum
value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines
apply: you can use a short to save memory in large arrays, in situations where the memory
savings actually matters.
• int: By default, the int data type is a 32-bit signed two's complement integer, which has a
minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use
the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and
a maximum value of 232-1. Use the Integer class to use int data type as an unsigned integer.

• long: The long data type is a 64-bit two's complement integer. The signed long has a minimum
value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data
type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum
value of 264-1. Use this data type when you need a range of values wider than those provided
by int.
• float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values
is beyond the scope of this discussion, but is specified in the 
Floating-Point Types, Formats, and Values section of the Java Language Specification. As with
the recommendations for byte and short, use a float (instead of double) if you need to save
memory in large arrays of floating point numbers.
• double: The double data type is a double-precision 64-bit IEEE 754 floating point.
Its range of values is beyond the scope of this discussion, but is specified in the 
Floating-Point Types, Formats, and Values section of the Java Language
Specification. For decimal values, this data type is generally the default choice. As
mentioned above, this data type should never be used for precise values, such as
currency.
• boolean: The boolean data type has only two possible values: true and false. Use
this data type for simple flags that track true/false conditions. This data type
represents one bit of information, but its "size" isn't something that's precisely
defined.
• char: The char data type is a single 16-bit Unicode character. It has a minimum
value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
Java supports more than 18 international languages so java take 2 byte for
characters because for 18 international languages 1 byte of memory is not
sufficient.c supports ASCII code only 1 byte is enough to support english languages
symbols.
• Variable:
• A variable is nothing but a name given to a storage area that our programs can ma-
nipulate. Each variable in java has a specific type, which determines the size and layout
of the variable's memory; the range of values that can be stored within that memory;
and the set of operations that can be applied to the variable.The name of a variable can
be composed of letters, digits, and the underscore character. It must begin with either
a letter or an underscore.
• The basic form of a variable declaration is shown here:
• 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. You can initialize the variable by specifying an equal sign
and a value. To declare more than one variable of the specified type, use a
commaseparated list.
• THE JAVA LANGUAGE
• Here are several examples of variable declarations of various types. Note that some
• include an initialization.
• int a, b, c; // declares three ints, a, b, and c.
• int d = 3, e, f = 5; // declares three more ints
• byte z = 22; // initializes z.
• double pi = 3.14159; // declares an approximation of pi.
• char x = 'x'; // the variable x has the value 'x'.
Arrays:
An array is a group of similar-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.
One-Dimensional Arrays
A one-dimensional array is a list of like-typed variables. To create an array, you first
must create an array variable of the desired type. The general form of a one
dimensional array declaration is
type var-name[ ];
Here, type declares the base type of the array. For example, the following declares
an array named month with the type ―array of int‖:
int month [];
Operator:
An operator is a symbol that tells the compiler to perform specific mathematical or
logical functions. java language is rich in built-in operators and provides the
following types of operators. An operand is a term used to describe any object that
is capable of being manipulated. E.g.
A+B//A and B are operands and + is operators
• According to utility and action
1. Arithmatic operators
2. Relational Operator
3. Logical Operator
4. Bitwise Operator
5. Assignment Operator
6. Increament and decreament operator
7. Conditional operator
8. Misc Operator
1. Arithmatic operators
The basic operators for performing arithmetic are the same in many computer languages:
1. + Addition
2. - Subtraction
3. * Multiplication
4. / Division
5. % Modulus (Remainder)
2. Relational Operator:
< ( less than ), <= (less than or equal to ), > (greater than ), >= ( greater than
or equal to ), == ( equal to ) and ! = (not equal to ) are relational operators. A
logical expression is expression connected with a relational operator.
3. Logical Operator
The relational operators work with arbitrary numbers and generate true/false values.You
can also combine true/false values by using the Boolean operators, which take true/false
values as operands and compute new true/false values. The three Boolean operators are:
1. && AND operation
2. || OR
3. ! NOT inverter
4. Assignment Operator
These operators are used for assigning a value of expression to another identifier.
=, + =, - = , * =, /= and % = are assignment operators.
a = b+c results in storing the value of b+c in `a'.
a += 5 results in increasing the value of a by 5. a=a+5
a /= 3 results in storing the value a/3 in a and it is equivalent a=a/3
5. Conditional Operator
The operator ?: is the conditional operator. It is used as variable 1 = expression 1 ?
expression 2 : expression 3. Here expression 1 is a logical expression and expression
2
and expression 3 are expressions having numerical values. If expression 1 is true,
value
of expression 2 is assigned to variable 1 and otherwise expression3 is assigned.
int a , b , c , d , e ;
a=3; b=5; c=8;
d=(a<b) ? a : b ;
6. Bitwise Operator
java has a distinction of supporting special operator known as bitwise operator for
manip-
ulation of data at bit level. These operators are used for testing bits or shifting them
right or left. Bitwise operator may not be applied to float or double. Following are
the
bitwise operators.
a. & bitwise AND
b. | bitwise OR
c. ^ bitwise exclusive OR
d. << shift left
e. >> shift right
7.Increament and decreament operators:
Java has increament and decreament operators i.e ++ and --.
The ++ operators adds 1 to the operand while – subtracts 1 from the operands.
Two types of increament and dercreament operators .
a++//post increament operator
++a//pre increament operator
a--//post decreament operator
--a//pre decreament
E.g. j,k=7;
Expression operation interpretation
j=++k pre increament k=k+1;j=k//k=8;j=8
j=k++ post increament j=k;k=k+1;//j=7;k=8
j=--k pre decreament k=k-1; j=k;//k=6;j=6
j=k-- post decreament j=k;k=k-1;//j=7;k=6
8.Misc opefrators:
Java mis operators are:member access dot(.) operator, comma,array index,new
instance of and type cast
In general the program statements are executed in same order in which they
appear in the source program but if we want to alter the flow of normal execution
of a program ,control statements are used. Control structures enable us to specify
the flow of program control. They make it possible to make decissions ,to perform
tasks repeatedly or to jump from one section of code to another.

Types of decision making :


1. If statement
2. If else
3. If else if ladder
4. Nested if statement
5. Nested if else statement
6. Switch statement
Branching structure
In general the program statements are executed in same order in which they appear in
the source program but if we want to alter the flow of normal execution of a
program ,control statements are used. Control structures enable us to specify the flow
of program control. They make it possible to make decissions ,to perform tasks
repeatedly or to jump from one section of code to another.
A. Types of Decision making :
1. If statement
2. If else
3. If else if ladder
4. Nested if statement
5. Nested if else statement
6. Switch statement
B. Looping Structure:
Programmi languages provide various control structures that allow for more compli-
cated execution paths. A loop statement allows us to execute a statement or groupof
statements multiple times. Java programming language provides the following types of
loops to handle looping requirements.
1. while loop
2. do while loop
3. for loop
C.Jumping structure
Break
Continue
Return
Decisiion making structure
1.If statement:
An if statement consists of a Boolean expression followed by one or more
statements.If the Boolean expression evaluates to true, then the block of code
inside the 'if' statement will be executed. If the Boolean expression evaluates to
false, then the reset set of code after the end of the 'if' statement (after the closing
curly brace) will be executed.
If else statement
An if statement can be followed by an optional else statement, which executes
when the Boolean expression is false.If the Boolean expression evaluates to true,
then the if block will be executed,otherwise, the else block will be executed.C
programming language assumes any non-zero and non-null values as true,and if it is
either zero or null, then it is assumed as false value.
3.If else if ladder: In this statement there is one if condition, multiple else-if
condition and at last one else block.
4.Nested if statement:
When a series of decisions are invoked , we have to use more than one if else
statement in nested.
Syntax:
if(condition1)
{
if(coondition2)
{
Statement 1;
}
else
{
Statement2;
}
}
else
{
statement 3;
}
5. Nested if-else statement:
Similar to nested if statement if else statement shall also be written inside the body of
another if else body called called nested if else statements.Syntax:
if(condition1)
{
if(coondition2)
Statement 1;
else
Statement2;
}
else
{
if(coondition3)
Statement 3;
else
Statement 4;

}
Example program of nested if else
Switch Statement:
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 switched on is checked for each
switch case. The following rules apply to a switch statement:
• The expression used in a switch statement must have an integral or enumerated
type, or be of a class type in which the class has a single conversion function to
an integral or enumerated type.
• 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.
• The constant-expression for a case must be the same data type as the variable in
the switch, and it must be a constant or a literal.
• When the variable being switched on is 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.
Syntax:
switch(expression)
{
case constant 1: statement 1; break;
case constant 2: statement 2; break;
case constant 3: statement 3; break;
case constant 4: statement 1; break;

default: default statement;


}
Fig: Flowchart of switch statement
#include<stdio.h>
#include<conio.h>

void main()
{
int month;
printf("enter the value of month:");
scanf("%d",&month);
switch(month)
{
case 1: printf("January"); break;
case 2: printf("February"); break;
case 3: printf("March"); break;
case 4: printf("April"); break;
case 5: printf("May"); break;
case 6: printf("june"); break;
case 7: printf("july"); break;
case 8: printf(“August"); break;
case 9: printf(“September"); break;
case 10: printf(“October"); break;
case 11: printf(“November"); break;
case 12: printf(“December"); break;

default: printf("you should entered the no of month between(1-12)");


}
getch();

}
Looping Structure:
Programming languages provide various control structures that allow for more compli-
cated execution paths. A loop statement allows us to execute a statement or groupof
statements multiple times. C programming language provides the following types of
loops to handle looping requirements.
1. while loop
2. do while loop
3. for loop

1. while loop:
A while loop in C programming repeatedly executes a target statement as long as a
given condition is true.
Syntax:
initialization;i=1
while ( i<=100 )
{
statement ( s ) ;
increament/decreament;
}
• Here, statement(s) may be a single statement or a block of statements. The condition
may be any expression, and true is any non zero value.
• The loop iterates while the condition is true. When the condition becomes false, the
program control passes to the line immediately following the loop. Here, the key point to
note is that a while loop might not execute at all. When the condition is tested and the
result is false, the loop body will be skipped and the rest statement after the while loop will
be executed.
• Flowchart of while loop:
//Program to print hello world 10 times.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=1;//initialization
while(i<=10)//condition
{
printf("Hello World\n");//statement
i++;//increament
}
getch();
}
//program to print numbers from 1 to 50
#include<stdio.h>
#include<conio.h>

void main()
{
int i;
i=1;
while(i<=50)
{
printf("%d\t",i);
i++; //i=i+1
}
getch();
}
2. 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.
Syntax:
for ( initialization ; condition ; increment/decreament )
{
Statements;
}
Example piece of code:
public static void main()
{
int i;-
for(i=1;i<=10;i++)
{
System.out.println("Hello World\n");
}
}
3. The do-while loop: The general form is:
initialization;
do
{
statement(s);
increament/decreament;
} while(test expression);
In this case, the program executes the statement(s) first and then evaluates the test
expression. If this expression is true, the loop will repeat. Otherwise, the loop terminates.
The curly braces are unnecessary if only a single statement is being repeated. This loop always
executes statement(s) at least once, because its test expression is at the bottom of the loop.
Example piece of code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=1;//initialization
do
{
printf("%d\t",i);//statement
i++;//increament
}while(i<=100);//condition
getch();

}
Jump Statements :
For the transfer of control from one part of the program to another, C supports three jump statements: break, continue, and return.
A. The break Statement: The use of break statement causes the immediate termination of the switch statement and the loop (all type of loops)
from the point of break statement. The control then passes to the statements following the switch statement and the loop.
Example piece of code:
for( i = 1; i <= 10; i++ )
{
if( i == 7 )
break;
Printf( " i =%d “, i);

}
}
Output:
i = 1 i = 2 i = 3 i = 4 i=5 i=6
In the code above the loop is terminated immediately after the value of i is 5.
The continue Statement: Continue statement causes the execution of the current iteration of
the loop to stop, and then continue at the next iteration of the loop.
Example:
for(i=1; i<= 10; i++ )
{
if( i == 7 )
continue;
}
System.out.println(+i);
}
Output:
1 2 3 45 6 8 9 10

C. The return Statement: It is used to transfer the program control back to the caller of the
method. There are two forms of return statement. First with general form return expression;
returns the value whereas the second with the form return; returns no value but only the
control to the caller.

You might also like