Presentation is loading. Please wait.

Presentation is loading. Please wait.

COM S 207 Type and Variable Instructor: Ying Cai Department of Computer Science Iowa State University

Similar presentations


Presentation on theme: "COM S 207 Type and Variable Instructor: Ying Cai Department of Computer Science Iowa State University"— Presentation transcript:

1 COM S 207 Type and Variable Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu

2 8 Primitive Data Types Integer types byte (8 bits): [ -2^7, 2^7-1], i.e., [-128, 127] short (16 bits): [ -2^15, 2^15-1], i.e., [-32,768, 32,767] int (32 bits) : [ -2^31, 2^31-1] long (64 bits): : [ -2^63, 2^63-1] Float types float (32 bits) double (64 bits) Other types boolean (1 bit)  true / false char (16 bits)  ‘a’, ‘A’,..., ‘1’, ‘2’

3 Variable: A named space that stores values of one particular type Declare a variable (give a type and a name) Ask for a space and give it a name In Java, each variable must have a type Store a value (save) Get the stored data (recall) Examples int numberOfStudents; //declare a variable numberOfStudents = 10; //save the data in the space System.out.println(numberOfStudents); //recall

4 Declaring a variable When declaring a variable, you specify a name, a type, and often an initial value

5 Naming a variable Name should describe the purpose “canVolume” is better than cv Use five simple rules Use only letters, digits or the underscore (_)  Don’t start with a digit Separate words with “camelHump” notation Variable names are case-sensitive Don’t use reserved “java” words (See App.B) Variables start with lower case, class names with upper case (by convention, not a rule)

6 Legal and illegal variable names in Java

7 Assign value to a variable

8 Common Error Undeclared variables You must declare a variable before you use it double canVolume = 12 * literPerOunce; double literPerOunce = 0.0296; Uninitialized variables You must initialize a variable before you use it int bottles; int bottleVolume = bottles * 2;

9 Other Frequently Used Types String String courseName = “COMS 207”; String studentName = “John Scott”; System.out.println(courseName);

10 Announcements Read chapter 2 (2.1, 2.2., 2.3) Recitation this week Homework 1 Posted on blackboard Use blackboard to submit answers


Download ppt "COM S 207 Type and Variable Instructor: Ying Cai Department of Computer Science Iowa State University"

Similar presentations


Ads by Google