Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

An introduction to identifiers and data types in java programming. It covers the rules for naming identifiers, the different primitive types (boolean, char, int, long, float, double), and the differences between int and long, float and double. It also explains the concept of integer division and how to fix it through typecasting.

Typology: Slides

2012/2013

Uploaded on 04/23/2013

sarmistha
sarmistha 🇮🇳

3.8

(22)

113 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Identifiers and types
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

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/