02_variables_flow_of_control

02_variables_flow_of_control - Java 211 - Lecture II...

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

View Full Document Right Arrow Icon
Java 211 - Lecture II Yosef Mendelsohn Variables A few things to remember about variables: - Choose your identifiers wisely: - They should always begin with a lower case letter - You can not have spaces between words. Typically, separate words are separated by either an underscore ‘_’, or by capitalizing every word: - int myFirstVariable = 3; - You must avoid using “ reserved keywords ”, that is, words that are reserved by the Java language. These include words like ‘main’, ‘public’, ‘class’, to name but a few - Variables in Java are strongly typed . This means that if a variable is declared as an ‘int’, you cannot assign the value 3.14 to that variable. Doing so will give you a type mismatch . Therefore, when you have an assignment statement, the final result of the right-side must match the declared type of the variable on the left side. - int y = 3 + 4.5; // y will be set to 7 Good or Bad? int x1 int x 2; float TotalAmount; Constants - Sometimes it is convenient to assign a value to an identifier, and you do NOT want the value to change. Perhaps you are storing the value of pi in a geometry program… - final float PI = 3.1415; - The value of PI can not be changed - Note that the identifier is in capitals. This is a convention for constants. - Using constants can also be helpful where you use a value multiple times in a program, and that value should not ever change. More on Data Types - There are 8 “primitive data types” in Java. - Four integer types: int, short, byte, long - Two floating types: float, double - One character type: char
Background image of page 1

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

View Full DocumentRight Arrow Icon
- One boolean type: boolean - Each data type requires a different amount of memory (E.g. byte=8 bits and can range from –128 to 128; float requires 32 bits and ranges from about –3e48 to +3e48) - In general, err on the side of caution. For example, unless you are absolutely certain that your value will be between –128 and +128, consider using an int as your data type. - Literals : When you assign an explicit value to a variable in your program, it is called a ‘literal’. In all the programs we have seen to date, all our values have been stored as literals. By default, Java assumes that all literals are of data type ‘int’ or ‘double’ unless indicated otherwise. You can indicate a different data type by appending a letter to the end of the value: final float PI = 3.1415F; Character Data Type (char) You can declare variables that store single characters. - There are various character sets that can be used, the most popular of which is the ASCII character set. This includes: Uppercase letters, lowercase letters, punctuation, digits, symbols, special characters (newline, null, end of file, etc), but is limited to only 256 characters. -
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/15/2011 for the course CSC 211 taught by Professor Stella during the Winter '09 term at Ill. Chicago.

Page1 / 12

02_variables_flow_of_control - Java 211 - Lecture II...

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

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