03-Expressions

# 03-Expressions - TheArtandScienceof CHAPTER 3 ERICS.ROBERTS...

This preview shows pages 1–6. Sign up to view the full content.

The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Jav a Expressions C H A P T E R 3 “What’s twice eleven?” I said to Pooh. (“Twice what?” said Pooh to Me.) “I think that it ought to be twenty-two.” “Just what I think myself,” said Pooh. —A. A. Milne, Now We Are Six, 1927 3.1 Primitive data types 3.2 Constants and variables 3.3 Operators and operands 3.4 Assignment statements 3.5 Boolean expressions 3.6 Designing for change

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Expressions in Java The heart of the Add2Integers program from Chapter 2 is the line The n1 + n2 that appears to the right of the equal sign is an example of an expression , which specifies the operations involved in the computation. An expression in Java consists of terms joined together by operators . Each term must be one of the following: A constant (such as 3.14159265 or "hello, world" ) A variable name (such as n1 , n2 , or total ) A method calls that returns a values (such as readInt ) An expression enclosed in parentheses int total = n1 + n2; that performs the actual addition.
Primitive Data Types Although complex data values are represented using objects, Java defines a set of primitive types to represent simple data. Of the eight primitive types available in Java, the programs in this text use only the following four: int This type is used to represent integers, which are whole numbers such as 17 or 53. double This type is used to represent numbers that include a decimal fraction, such as 3.14159265. In Java, such values are called floating-point numbers ; the name double comes from the fact that the representation uses twice the minimum precision. char This type represents a single character and is described in Chapter 8. boolean This type represents a logical value ( true or false ).

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Summary of the Primitive Types A data type is defined by a set of values called the domain and a set of operations . The following table shows the data domains and common operations for all eight of Java’s primitive types: Type short int long float double char boolean 8-bit integers in the range –128 to 127 16-bit integers in the range –32768 to 32767 32-bit integers in the range –2146483648 to 2146483647 64-bit integers in the range –9223372036754775808 to 9223372036754775807 32-bit floating-point numbers in the range ± 1.4 x 10 -45 to ± 3.4028235 x 10 -38 64-bit floating-point numbers in the range ± 4.39 x 10 -322 to ± 1.7976931348623157 x 10 308 16-bit characters encoded using Unicode the values true and false The arithmetic operators: + - * / % add subtract remainder divide multiply = = < != <= >= equal to less than greater or equal less or equal not equal > greater than The arithmetic operators except % The relational operators: The relational operators The relational operators The logical operators: add || or ! not Domain Common operations byte
Constants and Variables The simplest terms that appear in expressions are

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/27/2009 for the course COMP SCI 1MD3 taught by Professor Various during the Winter '07 term at McMaster University.

### Page1 / 23

03-Expressions - TheArtandScienceof CHAPTER 3 ERICS.ROBERTS...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online