CH02 - Chapter 2 Java an Introduction to Computer...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 2 Java: an Introduction to Computer Science & Programming - Walter Savitch Chapter 2 ● Primitive Data types ● Strings: a class ● Assignment ● Expressions ● Keyboard and Screen I/O ● Documentation & Style Primitive Types and Simple I/O Chapter 2 Java: an Introduction to Computer Science & Programming - Walter Savitch Creating Variables ● All program variables must be declared before using them Type Variable_1, Variable_2, …; ● For example, to create three integer variables to store the number of baskets, number of eggs per basket, and total number of eggs: int numberOfBaskets, eggsPerBasket, totalEggs; Chapter 2 Java: an Introduction to Computer Science & Programming - Walter Savitch Two Main Kinds of Types in Java primitive data types ● simplest types ● cannot decompose ● values only,no methods ● Examples: int- integer double- floating point (real) char- character class types ● more complex ● composed of other types » Primitives » Class types ● Both data and methods ● Examples: SavitchIn String Chapter 2 Java: an Introduction to Computer Science & Programming - Walter Savitch Syntax Rules for Identifiers ● Do not use reserved words, such as: » “if,” » “for”, ● Composed (only) of » Letters » Digits (but not first character) » Underscore character _ . ( Note: no spaces or dots) ● No official length limit » Always finite limit, but very large » Big enough for reasonable names. ● case sensitive! » junk , JUNK , and Junk are different identifiers » be careful typing! See Appendix 1 for full list) Chapter 2 Java: an Introduction to Computer Science & Programming - Walter Savitch Primitive Numeric Data Types ● integer » whole number » examples: 0, 1, -1, 497, -6902 » four data types: – Byte – Short – Int – long ● floating-point number » includes fractional part » examples: 9.99 , 3.14159 , -5.63 , 5.0 » Note: – 5.0 is a floating-point number even though the fractional part happens to be zero. » two data types: – Float – double Chapter 2 Java: an Introduction to Computer Science & Programming - Walter Savitch The char Data Type ● Stores single “ printable ” character ● Example: char answer = `y`; System.out.println(answer); Chapter 2 Java: an Introduction to Computer Science & Programming - Walter Savitch Primitive Data Types Type Name Kind of Value Memory Used Size Range byte integer 1 byte-128 to 127 short integer 2 bytes-32768 to 32767 int integer 4 bytes-2,147,483,648 to 2,147,483,647 long integer 8 bytes-9,223,372,036,854,775,808 to 9,223,374,036,854,775,808 float floating point 4 bytes +/- 3.4028… x 10 +38 to +/- 1.4023… x 0-45 double floating point 8 bytes +/- 1.767… x 10 +308 to +/- 4.940… x 0-324 char single character (Unicode) 2 bytes all Unicode characters boolean true or false 1 bit not applicable Chapter 2 Java: an Introduction to Computer Science & Programming - Walter Savitch Which Ones to Know for Now int...
View Full Document

This note was uploaded on 04/17/2008 for the course COMP SCI 201 taught by Professor Durga during the Spring '08 term at Wentworth Institute of Technology.

Page1 / 58

CH02 - Chapter 2 Java an Introduction to Computer...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online