KEMBAR78
OOP - Lecture04 - Variables, DataTypes and TypeConversion | PDF | Variable (Computer Science) | Data Type
0% found this document useful (0 votes)
6 views25 pages

OOP - Lecture04 - Variables, DataTypes and TypeConversion

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
0% found this document useful (0 votes)
6 views25 pages

OOP - Lecture04 - Variables, DataTypes and TypeConversion

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/ 25

Lecture 04

Variables, Data-Types and


Type Conversion
Lecture Outline

• Value, Variable and Data Type


• Type Conversion
• Arithmetic Expression Evaluation
• Scope of Variable
Variable

• Variable is used to store a value inside a computer


• A variable is a space in the memory to store a value
• This space is reserved until the variable is required
3 Important Characteristics of Variable

• Type
• How much memory do a variable need.
• This information is determined by a type.
• Name
• How to differentiate a variable with another variable of the
same type.
• Name refers to the memory location assigned to this variable.
• Value
• What is the value?
• The actual value contained by a variable.
Variable - Example

• int temperature = 35;


• Type of variable is integer (written as “int” in Java)
• temperature is the name of variable which we will use
whenever we want to access or store the value in it
• 35 is the initial value that we are assigning it at the time of
declaration
Variable – Example – Memory View

00000000 Location 0
Locations 0 – 3 are collectively 00000000 Location 1
called as ‘temperature’ 00000000 Location 2
00100011 Location 3
Location 4
100011 is the binary equivalent of 35 Location 5
Variable Type

• Among other advantages a ‘type’ binds the memory to a


variable name.
• Java is more strictly typed than either language.
• For example, in C/C++ you can assign a floating-point value to
an integer. In Java, you cannot
• Also, in C/C++, there is not necessarily strong type-checking
between a parameter and an argument. In Java, there is
Primitive Data Types - Numeric

• Whole Numbers

• Floating Point Numbers


Primitive Data Types – Non-Numeric
Relative Comparison of int and double

int numPeople = 2;

Reserves 32 bits (4 bytes)


and sets the value stored
in that space to 2. The name
‘numPeople’ is associated with
this space.

double bill = 32.45;

Reserves 64 bits (8 bytes)


and sets the value stored
in that space to 32.45. The name
‘bill’ is associated with
this space.
Type Conversion

• Java can perform conversion automatically


• int value can be assigned to long
• Depends upon type compatibility
• Not all type conversions implicitly allowed
• Can’t assign a long value to int
• Solution: Casting

• Type casting is when you assign a value of one primitive


data type to another type.
Type Conversion – Widening Conversion

• Narrow data types are converted into broad data type


with out loss of information
• Both types are compatible.
• Numeric types are not compatible with boolean and char
• Destination type is larger than source type.
• Example
• byte -> int
• int -> long

int myInt = 9;

// Automatic casting: int to double


double myDouble = myInt;
Type Conversion – Narrowing Conversion

• Broader data type is converted into narrower data type


with loss of information
• Process is called casting (explicit type conversion)

float x = 3.4f;

int y = x; // Narrowing Conversion, Error

int y = (int) x; // No Error


Permitted Conversions

• byte -> short, int, long, float, or double


• short -> int, long, float, or double
• char -> int, long, float, or double
• int -> long, float, or double
• long -> float or double
• float -> double
Type Conversion in Expressions
Type Conversion in Expressions - Output
Description

• In the first subexpression, f * b, b is promoted to a


float and the result of the subexpression is float.
• Next, in the subexpression i / c, c is promoted to int,
and the result is of type int.
• Then, in d * s, the value of s is promoted to double,
and the type of the subexpression is double.
• Finally, these three intermediate values, float, int, and
double, are considered.
• The outcome of float plus an int is a float. Then the
resultant float minus the last double is promoted to
double, which is the type for the final result of the
expression.
Manipulating Variables

• Assignment Statement
• In Mathematics the value x = x + 1 is not possible why?
• In Java x = x +1 is possible because “=” is an assignment
operator and not an equality operator.
• Assignment operator means that the contents of the right hand
side is transferred to the memory location of the left hand side.
• Example:
• X = 5671;
Constants

• Constants are values which cannot be modified e.g. the


value of Pi
• To declare a constant in Java, we write a keyword “final”
before the variable type

final double pi = 3.14;


What is the result of this expression?

• 6 + 2 * 3 / 6;
• 7
• 0.5
• 13.0
• 4
Manipulating Values

• Mathematical Operators
• Common mathematical operators are available in Java for
manipulating values e.g. addition(+), subtraction(-),
multiplication(*), division(/), and modulus (%)
• Operator Precedence
• Operator precedence controls the order in which operations are
performed
• Operator Associativity
• The associativity of an operator specifies the order in which
operations of the same precedence are performed

• Do all multiplications, divisions and remainders from left


to right
• Do additions and subtractions from left to right
Scope of Variables

• Most other computer languages define two general


categories of scopes: global and local.
• In Java, the two major scopes are those defined by a
class and those defined by a method.
• The scope defined by a method begins with its opening
curly brace.
Non-Primitive Data Types

• So far the variable types that we have studied are


primitive data types.
• Primitive data types only have a memory space for
storing values.
• However, Object-Oriented Programming is special
because OOP has more variables then just primitive data
types.
What Will Happen Here?

float f = 65/10 + 38/10;


System.out.println(f);
Thank You 

Any Questions?

You might also like