Chapter 2
Chapter 2
1
Hello world in Java Program
2
Java Programming Language development environment and tools
Integrated Development Environments (IDEs)
► They are software applications that provide a comprehensive set of tools for developers
to write, edit, debug, test, and deploy code more efficiently.
► Here are the most widely used IDE’s for java programming development
3
Basic input/output, datatype, arithmetic and relational
operators, control statement
Data types Boolean: This group includes boolean, which is a
special type for representing true/false values.
► Java defines eight simple types of data: byte, short, int, long,
Data types and their size
float, double and Boolean. These can be put in four groups:
2. Literals :A constant value in Java is created by using a literal representation of it. 100, 3.96, ”A+”,”
yes” .
3. Comments: They are never executed during program execution, They can be single line, multiline
comments , they are used to offer certain information the reader of the program. //, /* here are multiple
lines */
5
Basic input/output, datatype, arithmetic and relational
operators, control statement
4. Java Keywords:49 reserved keywords currently defined in the Java language. These
keywords, combined with the syntax of the operators and separators, form the definition of
the Java language. These keywords cannot be used as names for a variable, class, or method.
6
Basic input/output, datatype, arithmetic and relational
operators, control statement
Operators
7
Basic input/output, datatype, arithmetic and relational
operators, control statement
2. The Bitwise Operators: Java defines several bitwise operators which can be applied to the
integer types, long, int, short, char, and byte. These operators act upon the individual bits of
their operands.
8
Basic input/output, datatype, arithmetic and relational
operators, control statement
3. Relational Operators: They determine the relationship
that one variable value has to the other. Specifically, they
Example
determine equality and ordering.
var = expression;
✓ Iteration statements enable program execution to repeat one or more statements (that is,
iteration statements form loops).
✓ Jump statements allows program to execute in a nonlinear fashion. All of Java’s control
statements are examined here.
12
Selection statements
► 1. if Statement: It is Java’s conditional Example
branch statement. It can be used to route
program execution through two different
paths.
If(condition){
Statement;}
else{
Statement2;}
13
Selection statements
► 2. Nested if : is an if statement that is the Example
target of another if or else. They are very
common in programming. When you nest
ifs, the main thing to remember is that an
else statement always refers to the nearest if
statement that is within the same block as
the else and that is not already associated
with an else
14
Selection statements
► 3. The if-else-if Ladder: A common
Example
programming construct that is based upon a
sequence of nested ifs is the if-else-if ladder.
► if(condition1)
Statement 1;
else if(condition 2)
Statement2;
else if(condtion3)
Condition3 ;
► The if statements are executed from the top
to down
15
Example
Selection statements
► Switch Statements: Java's multi way branch statement. It
provides an easy way to dispatch execution to different parts
of your code based on the value of an expression. As such, it
often provides a better alternative than a large series of if-
else-if statements.
switch(case){
case value1:{
Statement1;
break;}
case value2:{
Statement2;
break; }
default:
Statement;
break;
16
Iteration Statements
► While Loop
17
Jump Statements
► break statement: A break statement takes control out of the
loop.
break
► continue statement: A continue statement takes control to the
beginning of the loop.
18
Arrays in java Array
► Data structures collection of data items
20
Data Input in Java
Example
Data inputs in java
► Import: it is a keyword that helps the compiler to locate
a class that can be used in a program.
► Java has rich set of predefined class that we can reuse
than “reinventing the wheel”
► Thes classes are grouped to package , which is a
collection of related classes, collectively named java
class libraries or java APIs(application programming
interfaces).
► All import declaration must appear before any class
declaration.
► Example import java.util.Scanner;
► We are importing the class named “Scanner” from the
package java.util. , It contains the collections
framework, legacy collection classes, event model, date
and time facilities, internationalization, and
miscellaneous utility classes (a string tokenizer, a
random-number generator, and a bit array).
21