lec5 - COMP-202 Programming Basics © 2008 Clark Verbrugge...

Info iconThis preview shows pages 1–7. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COMP-202 - Programming Basics, © 2008 Clark Verbrugge, Bettina Kemme, Jörg Kienzle Identifers • Identifers are the words a programmer uses in a program • They are used to give names to things • Example oF things that can be named with identifers: • Classes • Methods • Variables • Constants • An identifer can be made up oF letters , digits , the underscore character (_), and the dollar sign ($) • Identifers cannot begin with a digit • Java is case sensitive , thereFore Result and result are diFFerent identifers 17 COMP-202 - Programming Basics, © 2008 Clark Verbrugge, Bettina Kemme, Jörg Kienzle More on Identifers • Sometimes we choose identifers ourselves when writing a program (such as input1 , AddTwoIntegers ) • We should choose identifers that are meaningFul so that it is more readable For humans • Sometimes we are using another programmer's code, so we use the identifers that they chose (such as println() ) • OFten we use special identifers called reserved words that already have a predefned meaning in the language • A reserved word cannot be used in any other way • Examples oF reserved words: class , public , static , void • There are many others 18 COMP-202 - Programming Basics, © 2008 Clark Verbrugge, Bettina Kemme, Jörg Kienzle Identifer Exercise • Which oF the Following identifers are invalid according to the Java rules For identifers? Why are they invalid? myIdentifier _my_other_identifier yet-another-identifier $can 2for1 twoFor1 class my_class 19 COMP-202 - Programming Basics, © 2008 Clark Verbrugge, Bettina Kemme, Jörg Kienzle Variables • A variable is a placeholder for values • We can store a value in a variable • We can use the value a variable contains to compute other values (which may be stored in other variables) or make decisions • Each variable has a name • The compiler allocates memory cells to hold the value of the variable, and assigns the name chosen for the variable to these cells • We can then refer to the value stored in these memory cells using the variable name • Much easier and more practical than using the addresses of the memory cells (and less error-prone) 20 COMP-202 - Programming Basics, © 2008 Clark Verbrugge, Bettina Kemme, Jörg Kienzle Variable Declarations • Before we can store a value in a variable or use the value it contains, we have to declare it • A variable declaration is a statement that announces that we want to create and use a new variable • They can occur anywhere in a method, but they are typically at the beginning • They must indicate the name and the type of the new variable • Once a variable has been declared, we can store a value in it or use the value it contains • If you try to use a variable before it is declared, the compiler will generate an error 21 COMP-202 - Programming Basics, © 2008 Clark Verbrugge, Bettina Kemme, Jörg Kienzle Syntax for Declaring Variables 22 modiFer type identiFer = value ; (optional) (optional)...
View Full Document

This note was uploaded on 04/14/2008 for the course COMP 202 taught by Professor Verbrugge during the Winter '07 term at McGill.

Page1 / 37

lec5 - COMP-202 Programming Basics © 2008 Clark Verbrugge...

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

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