What is the use of int()?

Hello guys. I just started to learn to code in Processing. Though I’m finding it hard to understand the code int() (yes the one with the parenthesis). What it is for? Why do we need to use this code? And in what situation? I tried to read the description on processing.org but it isn’t helping much.

Here is an example on the web:

char c = 'E';
i = int(c);
println(c + " : " + i);  // Prints "E : 69"

It would be really good if somebody can share some explanation. Thank a lot.

1 Like

When you want a computer to remember something, you have to tell it what sort of a thing you want it to remember. If you want it to remember a character, you use a char. If you want it to remember some words, you use a String. If you want it to remember a true/false thing, you use a boolean.

When it comes to numbers, there are two main kinds. Whole numbers (0, 1, 2, 3…) and decimal numbers (like 783.2835). Whole numbers you can store in an int, and decimal numbers get stored in a float.

Anyway. Sometimes it is useful to convert one kind of remembered variable into a different kind. You do this by casting (changing the type) of the data in question. The int() function is what does this conversion for you when you want to convert something into a whole number. Basically, it takes a single character, and converts that character into a number representation. It can do this because, at the lowest level, both characters and ints are stored in binary - a sequence of 0’s and 1’s. Normally it would treat a character’s 0’s and 1’s as a character, because that’s what you told it to treat them as. But when you cast those 0’s and 1’s to a different type, you can have it treat them as some other kind of data.

You can convert the other way too! Try casting an int to a char (use the char() function), or try casting a float to an int!



  • char is 1 of the 8 Java primitive datatypes.
  • char is internally stored as a number value like the other 7 primitive datatypes.
  • All arithmetic & bitwise operations are allowed over a char datatype.
  • However, char got 3 caveats which make it diff. from the other primitives:
  1. char is a 16-bit (2-byte) unsigned datatype. Meaning it can’t represent negative values and its max value is 65535 (2^16 - 1).
  2. When a char and String are used together as operands for the operator +, the char is concatenated as a UTF-16 character rather than its actual numerical value.
  3. When we println() a char, it is displayed as a UTF-16 character rather than its numerical value.

For all other cases, a char behaves as a whole numerical value.


Thank a lot for the detailed explanation.

Thank you so much. This helps a lot.

Well, we don’t actually need it for primitive datatype conversions. :smirk:

We can simply use an (int) cast operator instead, which is faster btW: :horse_racing:
int i = int(c); :arrow_right: int i = (int) c;

But the function int() is still necessary for converting a String into an int though. :prayer_beads:

Also, int() is gr8 for converting an entire array of some other type into an int array. :cowboy_hat_face:

1 Like

Besides the unary cast operator (int) and PApplet’s converting function int(), there are also these 3 notable int conversion functions below: :currency_exchange:

  1. round(): Processing.org/reference/round_.html
  2. floor(): Processing.org/reference/floor_.html
  3. ceil(): Processing.org/reference/ceil_.html

Addendum: B/c Java automatically upcasts the primitives byte, short and char to int when we do any arithmetic or bitwise operation on them, we absolutely don’t need to convert any of them to int when assigning them to an int variable: :flushed:

So rather than char c = 'E'; int i = (int) c;
Just do it straight like this w/o any conversion: char c = 'E'; int i = c;


Gee thanks for all of that information bro :joy: Though it gonna take me a while to digest all or them :no_mouth: