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.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
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.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Numerical Data Recitation – 01/30/2009
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
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.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
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.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Intro to CS – Honors I Programming Basics GEORGIOS PORTOKALIDIS
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Objectives You should be able to describe: Data Types
Simple Data Type Representation and conversion of numbers
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSC Programming I Lecture 5 August 30, 2002.
CSC 221 Computer Organization and Assembly Language
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Numbers in Computers.
Programming Principles Operators and Expressions.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Floating Point Representations
Tokens in C Keywords Identifiers Constants
Object Oriented Programming
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Data Structures Mohammed Thajeel To the second year students
Fundamental Data Types
Operators and Expressions
Introduction to Abstract Data Types
Introduction to Java, and DrJava part 1
Chapter 2: Java Fundamentals
Lectures on Numerical Methods
Introduction to Java, and DrJava
Fundamental Data Types
Introduction to Java, and DrJava part 1
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

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 of bounds and wrapped around. Overflow. Example

Lecture: Underflow Similarly underflow can occur. byte b; b = -128; b -= 1; Every type has their own range. Variable types should be chosen wisely.

Data Types * Primitive Data Types Numeric Data Types Integer Numbers – byte (8-bits),short (2-bytes) – int(4-bytes), long (8-bytes) Real Numbers – float (4-bytes), double (8-bytes) Character Data Types – char (2-bytes) Logic – boolean – Requires 1-bit information (true or false) but its size is not precisely defined. * Reference Data Types – Object (varied-sized bytes) – String

- Why many data types? Small numbers require less bytes for representation. Precision – Some numbers need to be represented precisely for computation. -Type safety in Java Java is strongly typed. The type of every variable must be declared before its use. This allows Java to perform many safety and sanity checks during compilation and avoids many program errors.

Variables The format of variable declaration is data_type variable_name; Variable Declaration: - Variable declaration sets aside memory to hold the variable and attaches a name to that space. - No need to use new operator. Examples: double volume; – Memory is allocated to store double values. int num; - Memory is allocated to store integer values.

Variable assignment and Initialization * float x = 10.05f; //x is initialized to * float x; x = 10.05f; * float x = 10.0f; //float variable x is initialized to 10.0 float y = 20.0f; //float variable y is initialized to 20.0 x = y; //variable assignment. x has value 20.0 Different from Object assignment Student student1; student1 = new Student(); The reference to the first object is stored in student1. Student student2; student2 = student1; The reference stored in student1 is copied to student2.

Variables, Literals, Constants : Things to remember - Java does not allow two variables to have the same name within the same method (More details later). int num = 5; int num = 10; - Changing from one type to another is called casting. double value = d; int num = (int) value; - Here, the values, 5 and 10 are called literals. The type of 10 is int. The type of 10.0 is double. - Constants Precede variable declaration with 'final' keyword. final double PI = 3.14f;

9 One’s Complement Representation Positive number uses positional representation. Negative number formed by inverting all bits of positive value. Example: 4-bit representation represents represents -2

10 Two’s Complement Representation Positive number uses positional representation. Negative number formed by subtracting 1 from positive value and inverting all bits of result. Example: 4-bit representation represents represents -2 High-order bit is set if number is negative

Integer representation Positive Numbers – Binary Equivalent short x = 8; // short – 2 bytes 8 = short y = 10; 10 = short z = x + y; // Binary Addition Negative Numbers – Two's complement Binary Equivalent short x = -8; (-8) will be stored in two's complement representation

Converting a negative number to two's complement representation Step 1: Convert the positive number to binary equivalent. Step 2: Flip the 0's to 1's and 1's to 0's. (One's complement) Step 3: Add 1 to the one's complement number obtained in step 2. Example: short x = -8; 1. 8 = =

IEEE floating point representation IEEE floating point numbers are represented using 32 bits (single precision) or 64 bits (double precision). Three Components Sign bit- Single bit. 0 represents negative number and 1 represents positive number. Exponent – The exponent field needs to represent both positive and negative exponents. Mantissa - Significant digits of the number. a xb e a – mantissa, e – exponent, b - base Floating point numbers are typically stored in normalized form. This basically puts the radix point(that which separates the integer part and the fractional part) after the first non-zero digit. In normalized form, five is represented as 5.0 × 100.

Single Precision Sign bit - 1 bit Exponent – 8 bits Mantissa – 23 bits Double Precision Sign bit - 1 bit Exponent – 11 bits Mantissa – 52 bits Errors in Floating point Arithmetic – Many rational numbers can only be approximately represented in memory. – The arithmetic on these numbers yield approximate answers. – These errors get propagated in floating point calculations.

Errors in floating point arithmetic: continued Example:  Converting 0.3 to binary yields which is approximately  In this case, five bits are not enough to represent 0.3 fully. We have an error of =  1.3 cannot be represented exactly using a 32-bit value. 1.3 * 3.0 will be instead of 3.9

Arithmetic operators for Numeric data Addition int a = 10; double x = a // 'a' is promoted to double Subtraction double a = 5.48; int b =10; b = b – (int) a; // Needs explicit casting Multiplication short a = 2; int b = 4; double c = a * b;

Division Integer Division – 20/10 = 2 – 20/11 = 1 Real Division When either or both numbers are float or double, then the result is a float or double respectively. 10.0/5.0 = / 2 = 2.5 Double y = /1000 double z = 1999/1000 What is the value of y and z?

Modulo Returns the remainder of a division, usually involves only integers. Examples: 11 % 5 = 1 23 % 5 = 3 8 % 2 = 0 Note: In both division and modulus operations, the second number should not be 0.

Arithmetic Operators precedence In decreasing order of precedence, Subexpression ( ) Starting with innermost. Unary Operators -, + Left to right evaluation. Multiplicative operators *, /, % Left to right evaluation. Additive operators +, - Left to right evaluation. Example: int a = 31, b = 16, c = 1, d = 2; int k = b + c * d – a / b / d; int l = (-b + c) * a / d;

Math Class Math class is very powerful. It provides all kinds of useful methods such as: abs(a): absolute value of a max(a, b): the larger of a and b min(a, b): the smaller of a and b pow(a, b): a raised to power b round(a): a rounded to the nearest whole number sqrt(a): square root of a ceil(a): smallest whole number no less than a floor(a): largest whole number no bigger than a sin(a): trigonometric sine of a cos(a): trigonometric cosine of a exp(a): natural number e(2.718…) raised to power a

public class MathDemo{ public static void main(String[] args){ //E and round() System.out.println("e = " + Math.round(Math.E*100)/100f); //PI System.out.println("pi = " + Math.round(Math.PI*100)/100f); //abs() System.out.println("Absolute number = " + Math.abs(Math.PI)); //ceil() System.out.println("Integer greater than or equal to = " + Math.ceil(Math.PI)); //exp() System.out.println("Exponent number powered by the argument = " + Math.exp(0)); //min() System.out.println("Minimum Number = " + Math.min(10,10.3)); //pow() System.out.println("Power = " + Math.pow(10,3)); }