Docsity

Identifiers and Types: Understanding Variables and Data Types in Java, Slides of Java Programming

Partial preview of the text

Download Identifiers and Types: Understanding Variables and Data Types in Java and more Slides Java Programming in PDF only on Docsity!

Identifiers and types

Announcements

Emails in office hours, feel free to set up meeting via email JavaScript vs Java?

Identifiers

Identifiers

An identifier is the name of a variable (or object, class, method, etc.) int sum type identifier

  • Case sensitive
  • Must use only letters, numbers or _
  • Cannot start with a number
  • (Some reserved identifiers)

Identifiers

  1. james parker
  2. BoByBoY
  3. x
  4. 3x
  5. x_______
  6. _______x
  7. Home.Class
  8. Five%
  9. x- Which identifiers are valid?

Identifiers

boolean

Many other languages allow conversion between integer and boolean True = 1 and false = 0 Java does not allow this

int vs long?

int - Whole numbers in the approximate range: -2.14 billion to 2.14 billions ( 9 ) long - Whole numbers in the approximate range: -9.22 quintillion to 9.22 quintillion ( 18 ) Using int is standard (unless you really need more space, for example scientific computing)

float vs double?

float is now pretty much obsolete. double takes twice as much space in the computer and is 1) more accurate and 2) more precise Bottom line: use double (unless for a joke)

float and double

Both stored in scientific notation double x = 2858291; Computer's perspective: x = 2.858291e or x = 2.858291 * 10 6

Numerical analysis

Field of study for (reducing) computer error See: SubtractionError.java Can happen frequently when solving system of linear equations

int or double?

If you are counting something (money), use int If you are dealing with abstract concepts (physics), use double int doesn't make “rounding” mistakes Non-primitive types exist for both int and double for more expression/precision

Primitive type hierarchy

int x; double y; x+y int x; int y; x/y Converted to double Not converted (still int)

Integer division

See: SimpleDivision.java Can be fixed by typecasting: (double)1/