SlideShare a Scribd company logo
1 of 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
00000000 Location 1
00000000 Location 2
00100011 Location 3
Location 4
Location 5
Locations 0 – 3 are collectively
called as ‘temperature’
100011 is the binary equivalent of 35
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?

More Related Content

Similar to OOP - Lecture04 - Variables, DataTypes and TypeConversion.pptx

01 Java Language And OOP PART I
01 Java Language And OOP PART I01 Java Language And OOP PART I
01 Java Language And OOP PART IHari Christian
 
Lecture 4 variables data types and operators
Lecture 4  variables data types and operatorsLecture 4  variables data types and operators
Lecture 4 variables data types and operatorsalvin567
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of CSuchit Patel
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++Aahwini Esware gowda
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variablesteach4uin
 
Data types in Python.pptx
Data types in Python.pptxData types in Python.pptx
Data types in Python.pptxDukeCalvin
 
c++ computer programming language datatypes ,operators,Lecture 03 04
c++ computer programming language datatypes ,operators,Lecture 03 04c++ computer programming language datatypes ,operators,Lecture 03 04
c++ computer programming language datatypes ,operators,Lecture 03 04jabirMemon
 
CSC111-Chap_02.pdf
CSC111-Chap_02.pdfCSC111-Chap_02.pdf
CSC111-Chap_02.pdf2b75fd3051
 
java Basic Programming Needs
java Basic Programming Needsjava Basic Programming Needs
java Basic Programming NeedsRaja Sekhar
 
Lamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckLamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckseidounsemel
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2ndConnex
 

Similar to OOP - Lecture04 - Variables, DataTypes and TypeConversion.pptx (20)

Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
01 Java Language And OOP PART I
01 Java Language And OOP PART I01 Java Language And OOP PART I
01 Java Language And OOP PART I
 
Lecture 4 variables data types and operators
Lecture 4  variables data types and operatorsLecture 4  variables data types and operators
Lecture 4 variables data types and operators
 
Basic concept of c++
Basic concept of c++Basic concept of c++
Basic concept of c++
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of C
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
 
data types.pdf
data types.pdfdata types.pdf
data types.pdf
 
unit1 python.pptx
unit1 python.pptxunit1 python.pptx
unit1 python.pptx
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
Data types in Python.pptx
Data types in Python.pptxData types in Python.pptx
Data types in Python.pptx
 
c++ computer programming language datatypes ,operators,Lecture 03 04
c++ computer programming language datatypes ,operators,Lecture 03 04c++ computer programming language datatypes ,operators,Lecture 03 04
c++ computer programming language datatypes ,operators,Lecture 03 04
 
CSC111-Chap_02.pdf
CSC111-Chap_02.pdfCSC111-Chap_02.pdf
CSC111-Chap_02.pdf
 
java Basic Programming Needs
java Basic Programming Needsjava Basic Programming Needs
java Basic Programming Needs
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Prog1-L2.pptx
Prog1-L2.pptxProg1-L2.pptx
Prog1-L2.pptx
 
Lamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckLamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ck
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
 
Variables&DataTypes.pptx
Variables&DataTypes.pptxVariables&DataTypes.pptx
Variables&DataTypes.pptx
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
P1
P1P1
P1
 

Recently uploaded

一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办ezgenuh
 
Rekha Agarkar Escorts Service Kollam ❣️ 7014168258 ❣️ High Cost Unlimited Har...
Rekha Agarkar Escorts Service Kollam ❣️ 7014168258 ❣️ High Cost Unlimited Har...Rekha Agarkar Escorts Service Kollam ❣️ 7014168258 ❣️ High Cost Unlimited Har...
Rekha Agarkar Escorts Service Kollam ❣️ 7014168258 ❣️ High Cost Unlimited Har...nirzagarg
 
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...Delhi Call girls
 
Business Bay Escorts $#$ O56521286O $#$ Escort Service In Business Bay Dubai
Business Bay Escorts $#$ O56521286O $#$ Escort Service In Business Bay DubaiBusiness Bay Escorts $#$ O56521286O $#$ Escort Service In Business Bay Dubai
Business Bay Escorts $#$ O56521286O $#$ Escort Service In Business Bay DubaiAroojKhan71
 
Why Won't Your Subaru Key Come Out Of The Ignition Find Out Here!
Why Won't Your Subaru Key Come Out Of The Ignition Find Out Here!Why Won't Your Subaru Key Come Out Of The Ignition Find Out Here!
Why Won't Your Subaru Key Come Out Of The Ignition Find Out Here!AutoScandia
 
Delhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国ezgenuh
 
9990611130 Find & Book Russian Call Girls In Vijay Nagar
9990611130 Find & Book Russian Call Girls In Vijay Nagar9990611130 Find & Book Russian Call Girls In Vijay Nagar
9990611130 Find & Book Russian Call Girls In Vijay NagarGenuineGirls
 
Sales & Marketing Alignment_ How to Synergize for Success.pptx.pdf
Sales & Marketing Alignment_ How to Synergize for Success.pptx.pdfSales & Marketing Alignment_ How to Synergize for Success.pptx.pdf
Sales & Marketing Alignment_ How to Synergize for Success.pptx.pdfAggregage
 
What Causes BMW Chassis Stabilization Malfunction Warning To Appear
What Causes BMW Chassis Stabilization Malfunction Warning To AppearWhat Causes BMW Chassis Stabilization Malfunction Warning To Appear
What Causes BMW Chassis Stabilization Malfunction Warning To AppearJCL Automotive
 
John Deere 7430 7530 Tractors Diagnostic Service Manual W.pdf
John Deere 7430 7530 Tractors Diagnostic Service Manual W.pdfJohn Deere 7430 7530 Tractors Diagnostic Service Manual W.pdf
John Deere 7430 7530 Tractors Diagnostic Service Manual W.pdfExcavator
 
Greenery-Palette Pitch Deck by Slidesgo.pptx
Greenery-Palette Pitch Deck by Slidesgo.pptxGreenery-Palette Pitch Deck by Slidesgo.pptx
Greenery-Palette Pitch Deck by Slidesgo.pptxzohiiimughal286
 
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理ezgenuh
 
Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...
Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...
Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...anilsa9823
 
Bangalore Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore E...
Bangalore Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore E...Bangalore Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore E...
Bangalore Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore E...amitlee9823
 
Vip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp Number
Vip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp NumberVip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp Number
Vip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp Numberkumarajju5765
 
How To Fix Mercedes Benz Anti-Theft Protection Activation Issue
How To Fix Mercedes Benz Anti-Theft Protection Activation IssueHow To Fix Mercedes Benz Anti-Theft Protection Activation Issue
How To Fix Mercedes Benz Anti-Theft Protection Activation IssueTerry Sayther Automotive
 
Delhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...amitlee9823
 

Recently uploaded (20)

一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
 
Rekha Agarkar Escorts Service Kollam ❣️ 7014168258 ❣️ High Cost Unlimited Har...
Rekha Agarkar Escorts Service Kollam ❣️ 7014168258 ❣️ High Cost Unlimited Har...Rekha Agarkar Escorts Service Kollam ❣️ 7014168258 ❣️ High Cost Unlimited Har...
Rekha Agarkar Escorts Service Kollam ❣️ 7014168258 ❣️ High Cost Unlimited Har...
 
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
Call Girls in Malviya Nagar Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts Ser...
 
Business Bay Escorts $#$ O56521286O $#$ Escort Service In Business Bay Dubai
Business Bay Escorts $#$ O56521286O $#$ Escort Service In Business Bay DubaiBusiness Bay Escorts $#$ O56521286O $#$ Escort Service In Business Bay Dubai
Business Bay Escorts $#$ O56521286O $#$ Escort Service In Business Bay Dubai
 
Why Won't Your Subaru Key Come Out Of The Ignition Find Out Here!
Why Won't Your Subaru Key Come Out Of The Ignition Find Out Here!Why Won't Your Subaru Key Come Out Of The Ignition Find Out Here!
Why Won't Your Subaru Key Come Out Of The Ignition Find Out Here!
 
Delhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Mayur Vihar 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
 
9990611130 Find & Book Russian Call Girls In Vijay Nagar
9990611130 Find & Book Russian Call Girls In Vijay Nagar9990611130 Find & Book Russian Call Girls In Vijay Nagar
9990611130 Find & Book Russian Call Girls In Vijay Nagar
 
Sales & Marketing Alignment_ How to Synergize for Success.pptx.pdf
Sales & Marketing Alignment_ How to Synergize for Success.pptx.pdfSales & Marketing Alignment_ How to Synergize for Success.pptx.pdf
Sales & Marketing Alignment_ How to Synergize for Success.pptx.pdf
 
What Causes BMW Chassis Stabilization Malfunction Warning To Appear
What Causes BMW Chassis Stabilization Malfunction Warning To AppearWhat Causes BMW Chassis Stabilization Malfunction Warning To Appear
What Causes BMW Chassis Stabilization Malfunction Warning To Appear
 
John Deere 7430 7530 Tractors Diagnostic Service Manual W.pdf
John Deere 7430 7530 Tractors Diagnostic Service Manual W.pdfJohn Deere 7430 7530 Tractors Diagnostic Service Manual W.pdf
John Deere 7430 7530 Tractors Diagnostic Service Manual W.pdf
 
Greenery-Palette Pitch Deck by Slidesgo.pptx
Greenery-Palette Pitch Deck by Slidesgo.pptxGreenery-Palette Pitch Deck by Slidesgo.pptx
Greenery-Palette Pitch Deck by Slidesgo.pptx
 
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
 
Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...
Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...
Lucknow 💋 (Genuine) Escort Service Lucknow | Service-oriented sexy call girls...
 
Bangalore Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore E...
Bangalore Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore E...Bangalore Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore E...
Bangalore Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore E...
 
Vip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp Number
Vip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp NumberVip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp Number
Vip Hot Call Girls 🫤 Mahipalpur ➡️ 9711199171 ➡️ Delhi 🫦 Whatsapp Number
 
How To Fix Mercedes Benz Anti-Theft Protection Activation Issue
How To Fix Mercedes Benz Anti-Theft Protection Activation IssueHow To Fix Mercedes Benz Anti-Theft Protection Activation Issue
How To Fix Mercedes Benz Anti-Theft Protection Activation Issue
 
Delhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Kanakapura Road Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
 
Call Girls in Shri Niwas Puri Delhi 💯Call Us 🔝9953056974🔝
Call Girls in  Shri Niwas Puri  Delhi 💯Call Us 🔝9953056974🔝Call Girls in  Shri Niwas Puri  Delhi 💯Call Us 🔝9953056974🔝
Call Girls in Shri Niwas Puri Delhi 💯Call Us 🔝9953056974🔝
 

OOP - Lecture04 - Variables, DataTypes and TypeConversion.pptx

  • 1. Lecture 04 Variables, Data-Types and Type Conversion
  • 2. Lecture Outline • Value, Variable and Data Type • Type Conversion • Arithmetic Expression Evaluation • Scope of Variable
  • 3. 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
  • 4. 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.
  • 5. 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
  • 6. Variable – Example – Memory View 00000000 Location 0 00000000 Location 1 00000000 Location 2 00100011 Location 3 Location 4 Location 5 Locations 0 – 3 are collectively called as ‘temperature’ 100011 is the binary equivalent of 35
  • 7. 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
  • 8. Primitive Data Types - Numeric • Whole Numbers • Floating Point Numbers
  • 9. Primitive Data Types – Non-Numeric
  • 10. 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.
  • 11. 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.
  • 12. 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;
  • 13. 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
  • 14. 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
  • 15. Type Conversion in Expressions
  • 16. Type Conversion in Expressions - Output
  • 17. 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.
  • 18. 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;
  • 19. 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;
  • 20. What is the result of this expression? • 6 + 2 * 3 / 6; • 7 • 0.5 • 13.0 • 4
  • 21. 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
  • 22. 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.
  • 23. 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.
  • 24. What Will Happen Here? float f = 65/10 + 38/10; System.out.println(f);
  • 25. Thank You  Any Questions?