Comp 110 int "Integer"

int "Integer"

What is an int?

  • An int is a simple numerical type
  •  An int is an integer, just like in Math, with no decimals (think -1, 1, 2, 3, 4....)

Integer Literals

Below are some examples of int literals in Java:

42
110
-100

Notice int literals are just plain numbers, that optionally begin with a minus sign to indicate negative numbers, that do not contain a decimal point. They can never be fractions.

Declaration and Initialization Syntax

As for all data types, declaring and initializing an integer follows the syntax:

<type> <name> = <value>;

So, declaring and initializing int variables looks like:

int myAge = 18;
int aNumber = 3;
int roomNumber = 307;

Ints are most useful for counting.