Declaration and Memory

Similar documents
ECE 122 Engineering Problem Solving with Java

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

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

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

CMPT 125: Lecture 3 Data and Expressions

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

Chapter 2: Data and Expressions

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

Information Science 1

Chapter 3: Operators, Expressions and Type Conversion

UNIT- 3 Introduction to C++

Chapter 2: Data and Expressions

Operators and Expressions

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

3. Java - Language Constructs I

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Data Types and Variables in C language

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Chapter 2: Data and Expressions

Program Fundamentals

A First Program - Greeting.cpp

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

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

Visual C# Instructor s Manual Table of Contents

Basics of Java Programming

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

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

Chapter 2: Using Data

Lecture 3. More About C

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Data Conversion & Scanner Class

More Programming Constructs -- Introduction

A variable is a name for a location in memory A variable must be declared

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

Lecture 1. Types, Expressions, & Variables

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

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Computational Expression

Reserved Words and Identifiers

Information Science 1

Chapter. Let's explore some other fundamental programming concepts

Unit 3. Constants and Expressions

Chapter 12 Variables and Operators

Operators. Java operators are classified into three categories:

LECTURE 3 C++ Basics Part 2

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

Chapter 2: Using Data

Computer System and programming in C

Unit 3. Operators. School of Science and Technology INTRODUCTION

JAVA Programming Fundamentals

CHAPTER 3 Expressions, Functions, Output

Operators in java Operator operands.

Chapter 6 Primitive types

Primitive Data Types: Intro

Programming Lecture 3

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Building Java Programs

ESc101 : Fundamental of Computing

These are reserved words of the C language. For example int, float, if, else, for, while etc.

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

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

Java Notes. 10th ICSE. Saravanan Ganesh

Building Java Programs

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Java Programming Fundamentals. Visit for more.

Language Reference Manual simplicity

Ex: If you use a program to record sales, you will want to remember data:

Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Getting started with Java

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

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

Chapter 2: Basic Elements of C++

Chapter 2. C++ Basics

Java Fall 2018 Margaret Reid-Miller

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

1.3b Type Conversion

Eng. Mohammed S. Abdualal

Variables and literals

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

Chapter 2. Elementary Programming

Chapter 12 Variables and Operators

Informatics Ingeniería en Electrónica y Automática Industrial

Types and Expressions. Chapter 3

Values and Variables 1 / 30

Transcription:

Declaration and Memory With the declaration int width; the compiler will set aside a 4-byte (32-bit) block of memory (see right) The compiler has a symbol table, which will have an entry such as Identifier Type Block Address width int 8 Note: No initialization is involved; there is only an association of a name with an address. width 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 32 Integers vs. Reals Integers (byte, short, int, long): -3, -2, -1, 0, 1, 2, 3, Reals (double, float) include values with a decimal point; e.g., π 3.14579 e 2.71828 etc. 33 1

Reals (format, storage, range) Format Formatted according to the IEEE-754 standard for floating point arithmetic Includes a fractional part and a power (the details needn t concern us here) Storage float 4 bytes double 8 bytes Range float ±10 38 with 7 significant digits double ±10 308 with 15 significant digits 34 Real Examples double x; double interestrate = 1.5; float z = -1.1f; double abc = 3.4E-5; Float literal (default is double) Same as 0.000034 35 2

What happens if Special Cases Division by zero Integers: throws an arithmetic exception Reals: assigns a fictitious value, NaN ( not a number ) Out of range result Integers: range is treated as circular Reals: assigns a fictitious value, Infinity 36 The Type boolean Stores the result of a condition Has only two possible values, true or false true and false are reserved words Boolean variables are not integers 37 3

The Character Type char A char is a letter, digit, or symbol Stores the code, not the typeface The codes for English use ASCII 1 char is thus an (unsigned) integer type Numeric coding of characters uses the Unicode character set Unicode has 64K codes (next three slides) 1 ASCII codes are the first 256 entries in the Unicode character set. Try Wikipedia for more details. 38 Figure A.1 39 4

Figure A.2 40 Figure A.3 41 5

Character literals are recognized by single quotes surrounding a character, e.g., 'A' Special characters, such a single quote itself, are represented as literals using escape sequences Character Literals 42 Primitive and Non-primitive Types The eight types just introduced are short, byte, int, long, float, double, boolean, and char These are Java s primitive types They are internally supported Java also supports non-primitive types (class, interface, array) The most common non-primitive type is the String class (which supports literals) String literals: collection of characters enclosed in double quotes (e.g., "hello world") 43 6

Summary of Java s Primitive Types see next slide 44 N U M B E R PRIMITIVE TYPES I N T E G E R Type Size (bytes) Approximate Range min max S.D. S byte 1-128 +127?- I G short 2-32,768 +32,767?- N E int 4-2 10 9 +2 10 9?- D long 8-9 10 18 +9 10 18?- UNSIGNED char 2 0 65,535?- R E SINGLE float 4 +3.4 10 38 +3.4 10 38 7 A L DOUBLE double 8-1.7 10 308 +1.7 10 308 15 BOOLEAN boolean 1 true/false N/A - 45 7

Topics Anatomy of a program The declaration statement The assignment statement 46 Assignment Statement The statement (from Area.java) width = 8; is of the general form name = value; Pre-declared and in-scope Type can hold RHS Content will be overwritten Literal Name, or Expression Note: RHS = right-hand side, LHS = left-hand side 47 8

Examples int quantity; quantity = 25; int quantity = 25; int stock = quantity; int quantity = 25; char grade = B ; boolean isfound = false; double intrate = 1.25; int stock = 100; int order = 15; int total = order + stock; Declaration Assignment Declaration and assignment combined Name of variable on RHS Expression on RHS 48 Addition is Left to Right Note that int total = stock + order + 1; is the same as int total = (stock + order) + 1; Note: Parentheses force the enclosed operation to be performed first (which makes no difference in this case) 49 9

Summary of the int Arithmetic Operators see next slide 50 Precedence Operator Kind Syntax Operation -5 + infix x + y add y to x - infix x - y subtract y from x Lowest priority Highest priority -4-2 -1 * infix x * y multiply x by y / infix x / y divide x by y % infix x % y remainder of x / y + prefix +x identity - prefix -x negate x ++ prefix ++x x = x + 1; result = x -- prefix --x x = x - 1; result = x ++ postfix x++ result = x; x = x + 1 -- postfix x-- result = x; x = x - 1 51 10

Division (/) Notes (1) For integer operands, the result is an integer rounded toward zero, so 5 / 4 1-5 / 4-1 For real operands, the result is a real 5.0 / 4.0 1.25-5.0 / 4.0-1.25 52 Notes (2) Remainder (%) is the remainder after division I.e., a % b yields a (a / b) * b) 5 % 3 2 5 % -3 2-5 % 3-2 Note: the sign is always the same as the divisor 53 11

Notes (3) Auto increment (++), auto decrement (--) Prefix: Increment/decrement before using in an expression Postfix: Increment/decrement after using in an expression Example: If x is 5, z = ++x leads to z being 6 If x is 5, z = x++ leads to z being 5 In both cases above, x becomes 6 54 55 12

= 5 + 1 / 5-2 * 3 % 4 56 = 5 + 1 / 5-2 * 3 % 4 57 13

= 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 58 = 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 59 14

= 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 = 5 + 0-6 % 4 60 = 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 = 5 + 0-6 % 4 61 15

= 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 = 5 + 0-6 % 4 = 5 + 0-2 62 = 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 = 5 + 0-6 % 4 = 5 + 0-2 63 16

= 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 = 5 + 0-6 % 4 = 5 + 0-2 = 5-2 64 = 5 + 1 / 5-2 * 3 % 4 = 5 + 0-2 * 3 % 4 = 5 + 0-6 % 4 = 5 + 0-2 = 5-2 = 3 65 17

Note For the full scoop on Java operators, consult Oracle s Java Tutorials: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html 66 Mixing Types and Casting Promotion (aka widening conversion) Converts a lower type to a higher type (e.g., from int to float) Done automatically, when needed May lead to loss of precision, but the magnitude is preserved Demotion (aka narrowing conversion) Converts a higher type to a lower type (e.g., from double to long) Not done automatically Done explicitly using a cast Promotion and demotion illustrated (next slide) 67 18

double float Demotion long int Promotion byte short char 68 Promotion Demotion Examples float x = 1.5f; double xsquared = x * x; double x = 1.5; float xsquared = (float)(x * x); The result is a float, but it is automatically promoted to a double when assigned to xsquared. The initial result is a double, but it is cast down (i.e., demoted) to a float when assigned to xsquared. 69 19

Notes The cast operation has a precedence that is higher than * but less than ++ The = operator has the lowest precedence of all operators There are shorthand operators to combine assignment with an operator: x op= y is shorthand for x = x op y E.g., x += 1 is like x = x + 1 70 Example int ivar = 15; long lvar = 2; float fvar = 7.6f - ivar / lvar; double dvar = 1L / lvar + fvar / lvar; int result = 100 * dvar; What is the value of result? Do in class: Put the statements into a Java program, fix if need be, and output the value of result. 71 20

Thank You 72 21