# ©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.

## Presentation on theme: "©2004 Brooks/Cole Chapter 2 Variables, Values and Operations."— Presentation transcript:

©2004 Brooks/Cole Chapter 2 Variables, Values and Operations

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Data in a Java Program In this chapter we are going to talk about how data is stored in a Java program There are two basic kinds of data in a Java program –Primitive types (e.g. numbers) –Reference types (e.g. objects)

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Primitive Data Types Java has 8 primitive types that are part of the language Numeric types –Integers : byte, short, int, long –Real numbers : float, double Characters - char Boolean - boolean

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Integers Integers are stored using their base-two value –See section 1.8 if you are interested in the details Different types take different amounts of memory and store different ranges of values Default integer type is int –Literal representation is a number with no decimal point in it 123-52 For long, use an L after the number (123456789L)

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Real Numbers Also called floating point numbers Use the IEEE 754 standard –See appendix I for details Two formats for different ranged and precision Default type is double –Literal representation is a number with a decimal point 1.23-.045 1.6e-19 6.02E23 For float, use an F after the number (1.23F)

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Characters A char variable can store a single character Literal representation is the character enclosed in single quotes ('a' or 'Z') –Use the escape character for characters that need to be handled specially ('\n' or '\'') Characters are stored in memory as their Unicode values (16 bits)

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Boolean type The boolean type is used for data that can only have two values Literal values are true and false We'll talk more about these when we need them

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Primitive Data Types

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Arithmetic in Java Java has 5 binary operators to do arithmetic + Addition -Subtraction * Multiplication / Division works differently for integer and floating point numbers % Modulo

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Integer Division When both operands of the / operator are integers, the result will be an integer Any fractional part will be truncated –2/3 ---> 0 –4/3 ---> 1 –19/10 ---> 1 –19999/10000 -> 1

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Modulo Operator The % operator gives the remainder from an integer division –10 % 7 ---> 3 –9 % 10 ---> 9 –4 % 2 ---> 0

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Precedence and Associativity What happens when an expression has several operators? 5 + 3 * 7 Precedence rules to tell you what order the operations will be done Associativity rules tell you the order for several operations that have the same precedence

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Precedence 1.Parenthesized expressions 2.Negation 3.Multiplication, division and modulo 4.Addition and subtraction 3 + 5 * 7 will be 38 because multiplication is done first

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Associativity Arithmetic operators with same precedence associate from left to right a * b / c % d ---> (((a * b) / c) % d) 8 / 2 / 2 ---> 2

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 String Concatenation The + operator has a second meaning in Java If + has two strings as operands, it will concatenate the two strings –"top" + "cat" ---> "topcat" If either operand is a string, the result is a string –"answer " + 10 ---> "answer 10"

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Displaying Numerical Data The print() and println() methods can have numbers instead of strings for their argument –These are exampled of overloaded methods System.out.println( 6) will print the number 6. System.out.println( 6 + 9) will print the number 15.

Download ppt "©2004 Brooks/Cole Chapter 2 Variables, Values and Operations."

Similar presentations