CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Roundoff and truncation errors
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.
Mixing types in expressions Operators such as +, -, * and / are overloaded: the same name has many different values + overloaded as String concatenation.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 1.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
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.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
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.
Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.
Computer Science 210 Computer Organization Floating Point Representation.
Lecture 8 Floating point format
Chapter 3 Data Representation part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Binary Representation and Computer Arithmetic
Chapter3 Fixed Point Representation Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Chapter 1 Data Storage(3) Yonsei University 1 st Semester, 2015 Sanghyun Park.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Data Representation Dr. Ahmed El-Bialy Dr. Sahar Fawzy.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
COMP Primitive and Class Types Yi Hong May 14, 2015.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Floating Point Numbers
1.2 Primitive Data Types and Variables
 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.
Fixed-point and floating-point numbers Ellen Spertus MCS 111 October 4, 2001.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
FLOATING-POINT NUMBER REPRESENTATION
Fundamentals of Computer Science
Introduction To Computer Science
Chapter 6: Data Types Lectures # 10.
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Numbers in a Computer Unsigned integers Signed magnitude
April 2006 Saeid Nooshabadi
Modified from Sharon Guffy
Data Structures Mohammed Thajeel To the second year students
Introduction to Java, and DrJava part 1
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Presentation transcript:

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall

Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double, float Values of the primitive types are not objects –no properties –no capabilities

number systems Decimal (base 10) – with a decimal point __ /101/1001/1000 Binary (base 2) – with a binary point __ /21/41/81/16

Examples = 1* * * *2 -1 = ½ = 5½ = 1* * *2 -3 = ½ + ¼ + ⅛ = ⅞

double values: 0.0, 1.0, -3.5, e-3 inexact representation (!) operations: + - * / = 7.0+double X double  double 5.0 – 2.0 = 3.0-double X double  double 5.0 * 2.0 = 10.0*double X double  double 5.0 / 2.0 = 2.5/double X double  double

floating point types’ representation both double and float use the IEEE754 representation scheme: represents ± 1S * 2 E –S is represented in normalized form (no leading 0) –since first bit is always 1, it is not stored size of representation differs according to type: –the representation of a float is 4 bytes wide –the representation of a double is 8 bytes wide ±ES sign bit (indicates sign of number) exponent (expressed in two’s complement) significand (sometimes called mantissa) NB: I gloss over several important details of the IEEE754 standard here. The intent is not to give a full presentation of the standard, but to give you a rough idea of how floating point numbers are represented, and especially that integral types of floating point types use very different representation schemes!

Things to watch for! Representation is inexact –e.g. in base 10, 1/3 does not have an exact representation –e.g. similarly,  has a truncated representation –in base 2, 1/10 does not have an exact representation representation is in terms of powers of 2 Mixing magnitudes –it is possible that x+y is the same as x! 1.0e e-15  2.0 e e e-15  1.0 e+15 (whoops!) Round-off errors – comparisons! double d = 0.1; double pointNine = d+d+d+d+d+d+d+d+d; pointNine is not the same as 0.9 (whoops!)

mixing types in expressions Operators such as +, -, * and / are overloaded: the same name has many different values + overloaded as String concatenation too! “Good” + “ ” + “morning!”  “Good morning!” What happens in an expression which mixes values of different types? = ???? 5 is coerced to its equivalent double value, 5.0: = 7.5 Type coercion happens only from “smaller” type to “larger” type (e.g. int  double, not double  int)

relational operators We can form expressions like: x < y which have a value of true or false (i.e. the type of this expression is boolean) relational operators: >= == BUT: be careful doing == with floating point numbers!

Equality testing Equality testing: –of primitive values: == –of objects: equals method Consider: Foo a = new Foo();Foo c = a; Foo b = new Foo(); what is value ofwhat is value of (a==b)(a==c)

type casting a type cast is a function which maps from a value in one type to a corresponding value in another type (i.e. it performs a type conversion) form: ( ) semantics: mapped to example: (int) 2.1 maps to 2 example: (int) 2.9 maps to 2

type coercion a coercion is an implicit type conversion example – evaluates to 7.5: –4 is coerced to 4.0, then double addition takes place