Lecture 04
Variables, DataTypes and
TypeConversion
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 Casting
• Assigning a value of one type to a variable of another type is known as
Type Casting.
int x = 10;
byte y = (byte)x;
12
Widening or Automatic type converion
Automatic Type casting take place when,
• the two types are compatible
• the target type is larger than the source type
13
Example
public class Test { public static void main(String[] args) {
int i = 100;
long l = i; //no explicit type casting required
float f = l; //no explicit type casting required
[Link]("Int value "+i); [Link]("Long value "+l);
[Link]("Float value "+f);
}
}
14
Narrowing or Explicit type conversion
• When you are assigning a larger type value to a variable of smaller type,
then you need to perform explicit type casting.
15
Example
public class Test { public static void main(String[] args) { double d =
100.04;
long l = (long)d; //explicit type casting required
int i = (int)l; //explicit type casting required
[Link]("Double value "+d); [Link]("Long value
"+l); [Link]("Int value "+i);
}
}
16
Type Casting
• Done when information loss is acceptable.
double x=9.997
int nx= ( int ) x;
Q: what is the value stored in nx?
Introduction to Java
17
Type Casting
• Ans: 9
Q: suppose you want to round the number?
Ans:
double x=9.997
int nx= (int) [Link] (x);
Introduction to Java
18
Conversions
• Illegal to assign a floating-point expression to an integer variable
double balance = 13.75;
int dollars = balance; // Error
Casts: used to convert a value to a different type
int dollars = (int) balance; // OK
Introduction to Java
19
The Math class
• Math class: contains methods like sqrt and pow
• To compute xn, you write [Link](x, n)
• However, to compute x2 it is significantly more efficient simply to
compute x * x
• To take the square root of a number, use the [Link]; for example,
[Link](x)
Introduction to Java
20
Example
• In Java
• can be represented as
(-b + [Link](b * b - 4 * a * c)) / (2 * a)
Introduction to Java
21
Mathematical Methods in Java
[Link](x) square root
[Link](x, y) power xy
[Link](x) ex
[Link](x) natural log
[Link](x), [Link](x), sine, cosine, tangent (x in radian)
[Link](x)
[Link](x) closest integer to x
[Link](x, y), [Link](x, y) minimum, maximum
Introduction to Java
22
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;
[Link](f);
Thank You
Any Questions?