Skip to main content

Section 1.2 Data Types & Operators

Subsection 1.2.1 Brief Review of Data Types

Table 1.2.1. Data Types
Data Type Can Store Storage Size
double Floating point numbers, with up to 8 significant digits of precision 8 bytes
float Floating point numbers, with up to 7 significant digits of precision 4 bytes
long Whole numbers in range [-9223372036854775807, 9223372036854775807] 8 bytes
int Whole numbers in range [-2147483647, 2147483647] 4 bytes
short Whole numbers in range [-32767, 32767] 2 bytes
byte Whole numbers in range [-127, 127] 1 byte
char Single alpha-numeric or punctuation character 2 bytes
boolean True or False 1 byte

Note 1.2.2.

The ones that are bolded above are the ones used most commonly.
A String in Java is not a primitive datatype (Strings are objects), but they are a built-in datatype that is commonly used.
// Examples of declaring and initializing each datatype

double myDouble = 543.222987;
float myFloat = 983.2f; // note that you need the 'f' here to specify it as a float
long myLong = 456778866554433l; // note that you need the 'l' here to specify it as a long
int myInt = 35698742;
short myShort = -3566;
byte myByte = 120;
        

Subsection 1.2.2 Built-in Mathematical Operators

  • Addition: +
    • String, Integer, and Floating-point values
  • Subtraction: -
    • Integer and Floating-point values
  • Division: /
    • Integer and Floating-point values
  • Multiplication: *
    • Integer and Floating-point values
  • Modulo (remainder from integer division): %
    • Integer and Floating-point values
  • Increment (adds one to variable): ++
    • Integer and Floating-point values
  • Decrement (subtracts one from variable): --
    • Integer and Floating-point values
Division is a special case in Java. If you divide two integers, you will get an integer (whole number) result, even if the two numbers don’t divide evenly.
  • \(x = 5/2\) = \(2\)
To perform floating point division, cast either the numerator or the denominator to a double or float.
  • \(x = (float)5/2\) = \(2.5\)
Ensure that \(x\) can store a floating point result. Otherwise, you will get an error if \(x\) is declared as an int.

Subsection 1.2.3 Type Conversion

You can always store a smaller numeric type in a bigger type, that conversion will happen automatically:
int x = 3;
double y = 55.7;
y = x; // no problem here!
        
But you can’t go the other way without an explicit cast:
int x = 3;
double y = 55.7;
x = y; // this will generate an error because you would lose the .7 by doing this
// and Java won't do it for you, unless you tell the compiler you really want to
x = (int)y; // this works - you are telling Java that it's okay to lose the .7
        

Subsection 1.2.4 Exercise 1

Complete this exercise to make sure you understand common datatypes by replacing all the question marks with the appropriate dataype. Run the program to test it and make sure it works.

Subsection 1.2.5 Exercise 2

To practice a few other common datatypes, declare and initialize 3 variables so that the print statements print out sensible data.

Subsection 1.2.6 Exercise 3

Create and populate variables that will make the print statements run without error and generate sensible output. Do not edit the print statements. Test that your solution is working. An example of correct output is:
The address of the house is: 3 William St., Unit 5
It is listed for sale at $368000
A 10% downpayment would be $36800.0
        

Subsection 1.2.7 Exercise 3

Create and populate variables that will make the print statements run without error and generate sensible output. Do not edit the print statements. Test that your solution is working. An example of correct output is:
You have attempted of activities on this page.