Types and variables

posted by: Ms. Martin 15 September 2009 No Comment

We discussed Java’s primitive number types — int and double — and how to write expressions with them.  Most of the operators we discussed were familiar but there were still a couple of surprises to be had.  First, the modulo operator (%) was new to most.  It calculates the remainder of integer division and you can see examples here.  We also learned that integer division truncates (it doesn’t round) such that 13 / 5 would evaluate to 2.

We then talked about variables as a way to name spots in memory to store information for later.  We learned that we always have to declare a variable before giving it a value by associating it with a type.  Mixing types results in errors.  For example:

String zach = "Ryan";  // a variable named zach refers to the text Ryan
int bar;
bar = 10;
double foo = 10.2;

We can use variables anywhere we would use a literal value of the corresponding type.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>