0% found this document useful (0 votes)
71 views26 pages

Ch02 Lecture03 ECEg3142 OOP 20181122-Introduction To Java Elements

Uploaded by

Bitcoin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views26 pages

Ch02 Lecture03 ECEg3142 OOP 20181122-Introduction To Java Elements

Uploaded by

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

Fundamental Programming

Structures

Tensay G. Kiflu
Department of Electrical and Computer Engineering (ECE)
Addis Ababa Science and Technology University(AASTU)
Variables

 The variable is the basic unit of storage in a


Java program.
 A variable is defined by the combination of
 An identifier
 A type
 An optional initializer.
 In addition, all variables have a scope, which
defines their visibility, and a lifetime.

© 2018 Tensay, ECE Dept., AASTU 2


Declaring a Variable
 All variables must be declared before they
can be used.
 The basic form of a variable declaration

 Examples of variable declarations


 int a, b, c; // declares three ints, a, b, and c.
 double pi = 3.14159; // declares an
approximation of pi.

© 2018 Tensay, ECE Dept., AASTU 3


Scope and Lifetime of Variables

 Java allows variables to be declared within


any block.
 A block is begun with an opening curly
brace and ended by a closing curly brace.
 A block defines a scope.
 A scope determines what objects are visible
to other parts of your program.
 It also determines the lifetime of those
objects.
© 2018 Tensay, ECE Dept., AASTU 4
Scope Illustration

© 2018 Tensay, ECE Dept., AASTU 5


Lifetime of Variables

 Variables are
 created when their scope is entered
 destroyed when their scope is left
 This means that a variable will not hold its
value once it has gone out of scope.
 Variables declared within a method will not
hold their values between calls to that
method.

© 2018 Tensay, ECE Dept., AASTU 6


Lifetime of Variables …
 A variable declared within a block will lose its
value when the block is left.
 Thus, the lifetime of a variable is confined to
its scope.

© 2018 Tensay, ECE Dept., AASTU 7


Lifetime Illustration

© 2018 Tensay, ECE Dept., AASTU 8


Constants

 If we want a value to remain fixed, then we


use a constant.
 A constant is declared in a manner similar to
a variable but with the additional reserved
word final
 A constant must be assigned a value at the
time of its declaration

© 2018 Tensay, ECE Dept., AASTU 9


Examples of Constants Declaration

Named constant or symbolic constant


e.g.
PI
FARADAY_CONSTANT

Literal constant (an actual value)


e.g. 4

© 2018 Tensay, ECE Dept., AASTU 10


Primitive Data Types

 There are six numerical data types in Java:


 byte, short, int, long, float, and double.
 The data types byte, short, int, and long are
for integers
 The data types float and double are for real
numbers

© 2018 Tensay, ECE Dept., AASTU 11


Numerical Data Types and Precisions

© 2018 Tensay, ECE Dept., AASTU 12


Comments

 Java has three kinds of comments


 /* and */ surround multiline comments
 All text between the two delimiters is ignored.
 Comments cannot be nested
 Double-slashes (//) can be used for a single line of
comment.
 All the text up to the end of the line is ignored.
 /** and ends with */
 Special comments used for the javadoc system

© 2018 Tensay, ECE Dept., AASTU 13


Arithmetic Operators

Notice that x % y = 0 when y divides x perfectly; for example, 16 % 2 = 0. Also notice that x %
y = x when y is larger than x; for example, 23 % 25 = 23

© 2018 Tensay, ECE Dept., AASTU 14


Precedence Rules

© 2018 Tensay, ECE Dept., AASTU 15


Precedence Rules Illustration

© 2018 Tensay, ECE Dept., AASTU 16


Shorthand assignment operators

© 2018 Tensay, ECE Dept., AASTU 17


Relational Operators

© 2018 Tensay, ECE Dept., AASTU 18


Boolean Expressions and Variables

 Three boolean operators are


 AND, OR, and NOT
 In Java, the symbols
 &&, ||, and ! represent the AND, OR, and NOT
operators, respectively.

© 2018 Tensay, ECE Dept., AASTU 19


Boolean Operators

© 2018 Tensay, ECE Dept., AASTU 20


Numerical Input

© 2018 Tensay, ECE Dept., AASTU 21


Example 1

© 2018 Tensay, ECE Dept., AASTU 22


Example 2

© 2018 Tensay, ECE Dept., AASTU 23


Example 3

© 2018 Tensay, ECE Dept., AASTU 24


Complete Program

© 2018 Tensay, ECE Dept., AASTU 25


Thank You!

Questions?

© 2018 Tensay, ECE Dept., AASTU 26

You might also like