0% found this document useful (0 votes)
9 views

Chapter 2

Uploaded by

eyuelayalew185
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Chapter 2

Uploaded by

eyuelayalew185
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 2

Revision of Basic Programming Constructs in java

1
Hello world in Java Program

 main ( ) is the method called when a Java application begins. Keep


in mind that Java is case-sensitive. Thus, Main is different from
main.

 String args[ ] declares a parameter named args, which is an


array of instances of the class String. Objects of type String
store character strings. In this case, args receives any
 All Java applications begin execution from main ( ) function.
command-line arguments present when the program is
(This is just like C/C++.)
executed.
 Public : is a java keyword which is access modifier, if a class is
preceded by public it implies that it can be accessed outside the  System.out.println This line outputs the string “Hellow
class even the package worled.” followed by a new line on the screen. Output is
 Static: allows main ( ) to be called without having to instantiate a actually accomplished by the built-in println( ) method. In this
particular instance of the class.
case, println( ) displays the string which is passed to it.
 void simply tells the compiler that main ( ) does not return a value
 We can use System.out.print(“Hellow worled”);

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:

► 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 numbers with fractional precision.

 Characters: This group includes char, which represents


symbols in a character set, like letters and numbers

 Strings. Strings are used for storing text. A String variable


contains a collection of characters surrounded by double
quotes:
4
Basic input/output, datatype, arithmetic and relational
operators, control statement
 Java programs are a collection of identifiers, comments, literals, operators, and keywords
etc.
 1. Identifiers: they are used for class names, method names, and variable names. They can be any
descriptive sequence of uppercase and lowercase letters, numbers, or the underscore and dollar-sign
characters. Value and value in java are different , some types of valid identifiers are :- Sum, X1, Y_Z, here
are some invalid identifiers 2x, high-temp, Not/Ok

 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

 1. Arithmetic Operators: Arithmetic operators are used in mathematical expressions in the


same way that they are used in algebra. The following table lists the arithmetic 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.

► The outcome of these operations is a Boolean value. The


relational operators are most frequently used in the
expressions that control the if statement and the various
loop statements.
9
Basic input/output, datatype, arithmetic and relational
operators, control statement
4. The Assignment Operator
► The assignment operator is the single equal sign, =. The Example
assignment operator works in Java much as it does in
any other computer language. It has this general form:

var = expression;

► Here, the type of var must be compatible with the type of


expression. The assignment operator does have one
interesting attribute that you may not be familiar with: it
allows you to create a chain of assignments. For
example, consider this fragment:int x, y, z;

x = y = z = 100; // set x, y, and z to 100


10
Basic input/output, datatype, arithmetic and relational
operators, control statement
5. Conditional Operator: Java includes a special
Example
ternary (three-way) operator that can replace certain
types of if-then-else statements.

The conditional operator has this general form:

expression1 ? expression2 : expression3

 Here, expression1 can be any expression that


evaluates to a boolean value. If expression1 is
true, then expression2 is evaluated; otherwise,
Assignment Review the operator’s
expression3 is evaluated. The result of the ? precedence in program execution
operation is that of the expression evaluated.
11
Java’s Control Statements
►A programming language uses control statements to cause the flow of execution to
advance and branch based on changes to the state of a program. Java’s program control
statements can be put into the following categories: selection, iteration, and jump.
✓ Selection statements allows program to choose different paths of execution based upon the
outcome of an expression or the state of a variable.

✓ 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

► for, while, and do-while. These statements create what we


commonly call loops. a loop repeatedly executes the same set
do-while loop
of instructions until a termination condition is met.

► 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

► Arrays: They are data structures consisting of related


data items of the same type .

► They are fixed length entities they remain the same


length once they are created .

► Arrays are objects so they are considered reference


types Declaring and creating arrays
► To refer a particular element of the array, we specify ► To create an array object the programmer specifies the
type of the array element and the size as part of
the name of the reference to the array and the position declaration with using the keyword new
number of the element in the array. The position
number of the element is called the elements index.

► array objects occupy space in memory.


19
Arrays in java
► Example

► Array initialization Manipulating array values

► For loop is used here to access each element of the


array iteratively.

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

You might also like