Object Oriented Programming
with Java
Programming
Structures in
Java
Rita Nagar
Assistant Professor, CSE Department MPGI
Outlines
1. Comments
2. Data types
3. Variables
4. Operators
5. Control Flow
6. Arrays & String
Object Oriented Programming in
Java statements
A statement specifies an action in a Java
program.
For example, a statement may tell to add
values of two variable x and y and assign their
addition to the variable sum. It then prints a
message to the standard output
Java statements can be classified into three categories:
Types of statement in Java
Declaration Expression Control flow
statement statement statement
Declaration statement
• A declaration statement is used to declare a variable.
• For example
A initialization statement is used to initialize a variable.
.
For example
Expression statement
• Expression is an essential building block of
any Java program.
• It is used to generate a new value.
Sometimes, we can also assign a value to a
variable.
• Expression is the combination of values,
variables, operators, and method calls.
• An expression with a semicolon at the end is
called an expression statement.
Example of expression
Control flow statement
Control-flow statements determine the order that statements
are executed.
If we want to execute a set of statements repeatedly for a
number of times or as long as a particular condition is true.
Java Tokens
• A token is the smallest element of a program that is
meaningful to the compiler.
• In Java, the program contains classes and methods.
Further, the methods contain the expressions and
statements required to perform a specific operation. These
statements and expressions are made up of tokens.
Types of Tokens
• Literals
• Keywords
• Variables
• Symbolic Constants
Literals
• Any constant value assigned to the variable is called
literal/constant.
• Literals in Java is a representation of boolean, numeric,
character, or string data.
• Literals represent fixed values in a source code.
• Once it has been defined cannot be changed.
Java supports several types of literals
• Integer literals: Used to represent integer values, such as 12, 15
• Floating-point literals: Used to represent floating-point values, such as 3.14159
• Boolean literals: Used to represent boolean values, such as true or false.
• Character literals: Used to represent single characters, enclosed in single
quotes, such as ‘a’ or ‘9’.
• String literals: Used to represent sequences of characters enclosed in double
quotes, such as “Hello, Btech (Sec-B) II semester!”.
• Null literal: Used to represent a reference that does not refer to any object,
represented by the keyword null.
Keywords
• Keywords are reserved words or pre-defined reserved words in
a programming language.
• Examples of keywords in Java include class, public, private, if,
else, while, for, switch, case, break, continue, return, and static,
among others.
Variables
• A variable is a container which holds the value while the Java
program is executed. A variable is assigned with a data type.
• Variable is a name of memory location.
Three types of variables in java
Local variable
Instance variable
Static variable
1) Local Variables
Local Variables are a variable that are declared inside the body of
a method.
2) Instance Variables
Instance variables are defined without the STATIC keyword .They
are defined Outside a method declaration. They are Object
specific and are known as instance variables.
3) Static Variables
Static variables are initialized only once, at the start of the
program execution. These variables should be initialized first,
before the initialization of any instance variables.
symbolic constant
• A symbolic constant is a named constant value defined once
and used throughout a program.
• Symbolic constants are declared using the final keyword. Which
indicates that the value cannot be changed once it is initialized
static final double PRICE=432.78
Data types
Data Types in Java are defined as specifiers that allocate different
sizes and types of values that can be stored in the variable or an
identifier. Java has a rich set of data types.
Data types in Java can be divided into two parts :
1.Primitive Data Types :- include integer, character, boolean, and
float
2.Non-primitive Data Types :- include classes, arrays and
interfaces.
Data type Bits Acquired In Memory
boolean 1
byte 8 (1 byte)
char 16 (2 bytes)
short 16(2 bytes)
int 32 (4 bytes)
long 64 (8 bytes)
float 32 (4 bytes)
double 64 (8 bytes)
Scope of variables
• Scope of a variable is the part of the program
where the variable is accessible.
Scope
Method
Block Scope
scope
Method Scope
Variables declared directly inside a method are available
anywhere in the method following the line of code in
which they were declared:
Method scope
Block Scope
• A block of code refers to all of the code
between curly braces {}.
• Variables declared inside blocks of code are
only accessible by the code between the curly
braces, which follows the line in which the
variable was declared
Example of block level
class Test
{
public static void main(String args[])
{
for (int x = 0; x < 4; x++)
{
System.out.println(x);
}
// Will produce error
System.out.println(x);
}
}
Type conversion
Type conversion
Widening Narrowing
Conversion
(Automatic) Explicit Conversion
Widening Conversion (Automatic)
The numeric data types are compatible with each other but no
automatic conversion is supported from numeric type to char or
Boolean. Also, char and Boolean are not compatible with each
other.
Explicit Conversion
If we want to assign a value of a larger data type to a smaller data
type we perform explicit type casting or narrowing.
Command-line arguments
The command line arguments in java are the arguments passed to
the program from the console.
The command line argument in java is the information passed to
the program at the time of running the program. It is the
argument passed through the console when the program is run.
Arguments within our java code is simple. They are stored as
an “array of Strings” passed to the main(). It is mostly named
as arg