ESc101 : Fundamental of Computing

Similar documents
ECE 122 Engineering Problem Solving with Java

1.3b Type Conversion

Programming in C++ 6. Floating point data types

Declaration and Memory

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Chapter 6 Primitive types

COMP Primitive and Class Types. Yi Hong May 14, 2015

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Lecture 2 Tao Wang 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Chapter 2: Using Data

CMPT 125: Lecture 3 Data and Expressions

Introduction to Programming Using Java (98-388)

Lecture 1. Types, Expressions, & Variables

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

COMP 110 Introduction to Programming. What did we discuss?

Information Science 1

Full file at

Basics of Java Programming

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Computational Expression

Lecture Set 4: More About Methods and More About Operators

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Data Types. Data Types. Integer Types. Signed Integers

Java Fall 2018 Margaret Reid-Miller

More Programming Constructs -- Introduction

PROGRAMMING FUNDAMENTALS

Chapter 2: Data and Expressions

Values, Variables, Types & Arithmetic Expressions. Agenda

CIS 110: Introduction to Computer Programming

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Operators. Lecture 3 COP 3014 Spring January 16, 2018

B.V. Patel Institute of BMC & IT, UTU 2014

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Lecture Set 4: More About Methods and More About Operators

Program Fundamentals

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

CSc Introduction to Computing

LECTURE 3 C++ Basics Part 2

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Data types Expressions Variables Assignment. COMP1400 Week 2

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

4. Inputting data or messages to a function is called passing data to the function.

Chapter 2: Data and Expressions

Values and Variables 1 / 30

Information Science 1

CS112 Lecture: Primitive Types, Operators, Strings

CSC Java Programming, Fall Java Data Types and Control Constructs

Full file at

Computational Expression

Computer Components. Software{ User Programs. Operating System. Hardware

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Fundamentals of Programming CS-110. Lecture 2

Zheng-Liang Lu Java Programming 45 / 79

Introduction to Programming EC-105. Lecture 2

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Chapter 7. Expressions and Assignment Statements ISBN

Lecture 3 Tao Wang 1

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Data Conversion & Scanner Class

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Lecture 2: Variables & Assignments

JAVA Programming Fundamentals

Primitive Data Types: Intro

Expressions and Casting

VARIABLES AND TYPES CITS1001

Chapter 7. Basic Types

Visual C# Instructor s Manual Table of Contents

Expressions & Assignment Statements

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Chapter 2: Data and Expressions

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CS61B Lecture #14: Integers

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Building Java Programs

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

Variables, Constants, and Data Types

Java Primer 1: Types, Classes and Operators

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Number Representation 3/2/01 Lecture #

Main Memory Organization

Building Java Programs

Engineering Computing I

Expressions & Flow Control

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Transcription:

ESc101 : Fundamental of Computing I Semester 2008-09 Lecture 9+10 Types Range of numeric types and literals (constants) in JAVA Expressions with values of mixed types 1

I : Range of numeric types and literals (constants) in JAVA 2

(Primitive) Data types in JAVA Domain Integer Floating points (fractional numbers) Boolean Characters Java type byte, short, int, long float, double boolean char 3

Numeric types in JAVA Type range size in bits byte -128 to 127 8 short -32768 to 32767 16 int -2 10 9 to 2 10 9 32 long -9 10 18 to 9 10 18 64 float ±3.4 10 38 and as small as ±1.4 10 45 32 double ±1.7 10 308 and more precision than float 64 4

Why different numeric types in JAVA arithmetic operations on floating points are more complex than on integers. Trade off between no. of bits and range(and/or precision) 5

Literals/Constants in Java Definition : The fixed values that are presented in their human readable form. We also call them constants. Examples : the number 100 is an integer constant. the number 12.453 is a floating-point constant. a is a character constant. true is a Boolean constant. What is Java type of a literal? 6

Integer constants in java By default, integer constants are of type int. To specify a long constant append an l or L. Example : 12 : int 12L : long 7

Assigning integer constants to integer data types in java IMPORTANT RULE : If i is a variable with integer data type, then we can assign any integer literal to i provided the value of the literal is lying within the range of type of i. 8

Assigning integer constants to integer data types in java Assignment Valid or Invalid byte b = 12;?? byte c = 156;?? short s1 = 3245;?? short s2 = 45678;?? 9

Assigning integer constants to integer data types in java Assignment byte b = 12; byte c = 156; short s1 = 3245; short s2 = 45678; Valid or Invalid valid invalid valid invalid 10

Assigning integer constants to integer data types in java Assignment Valid or Invalid int i = 123;?? int j = 3334445556667;?? long l = 123;?? long m = 3334445556667;?? long n = 3334445556667L;?? 11

Assigning integer constants to integer data types in java Assignment int i = 123; int j = 3334445556667; long l = 123; long m = 3334445556667; long n = 3334445556667L; Valid or Invalid valid invalid valid invalid valid 12

Floating point constants in Java By default, floating point constant are of type double. To specify a float constant, one must append an f or F. Examples : 1.223 : double 12.3f : float How to assign floating point literals to variables? 1. float f = 13.4f;?? 2. float f = 2.3;?? 3. float f = 2;?? Note : the reason for the validity/invalidity of the last two statements will be covered in next class when we discuss type conversion during assignment. 13

Floating point constants in Java By default, floating point constant are of type double. To specify a float constant, one must append an f or F. Examples : 1.223 : double 12.3f : float How to assign floating point literals to variables? 1. float f = 13.4f; valid 2. float f = 2.3; invalid 3. float f = 2; valid Note : the reason for the validity/invalidity of the last two statements will be covered in next class when we discuss type conversion during assignment. 14

The order among the numeric types Type range byte -128 to 127 short -32768 to 32767 int -2 10 9 to 2 10 9 long -9 10 18 to 9 10 18 float ±3.4 10 38 and as small as ±1.4 10 45 double ±1.7 10 308 and more precision than float 15

The numeric types in the increasing order of their range Based on the magnitude of the largest numeric value which can be stored in a data type (see last slide), the numeric types can be arranged from left to right in the increasing order of their range. byte short int long float double Terminology : int is wider than short but narrower than long float is wider than long but narrower than double 16

II : Expressions with values of mixed types 17

Expression built using different numeric types E : expression with operands of different types? How do we evaluate E? What is the type of E? Example : 1/2.3 4 18

Evaluation of an expression int i=2; byte b=45; double d = 34.567; (we use different colors to differentiate between different types Step 1 : (parenthesize): Step 2 :(replace the variables by their values) i b+d/i b (((i b) + (d/i)) b) (((2 45) + (34.567/2)) 45) Now we need to give rules for evaluation an expression of type value 1 o value 2. 19

Evaluation of an expression with single binary operator E : value 1 o value 2 o : a binary arithmetic operator. type(value 1 ) type(value 2 ). Evaluation of E is done in three steps. 1. value 1 and value 2 get promoted to int if they are of type byte or short. 2. if type(value 1 ) is wider than type(value 2 ) : : type of value 2 gets promoted to type(value 1 ). if type(value 2 ) is wider than type(value 1 ): : type of value 1 gets promoted to type(value 2 ). (At this stage type(value 1 ) = type(value 2 )) 3. the expression is evaluated. 20

Evaluation of an expression with single binary operator byte b=45; double d = 34.567; Expression :b/d b/d 45/34.567 45/34.567 45.0/34.567 1.3018 (replacing the variables by their values) (promotion of byte to int) (promotion of int to double) (Evaluation) 21