Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Similar documents
CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

Number Representations

Chapter 2 Number System

Chapter 3. Information Representation

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Data Representation and Binary Arithmetic. Lecture 2

Chapter 2 Bits, Data Types, and Operations

Under the Hood: Data Representation. Computer Science 104 Lecture 2

Fundamentals of Programming (C)

Chapter 2 Bits, Data Types, and Operations

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

3.1. Unit 3. Binary Representation

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

Chapter 2 Bits, Data Types, and Operations

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

Oberon Data Types. Matteo Corti. December 5, 2001

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Number Systems II MA1S1. Tristan McLoughlin. November 30, 2013

Number Systems Base r

EE 109 Unit 2. Binary Representation Systems

Fundamental Data Types

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Chapter 2 Bits, Data Types, and Operations

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Do not start the test until instructed to do so!

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

Chapter 2 Bits, Data Types, and Operations

Fundamentals of Programming

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

Number System (Different Ways To Say How Many) Fall 2016

Simple Data Types in C. Alan L. Cox

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

The Binary Number System

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

ASSIGNMENT 5 TIPS AND TRICKS

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Chapter 8. Characters and Strings

Source coding and compression

Positional Number System

COMP2121: Microprocessors and Interfacing. Number Systems

Do not start the test until instructed to do so!

Java Basic Datatypees

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

RS-232 Control of the Advantage EQ281/8, EQ282/8 and Advantage SMS200

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

PureScan - ML1. Configuration Guide. Wireless Linear Imager Wireless Laser scanner - 1 -

void mouseclicked() { // Called when the mouse is pressed and released // at the same mouse position }

1. Character/String Data, Expressions & Intrinsic Functions. Numeric Representation of Non-numeric Values. (CHARACTER Data Type), Part 1

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

CPSC 301: Computing in the Life Sciences Lecture Notes 16: Data Representation

FD-011WU. 2D Barcode Reader User Guide V1.6CC

Variables and data types

CSE 30 Fall 2013 Final Exam

CSE 30 Winter 2014 Final Exam

Chapter 2 Elementary Programming

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Do not start the test until instructed to do so!

Characters Lesson Outline

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

JAVA Programming Fundamentals

Lecture (09) x86 programming 8

CSE 30 Spring 2007 Final Exam

Inf2C - Computer Systems Lecture 2 Data Representation

marson MT8200S 2D Handheld Scanner User Manual V / 6 / 25 - I -

Work relative to other classes

RS-232 Control of the Advantage DRI

3. Java - Language Constructs I

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

CSE 30 Fall 2007 Final Exam

Getting started with Java

Computer System and programming in C

Integer Representation Floating point Representation Other data types

MACHINE LEVEL REPRESENTATION OF DATA

This is great when speed is important and relatively few words are necessary, but Max would be a terrible language for writing a text editor.

ASCII Code - The extended ASCII table

Table of Contents Sleep Settings How to Configure the Scanner. 7 Chapter 2 System Setup

UNIT 2 NUMBER SYSTEM AND PROGRAMMING LANGUAGES

MK D Imager Barcode Scanner Configuration Guide

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

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

CSE 30 Spring 2006 Final Exam

6.096 Introduction to C++ January (IAP) 2009

COMP6700/2140 Data and Types

CSE 30 Winter 2009 Final Exam

Java Notes. 10th ICSE. Saravanan Ganesh

Transcription:

Exercises Software Development I 03 Data Representation Data types, range of values, ernal format, literals October 22nd, 2014 Software Development I Wer term 2013/2014 Priv.-Doz. Dipl.-Ing. Dr. Andreas Riener Institute for Pervasive Computing Johannes Kepler University Linz riener@pervasive.jku.at

Terminology: Variables Each variable is, when defined, related to a specific data type Data type defines the range of values, e.g., byte = 0 255 (8bit) At runtime, each and every variable has also assigned a concrete value Default value Assigned by the programmer Determined within assignment operation Values (eger, float, characters, etc.) are ernally represented (i.e., working memory) as binary numbers ( calculating with binary numbers?) Data type conversions Possible, but not always without loss (of precision) Software Development I // Exercises // 03 Data Representation // 2

Variables: Data Types and Range of Values Integer types byte 8bit -128 to +127 short 16bit -32768 to +32767 32bit -2147483648 to +2147483647 (=2 31-1 to 2 31, 32nd bit used for sign) long 64bit -9223372036854775808 to +9223372036854775807 Floating-po types float 32bit single precision double 64bit double precision Character type char 16bit single character, Unicode character set modes of assignments 1) alphanumeric character in single quote ('a', 'B', etc.) 2) 4-digit hexadecimal Unicode index (\u0000 to \uffff) 3) Integer value (16bit unsigned; 0 to 65535) (?) Software Development I // Exercises // 03 Data Representation // 3

Internal Format: Integer Values Values (of any data type) are ernally represented as binary numbers (sequence of digits 0/1) Example: short number1, number2, result; //short 16 bit number1 = 7; number2 = 15; result = number1 + number2; result = (short) number1 + number2; // Java speciality! // (more later) //calculation number1 = 7 00000000 00000111 number2 = 15 00000000 00001111 result = 22 00000000 00010110 Software Development I // Exercises // 03 Data Representation // 4

Internal Format: From Binary to Integer Numbers Given: binary number b n+1 b n b n-1... b 2 b 1 //b n+1 = sign Leading sign: 1st (leading) bit as indicator for positive/negative number b n+1 = 0 positive number b n+1 = 1 negative number Wanted: its (positive) eger value x x = n i= 1 2 Example: 00010100 2 0 x 2 0 + 0 x 2 1 + 1 x 2 2 + 0 x 2 3 + 1 x 2 4 = 20 10 1st bit is reserved for the leading sign maximum positive number (data type short): 16bit 1bit (sign) = 15 2 15-1 = 01111111 11111111 2 = 32767 10 b i i 1 [short: n=15, : n=31, long: n=63] Software Development I // Exercises // 03 Data Representation // 5

Internal Format: (Negative) Integer Values Given: binary number b n+1 b n b n-1... b 2 b 1 //b n+1 = sign Wanted: its (negative) eger value x not that easy: different variants possible (dependent, e.g., on computer or operating system architecture) Variant 1: Simple signed Binary number b n+1 b n b n-1... b 2 b 1 Remember: b n+1 =0 for positive numbers, b n+1 =1 for negative numbers Example (datatype short): Integer number 24 = 11000 2, data type short (2 Byte) 00000000 00011000 2 Integer number -24 = -11000 2, data type short (2 Byte) 10000000 00011000 2 All the other digits are not affected by the most significant digit (sign) Software Development I // Exercises // 03 Data Representation // 6

Internal Format: (Negative) Integer Values Variant 2: One s complement b n+1 =1 (negative number): bitwise inversion of every digit Example (datatype short): Integer number 24 = 11000 2, data type short (2 Byte) 00000000 00011000 2 Integer number -24 = -11000 2, data type short (2 Byte) Software Development I // Exercises // 03 Data Representation // 7 11111111 11100111 2 Every single digit is affected by the sign inversion is effective! Variant 3: Two s complement (implemented in Java) b n+1 =1 (negative number): one s complement + 1 Example (datatype short): Integer number 24 = 11000 2, data type short (2 Byte) 00000000 00011000 2 Integer number -24 = -11000 2, data type short (2 Byte) [one s complement) 11111111 11100111 2 Integer number -24 = -11000 2, data type short (2 Byte) [two s complement) 11111111 11101000 2 Todays computer system usually implement two s complement reason: simplified ernal representation (no additional control logic)

I Internal Format: Why is the negative number range greater by one? Expert knowledge Internal representation (example 16bit): -2 15 2 15-1 Largest positive number (2 15-1 = 32767) 01111111 11111111 2 Largest negative number; Expectation = -(largest positive number + 1)? (1) 10000000 00000000 2 // if data type not fixed to 16bit, // i.e., no overflow One s complement: (0) 01111111 11111111 2 Two s complement (=One s complement + 1): 10000000 00000000 2 Software Development I // Exercises // 03 Data Representation // 8

Internal Format: Floating-po Numbers s e1 e2... e8 f1 f2 f3... f23 Sign s Exponent e Mantissa M 0 = +; 1 = - exp - 127 floating-po num. <1; power of 2 (neg.) Normalized representation: (-1) s 2 exp-127 1.M (for 0 < e < 255) M = f 1 x 2-1 + f 2 x 2-2 + f 3 x 2-3 +... f 23 x 2-23 Not every real number can be represented using this floating-po format. Why? Each and every finite erval [a, b[ has room for endless (infinite) real numbers Only a subset of them can be represented using only a finite number of n Bytes (e.g. in Java n=4 for float, n=8 for double) Software Development I // Exercises // 03 Data Representation // 9

Internal Format: Floating-po Numbers Example: Floating-po number -160.0 1 10000110 01000000 00000000 0000000 2 Leading sign: s = -1 Exponent exp = 134 (0+2+4+0+0+0+0+128=134) Mantissa M = 0 x 2-1 + 1 x 2-2 + 0 x 2-3 +... 0 = 0.25 Result (floating-po number): -1 2 134-127 1.25 = -2 7 1.25 = -160.0 exact, i.e., no loss of precision! Software Development I // Exercises // 03 Data Representation // 10

Internal Format: Floating-po Numbers Special cases +/- Infinity and Not-a-Number (NaN) requires particular requirements/predefinitions +/- Infinity: Mantissa M is set to 0, exponent exp is set to 255 + Infinity binary representation: 0 11111111 00000000 00000000 0000000 2 floating-po number: 1 2 255-127 1.0 = 1 2 128 1.0 = Infinity - Infinity binary representation: 1 11111111 00000000 00000000 0000000 2 floating-po number: -1 2 255-127 1.0 = 1 2 128 1.0 = -Infinity Not-a-Number: Mantissa M is >0 (any digit!=0), exponent exp is set to 255 NaN binary representation:? 11111111??????????????????????? 2 floating-po number:? 2 255-127 1.? =? 2 128 1.? = NaN Software Development I // Exercises // 03 Data Representation // 11

Internal Format: Floating-po Numbers Floating-po number 0.0 requires special treatment (not representable in standard floating-po format!) s e1 e2... e8 f1 f2 f3... f23 Sign s Exponent e Mantissa M 0 = +; 1 = - exp - 127 floating-po num. <1; power of 2 (neg.) Sub-normal representation: (-1) s 2-127 0.M (for e=0) M = f 1 x 2-1 + f 2 x 2-2 + f 3 x 2-3 +... f 23 x 2-23 Mantissa M = 0, exponent exp = 0 0.0 (positive) binary representation: 0 00000000 00000000 00000000 0000000 2 floating-po number: 1 2-127 0.0 = 0.0-0.0 (negative) binary representation: 1 00000000 00000000 00000000 0000000 2 floating-po number: -1 2-127 0.0 = -0.0 Software Development I // Exercises // 03 Data Representation // 12

I SWE1 UE03 RoundingError.zip Internal Format: Floating-po Numbers Origin of rounding errors Example: float f1 = 1.1f; float f2 = 0.1f; float f3 = f1-2*f2; // exact value: 1.1f-2*0.1f = 0.9f if (f1==1.1f) System.out.prln(f1); // output: "1.1" if (f2==0.1f) System.out.prln(f2); // output: "0.1" if (f3==0.9f) System.out.prln(f3); // never reached; f3 is 0.90000004! if (f3==(1.1f-2*0.1f)) System.out.prln(f3); // reached; output: "0.90000004" Software Development I // Exercises // 03 Data Representation // 13

Implicit, explicit type conversions

I Data Types: Implicit Type Conversion +-*/% char byte short long float double char long float double byte long float double short long float double long float double long long long long long long float double float float float float float float float double double double double double double double double double Software Development I // Exercises // 03 Data Representation // 15

Data Types: Numeric Type Conversions Implicit & Explicit Rules for implicit (i.e., no type cast) and explicit type conversions Any eger type can be assigned without explicit conversion to any other greater (i.e., more bits ) eger type (e.g. byte, short to ) Character data type char is without explicit type conversion compatible to be assigned to all eger types (, and long) (Explicitly) converting an eger data type o another eger type of smaller size (i.e., fewer bits ): higher bits are truncated, the leading sign (most significant bit) is cut as well (sign is not adjusted!) Conversion from floating-po type double to float: Result is rounded (that float value with fewest difference to the original double value) Conversion from floating-po to eger data type is done by simply truncating all the decimal places (rounding against zero!); is the eger part of the floating-po number too large to fit o the corresponding eger data type then the result is the maximum (or minimum) representable number of that data type Software Development I // Exercises // 03 Data Representation // 16

Data Types: Conversion from Floating-po to Integer Number In any case such a conversion requires an explicit type cast; Java uses rounding against zero loss of precision! Example Truncating public static void main (String[] arg) { res1, res2; float number1 = 12.34f; double number2 = -67.89; } res1 = ()number1; res2 = ()number2; System.out.prln(res1); System.out.prln(res2); Results res1 = 12 res2 = -67 Example Overflow public static void main (String[] arg) { float numfl = 4147483647.37f; numint; 4147483647.37 > MAX(); result: numint = MAX() = 2147483647 Software Development I // Exercises // 03 Data Representation // 17 } numint = ()numfl; System.out.prln(numInt); Result numint = 2147483647

Data Types: Conversion from Integer to Integer Number Converting from large eger type o another eger type of smaller size (i.e., fewer bits ): higher bits are truncated, no sign adjustment public static void main (String[] arg) { numint1 = 123456789; // 00000111010110111100110100010101 numint2 = -123456; // 11111111111111100001110111000000 short numshort1 = (short) numint1; // 1100110100010101 short numshort2 = (short) numint2; // 0001110111000000 } System.out.prln(numShort1); System.out.prln(numShort2); 32bit 16bit most significant bit = sign! Results numshort1 = -13035 numshort2 = 7616 ( 64+128+256+1024+2048+4096=7616) Software Development I // Exercises // 03 Data Representation // 18

I Data Types: Character representation (type char) ASCII (American Standard Code for Information Interchange) code table Char Dec Oct Hex Char Dec Oct Hex Char Dec Oct Hex Char Dec Oct Hex ------------------------------------------------------------------------------------- (nul) 0 0000 0x00 (sp) 32 0040 0x20 @ 64 0100 0x40 ` 96 0140 0x60 (soh) 1 0001 0x01! 33 0041 0x21 A 65 0101 0x41 a 97 0141 0x61 (stx) 2 0002 0x02 " 34 0042 0x22 B 66 0102 0x42 b 98 0142 0x62 (etx) 3 0003 0x03 # 35 0043 0x23 C 67 0103 0x43 c 99 0143 0x63 (eot) 4 0004 0x04 $ 36 0044 0x24 D 68 0104 0x44 d 100 0144 0x64 (enq) 5 0005 0x05 % 37 0045 0x25 E 69 0105 0x45 e 101 0145 0x65 (ack) 6 0006 0x06 & 38 0046 0x26 F 70 0106 0x46 f 102 0146 0x66 (bel) 7 0007 0x07 ' 39 0047 0x27 G 71 0107 0x47 g 103 0147 0x67 (bs) 8 0010 0x08 ( 40 0050 0x28 H 72 0110 0x48 h 104 0150 0x68 (ht) 9 0011 0x09 ) 41 0051 0x29 I 73 0111 0x49 i 105 0151 0x69 (nl) 10 0012 0x0a * 42 0052 0x2a J 74 0112 0x4a j 106 0152 0x6a (vt) 11 0013 0x0b + 43 0053 0x2b K 75 0113 0x4b k 107 0153 0x6b (np) 12 0014 0x0c, 44 0054 0x2c L 76 0114 0x4c l 108 0154 0x6c (cr) 13 0015 0x0d - 45 0055 0x2d M 77 0115 0x4d m 109 0155 0x6d (so) 14 0016 0x0e. 46 0056 0x2e N 78 0116 0x4e n 110 0156 0x6e (si) 15 0017 0x0f / 47 0057 0x2f O 79 0117 0x4f o 111 0157 0x6f (dle) 16 0020 0x10 0 48 0060 0x30 P 80 0120 0x50 p 112 0160 0x70 (dc1) 17 0021 0x11 1 49 0061 0x31 Q 81 0121 0x51 q 113 0161 0x71 (dc2) 18 0022 0x12 2 50 0062 0x32 R 82 0122 0x52 r 114 0162 0x72 (dc3) 19 0023 0x13 3 51 0063 0x33 S 83 0123 0x53 s 115 0163 0x73 (dc4) 20 0024 0x14 4 52 0064 0x34 T 84 0124 0x54 t 116 0164 0x74 (nak) 21 0025 0x15 5 53 0065 0x35 U 85 0125 0x55 u 117 0165 0x75 (syn) 22 0026 0x16 6 54 0066 0x36 V 86 0126 0x56 v 118 0166 0x76 (etb) 23 0027 0x17 7 55 0067 0x37 W 87 0127 0x57 w 119 0167 0x77 (can) 24 0030 0x18 8 56 0070 0x38 X 88 0130 0x58 x 120 0170 0x78 (em) 25 0031 0x19 9 57 0071 0x39 Y 89 0131 0x59 y 121 0171 0x79 (sub) 26 0032 0x1a : 58 0072 0x3a Z 90 0132 0x5a z 122 0172 0x7a (esc) 27 0033 0x1b ; 59 0073 0x3b [ 91 0133 0x5b { 123 0173 0x7b (fs) 28 0034 0x1c < 60 0074 0x3c \ 92 0134 0x5c 124 0174 0x7c (gs) 29 0035 0x1d = 61 0075 0x3d ] 93 0135 0x5d } 125 0175 0x7d (rs) 30 0036 0x1e > 62 0076 0x3e ^ 94 0136 0x5e ~ 126 0176 0x7e (us) 31 0037 0x1f? 63 0077 0x3f _ 95 0137 0x5f (del) 127 0177 0x7f Software Development I // Exercises // 03 Data Representation // 19 Specific mapping between character and number (1-127) After the 128 th character, the mapping allows for country (code table) specific character sets, etc.

Data Types: Character representation (type char) Examples: Calculation with characters public static void main (String[] arg) { char charres = 'b'-'a'; = 98-97 = 1 System.out.prln(()charRes); System.out.prln('b'-'a'); // simplified } Reverse calculation: from number to character public static void main (String[] arg) { char chara = 'A'; char charb; } charb = (char) (chara+1); = (char)(65+1) = (char)66 = 'B' System.out.prln(()charB); System.out.prln('A'+1); // simplified Application: Lowercase to uppercase conversion public static void main (String[] arg) { char charupper, charlower = 'q'; charupper = (char) (charlower-'a'+'a'); } Software Development I // Exercises // 03 Data Representation // 20 = (char)(113-97+65) = (char) 81 = 'Q'

Literals in Java In computer science, a literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as egers, floating-po numbers, strings, and booleans

I Java Literals: General Introduction What you type is what you get By literal we mean any number, text, or other information that represents a (concrete) value ( constant ) Literals in a Java program represent fixed and atomic values Literals must not be mixed up with the declaration of constants Example //eger literal, value 10 month = 10; In Java the following types of literals are known Integer/long numbers: 0; 123; 0xDadaCafe / 12L; 022L; Float/double numbers: 3.2f; -3e-22f / 3.2; -3e-22D Logic literals: true; false Character literals: 'A'; 'z'; '1' String literals: ""; "\""; "Hello World Reference literals: null; this; super Software Development I // Exercises // 03 Data Representation // 22

Java Literals: Integer type Integer literals are a sequence of digits To represent the type as long eger the value is followed by a suffix L or l Integer variables may be represented in Decimal format (base 10): default, no prefix/suffix needed - Example age = 19; Decimal format, long - Example long longliteral = 2101544L; Octal format (base 8): prefix 0 (zero) followed by digits 0 to 7 - Example octage = 023; Hexadecimal (base 16) : prefix 0x - Examples hexage = 0x13; short largeshort = 0xff32; //65330 10 Software Development I // Exercises // 03 Data Representation // 23

Java Literals: Hands on Experience (Integer) What about eger literals of type short/byte? eger literals are in Java always of type or long no short/byte literals, http://www.straub.as/java/basic/const.html Examples short s = 100; // max(s)=32767 // ok; literal; implicit type cast to 'short' (compiler) // assigned only if '' literal fits o 'short' type short s = 100000; // max(s)=32767 // Type mismatch: cannot convert from to short short s = 1000000000; // max(s)=32767 // The literal 1000000000 of type is out of range // Type mismatch: cannot convert from to short short s = 1000000000L; // max(s)=32767 // Type mismatch: cannot convert from long to short Software Development I // Exercises // 03 Data Representation // 24

Java Literals: Character type Character literals are specified as a single prable character in a pair of single quote characters (ASCII character set: letters, numerals, punctuations, etc.) Standard characters - Examples char h = 'h', e = 'E', one = '1', special = '#'; Characters represented by their eger value (dec, hex, oct, dec) - Examples char h = 104, e = 0x45, one = 061, special = 35; Special characters (not readily prable through a keyboard): Escape sequences - Examples char newline = '\n', singlequote = '\'', backslash = '\\'; - Newline symbol - can be used to detect if the user has pressed the return key - but: '\n' may have, depending on the platform, different meaning, e.g. carriage return (CR), carriage return + line feed (CR+LF), etc. - in Java both symbols ('\n', '\r' ) have different, fixed values! Software Development I // Exercises // 03 Data Representation // 25

Java Literals: Character type (con t) Character literals are specified as a single prable character in a pair of single quote characters (ASCII character set: letters, numerals, punctuations, etc.) Escape sequences in combination with numbers '\d' octal '\xd' hexadecimal '\ud' Unicode character d represents in each case a number Examples char unicodechar = '\u0041' //capital letter A = '\u0030' //digit 0 = '\u0022' //double quote " = '\u003b' //punctuation ; = '\u0009' //horizontal tab Software Development I // Exercises // 03 Data Representation // 26

Java Literals: Floating po type Floating po literals are like real numbers in mathematics Java has two kinds of floating po numbers: float and double; default type when you enter a floating po literal is double Floating po literals can be denoted as a decimal po, a fraction part, an exponent (represented by e or E), or as an eger; to represent the precision a suffix f or F (float) or d/d (double) may be added Examples Single precision float one = 1.0f, pi = 3.1415f, num = 4/5f, small = 2.9e-12f; Double precision double large = 6.5E32, one = 1D, belowzero=0.99999999; Software Development I // Exercises // 03 Data Representation // 27

Java Literals: Hands on Experience (Float) Mixed operations with float/double literals Example 1 float weight = 1.3; float weight = 1.3f; double weight = 1.3f; float f1 = (float)1.2; Error: double float ( type mismatch: cannot convert... ) Example 2 byte count = 5; float totalweight = count * weight; totalweight = count float * weight float = 5.0 * 1.3 = 6.5 Example 3 double x = 2.0 * 0.5; y; y = x; y = ()x; ok: explicit cast = () 1.0 = 1 Error: double ( type mismatch: cannot convert... ) Software Development I // Exercises // 03 Data Representation // 28

I SWE1 UE03 RoundingError.zip Java Literals: Hands on Experience (Float) Wrong result for the division of two numbers ( literals ) Why? Example:! float onethirdwrong = 3/9; // 3() /() 9() = 0() -> 0.0(float) WRONG! float onethirdcorrect = 3f/9; // 3(float) /(float) 9(float) = 0.333..4 (float)! number1 = 10; number2 = 3; double result = number1 / number2; // Same as before: division in type (type conversion table!) // Result enlarged to double (3 -> 3.0f), assigned to result WRONG! // Also wrong; 10/3 () = 3 -> 3.0D, assigned to result result = (double)(number1 / number2); // Correct solution (3.3333...5): // Use explicit type cast (either of number1 or number2) result = (double)number1 / number2; result = number1 / (double)number2; Software Development I // Exercises // 03 Data Representation // 30

I Data Types: Calculations with Different, Mixed Types Just few simple rules Data type with greater range of values dominates Explicit type casts to smaller data types Take care of decimal places and range of values (overflow) In-class activity (try on your own!) //use in each case the initial assignment a = 10; short b = 20; byte c = 30; float d = 1.2f; double e = 2.2; problem1: a + b + c + d + e =? problem2: a + b + c + () (d + e) =? problem3: () (e * e) (5 b c) * d =? Software Development I // Exercises // 03 Data Representation // 31

Exercises Software Development I 03 Data Representation Data types, range of values, ernal format, literals October 22nd, 2014 Software Development I Wer term 2013/2014 Priv.-Doz. Dipl.-Ing. Dr. Andreas Riener Institute for Pervasive Computing Johannes Kepler University Linz riener@pervasive.jku.at