Docsity

Continue Declarations and Data Types - Lecture Notes | CS 139, Study notes of Computer Science

Toggle sidebar
bg1
Lecture Notes for Sept 9, 2004 (Wednesday).
Continue declarations and data types
Primitive types
int, long, short, byte โ€“ integer space of different sizes โ€“ when in doubt use int
1, 23, -459
float and double โ€“ rational numbers โ€“ float 7 positions, double 15 positions
3.1345, -55.000
char โ€“ character representation โ€“ See chart Appendix C
โ€˜5โ€™, โ€˜xโ€™, โ€˜ โ€˜
boolean โ€“ values (and reserved words) of true and false
true, false
Reference type โ€“ provides a user defined or java library means of adding abstract data types
String โ€“ special built in class to represent character strings
โ€œThis is a string of charactersโ€, โ€œ\nโ€, โ€œ\tโ€, โ€œ\โ€โ€ ๏ƒŸ see escape character chart
All variables used, must be declared at the beginning of the method in which they are used. It is a
way of documenting the code.
An acceptable abbreviation for the declaration is:
int numberOne, numberTwo, numberThree; // note the space after each comma
OR
int numberOne,
numberTwo,
numberThree; // this format lets you describe each variable
Operations and Operators
Two meanings of +;
+ - addition of numbers
+ - concatenation of Strings
How does java decide? Think of the lab assignment
Context โ€“ java is strongly typed.
Automatic conversion (widening not narrowing)
Order of evaluation - + is evaluated from left to right.
Ex: 75 + 89 + 62 Only one operation can be done at a time.
So when we mix the types (demo program):
pf2

Partial preview of the text

Download Continue Declarations and Data Types - Lecture Notes | CS 139 and more Study notes Computer Science in PDF only on Docsity!

Lecture Notes for Sept 9, 2004 (Wednesday).

Continue declarations and data types

Primitive types

int, long, short, byte โ€“ integer space of different sizes โ€“ when in doubt use int

float and double โ€“ rational numbers โ€“ float 7 positions, double 15 positions

char โ€“ character representation โ€“ See chart Appendix C

โ€˜5โ€™, โ€˜xโ€™, โ€˜ โ€˜

boolean โ€“ values (and reserved words) of true and false

true, false

Reference type โ€“ provides a user defined or java library means of adding abstract data types

String โ€“ special built in class to represent character strings

โ€œThis is a string of charactersโ€, โ€œ\nโ€, โ€œ\tโ€, โ€œ\โ€โ€ ๏ƒŸ see escape character chart

All variables used, must be declared at the beginning of the method in which they are used. It is a

way of documenting the code.

An acceptable abbreviation for the declaration is:

int numberOne, numberTwo, numberThree; // note the space after each comma

OR

int numberOne, numberTwo, numberThree; // this format lets you describe each variable

Operations and Operators

Two meanings of +;

+ - addition of numbers

+ - concatenation of Strings

How does java decide? Think of the lab assignment

Context โ€“ java is strongly typed.

Automatic conversion (widening not narrowing)

Order of evaluation - + is evaluated from left to right.

Ex: 75 + 89 + 62 Only one operation can be done at a time.

So when we mix the types (demo program):

System.out.println ("8 plus 5 is " + 8 + 5); System.out.println (8 + 5 is โ€œ8 plus 5โ€); Other operations = assignment

  • subtraction
  • multiplication / division (how is integer division special?) Operator precedence โ€“ see chart D. Generally assignment is done last. Within levels, usually left to right. Parentheses alter the order of operations. Homework assignment next week to practice with operations and lab tomorrow will do likewise. Interactive programs Book describes a Scanner class. This is in an upcoming release of java and is not available on our system. We will be using a class called Keyboard. Keyboard will need to be downloaded to your working directory. Only the class need be downloaded. Keyboard provides methods that return the values entered via the keyboard. The method waits until the Enter key is pressed, then evaluates the input. If it is not correct, an error message is produced. See example: