 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
Expressions, Data Conversion, and Input
Objectives You should be able to describe: Data Types
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2440: 211 Interactive Web Programming Expressions & Operators.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
C++ Programming: Basic Elements of C++.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
August 6, 2009 Data Types, Variables, and Arrays.
Primitive Variables.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 2. Program Construction in Java. 01 Java basics.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Object Oriented Programming
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Java - Data Types, Variables, and Arrays
Expressions and Assignment
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
Review of Java Fundamentals
Presentation transcript:

 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable

Each location is 1 byte of memory 1 byte = 8 bits Each bit is carrying 1 or 0.

 To store a value inside a computer a ‘variable’ is used.  A variable is a space in the memory to store a value.  This space is reserved until the variable is required.

 Variable has three important characteristics: ◦ 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.

= 35temperatureint Type of the variable is integer (written as “int” in Java) A name of the variable An initial value of the variable

int temperature = Location Location Location Location 3 Location 4 Location 5 Locations 0 – 3 are collectively called as ‘temperature’ is the binary equivalent of 35

 Lets change the value of ‘temperature’. temperature = Location Location Location Location 3 Location 4 Location 5 Locations 0 – 3 are collectively called as ‘temperature’ is the binary equivalent of Location 2

 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 there is not necessarily strong type-checking between a parameter and an argument. In Java, there is. JavaC++

 The type int is of 4 bytes in Java.  Therefore, it can hold maximum of 2,147,483,647 value.  It can also hold values in negative down to -2,147,483,648.  Java does not support unsigned, positive-only integers.

 int cannot hold a real value.  Therefore, a type “double” is used to hold real values.  Double takes 8 bytes of memory instead of 4 bytes of a double.  Out of the 8 bytes in a double 4 bytes are used to hold the value before the decimal point and 4 bytes for the value after the decimal point.

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 The name ‘bill’ is associated with this space.

 In Java, the data type used to store characters is char.  However char in Java is not the same as char in C or C++. ◦ In C/C++, char is an integer type that is 8 bits wide. ◦ This is not the case in Java. Instead, Java uses Unicode to represent characters. ◦ Unicode defines a fully international character set that can represent all of the characters found in all human languages. ◦ It is a unification of dozens of character sets, such as Latin, Greek, Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more. ◦ For this purpose, it requires 16 bits. Thus, in Java char is a 16-bit type.

 Java has a simple type, called boolean, for logical values.  It can have only one of two possible values, true or false.  This is the type returned by all relational operators, suchas a < b.  boolean is also the type required by the conditional expressions that govern the control statements such as if and for.

 The simple types represent single values—not complex objects.  Although Java is otherwise completely object- oriented, the simple types are not. ◦ That’s why Java is 99.99% Object Oriented Programming Language  The simple types are defined to have an explicit range and mathematical behavior. ◦ Languages such as C and C++ allow the size of an integer to vary based upon the dictates of the execution environment. However, Java is different. ◦ Because of Java’s portability requirement, all data types have a strictly defined range.

 Java can perform conversion automatically  int value can be assigned to long.  Depends upon type compatibility  Not all type conversions implicitly allowed.  Cant assign a long value to int.  Solution ◦ Casting

 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

 Narrowing conversion ◦ Broader data type is converted into narrower data type with loss of information ◦ Process is called casting ( explicit type conversion ) ◦ Target variable = (Target-type) Source variable  byte b;  int a=50;  b=(byte)a; ◦ Truncation?????? ◦ Type conversion in expressions  (f*b) + (i/c) –(d*s) ?????????

 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.

 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.

x = is written at the memory location reserved for x

 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 arithmetic expression * 3 / 6 a)7 b)0.5 c)13.0 d)4

 Mathematical Operators ◦ Common mathematical operators are available in Java for manipulating values e.g. addition(+), subtraction(-), multiplication(*), division(/), and modulus (%).  Java has many other operators also which we will study in due course.

 To evaluate an arithmetic expression two concepts needs to be understood ◦ 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

 Operators Precedence and Associativity for Java is following 1.*, /, %  Do all multiplications, divisions and remainders from left to right. 2.+, -  Do additions and subtractions from left to right.

6 + 2 * 3 / 6  Three operators are in this expression.  However, * and / both have the same precedence and + has lower precedence then these two.  * and / will be evaluated first but both have the same precedence level.  Therefore, operator associativity will be used here to determine the first to get evaluated i.e. left to right.  The right most sub expression will be evaluated followed by the next right one and so on.

 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.

Blocks can be nested, In Java you cannot declare a variable to have the same name as one in an outer scope. In this regard, Java differs from C and C++.

 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.

 Objects are one step ahead of primitive data types.  They contain values and operations both. ◦ e.g. A String object has a value as well as operations that could be performed on that value e.g. operations to find its size, operation to compare its size with another String etc.

 1 : What will happen? float f=65/10+38/10; System.out.println(f);

 2: What will be the output? int i=638; byte b=(byte)i; System.out.println(b);

Questions? 41