Featured Articles:

Wednesday, September 16, 2026

Variables In Coding Explained: int, float, double, And Every Type In Between


Welcome to another blog post about the absolute bread and butter of programming: variables in coding. If you're just starting out, you've probably already typed something like int score = 0; without really knowing why it works, or why it had to be int and not something else. Alright, that's fair; hardly anybody explains it well the first time around. 

Maybe I can shine some light on this topic.



What Is a Variable, Really?

A variable is a labeled box in your computer's memory. You give the box a name, you decide what type of thing it's allowed to hold, and you put a value in it - what value? Well, that varies! Hence the name variable. That's it. That's the whole concept.

Here's the part beginners skip past: the "type" isn't decoration. It tells the computer exactly how many bytes of memory to set aside and how to interpret whatever bits get stored there. A variable data type is basically a contract, you're telling the compiler "this box will only ever hold a whole number" or "this box holds text" and the compiler holds you to it.

variable name  →  memory box  →  data type decides size/shape  →  value stored inside

Change the value later? Totally fine, that's the point of a variable instead of a constant. Change the type later? Depends on the language, and in most beginner-friendly languages like C# or Java, no, not without some awkward conversion gymnastics.


The Common Variable Data Types (int, float, double, bool, string)

These are the ones you'll type a hundred times a day once you get going. Understanding types of variables in programming starts here, because these five cover probably 90% of what you'll ever need as a beginner. Later I'll go over the limitations of each, and which one you should use when.

  • int - an integer: whole numbers, no decimals. Player health, ammo count, number of enemies on screen. If you're not dealing with fractions, it's probably an int.
  • float - a floating-point number: a decimal number with lower precision. Movement speed, rotation values, anything physics-related. Cheaper on memory than a double, which is why game engines lean on it heavily.
  • double - double-precision floating-point number: also decimals, but double the precision (hence the name). Used when accuracy actually matters, like scientific calculations or financial math where rounding errors would be a real problem.
  • bool - a boolean: true or false. Nothing else. Is the door locked, yes or no? Is the player jumping? Is the game paused? That's a bool's whole job description.
  • string - a string of characters: text. Names, dialogue, UI labels. Technically not always a "primitive" type depending on the language, but every beginner treats it like one, and honestly, that's fine. A string variable "1" is not the same as an int variable "1". Sometimes you can "convert" (cast) types to match them up. 

Quick gut-check on float vs double, since it trips people up constantly: use float when you don't need extreme precision and want to save memory (most gameplay code), use double when precision actually matters and memory isn't the bottleneck (scientific or financial calculations). In Unity specifically, you'll be reaching for float way more often than double.


But Wait... There's More! The Not-So-Common Ones:

Once you've got the big five down, there's a second tier of variable data types that don't show up as often, but when you need them, nothing else will do.

  • char - a single character. Not a word, not a sentence, one letter or symbol. Useful for parsing input character-by-character, like checking if a key press was a letter or a number. The first time I used char was in my game "Hangman X", when you typed in a letter, it would check with a loop if the character existed in the secret word.
  • byte - a small whole number, way smaller range than an int. You'll see this in memory-tight situations, like storing an RGB color channel (0-255 fits perfectly in a byte) or networking code where every bit sent over the wire costs something.
  • long - like an int, but for numbers so big a regular int can't hold them. Think file sizes in bytes, timestamps, or a save system tracking total playtime in milliseconds across a very long game.
  • short - the opposite of long, a smaller-range whole number. Rare in beginner code, more common in embedded systems or old-school game dev where every byte of memory mattered.
  • arrays and lists - not a "type" in the traditional sense, but a variable that holds multiple values instead of one. An inventory system holding 20 items isn't 20 separate variables; it's one array variable holding 20 slots for example.


Local Variables, Global Variables, And Scope

This is where a lot of the confusion around local vs global variables actually starts, and it's less about the type and more about where you declared the thing.

A local variable only exists inside the method or function it was declared in. Once that function finishes running, the variable is gone, memory freed, done. This is usually a good thing, it keeps your code tidy and stops random pieces of your program from stepping on each other's toes.

A global variable (or in C# terms, often a static or public field) sticks around for the life of the program and can be accessed from pretty much anywhere.

private void DoSomething() 
{ int _localScore = 10; // exists only inside this function } // localScore doesn't exist out here anymore public static int GlobalScore = 0; // exists everywhere

The general rule beginners should walk away with: default to local variables. Only go global when you genuinely need something accessible everywhere, like a game manager tracking score across scenes (and even then I wouldn't use it). Overusing global variables is one of the fastest ways to make a small project impossible to debug!


Const And Static, The Quiet Achievers

Two more worth knowing, since they change how a variable behaves rather than what it holds:

  • const - a variable whose value is locked in at compile time and can never change. Gravity constants, max player count, the number of seconds in a minute, someone's birthday, etc. If it should never, ever change while the program runs, make it a const.
  • static - belongs to the type itself rather than any individual object made from it. There's only ever one copy, shared everywhere. Useful for counters, shared settings, or singleton-style managers.


So Which Variable Type Should You Actually Use?

Here's the no-fluff version, the gut check you can run through in your head:

  • Whole number, no decimals → int
  • Decimal number, gameplay-related → float
  • Decimal number, precision matters → double
  • True or false? Yes/no or on/off → bool
  • Text → string
  • Single character → char
  • Small number, memory-tight → byte
  • Huge number → long
  • Multiple values in one variable → array or list
  • Value should never change → const
  • Needs to be shared everywhere → static

Picking the right types of variables in programming isn't about memorizing a chart, it's about asking "what does this actually need to hold, and how long does it need to exist for?"

 Answer those two questions and the correct type basically picks itself.



How Much Can Each Variable Data Type Actually Hold?

Here's another thing about a variable data type that trips people up once they get past the basics: each one has a hard ceiling. Pick a type that's too small for the number you're trying to store, and you don't get a gentle warning, you get overflow bugs, silently wrong values, or in some languages a straight-up crash. Knowing the actual range of each variable data type up front saves you from chasing a bizarre bug three weeks later.

These ranges are standard across C# and most C-family languages (Unity's scripting language is C#, so this applies directly to what you'll be writing):

  • byte :

    0 to 255 (unsigned, no negative numbers at all). That's just 8 bits of storage, which is exactly why it's perfect for something like a color channel that maxes out at 255 anyway, or someone's age (unless you're referencing a vampire).

  • short :

    -32,768 to 32,767. 16 bits, signed. Big enough for most small counters, way too small for anything like a score that climbs into the millions.

  • int :

    -2,147,483,648 to 2,147,483,647 (roughly ±2.1 billion). 32 bits. This is the one you'll use constantly, and that ceiling is high enough that you'll rarely think about it, until you're building something like an idle game with numbers that spiral out of control.

  • long :

    roughly -9.2 quintillion to 9.2 quintillion. 64 bits. Essentially "the number is so big it broke int," which is why timestamps and file sizes lean on it.

  • float :

    about ±3.4 x 10^38, but only accurate to roughly 6-7 significant digits. The range is huge, the precision is the limiting factor, not the size.

  • double :

    about ±1.8 x 10^308, accurate to roughly 15-16 significant digits. Massively bigger range than float, and way more precise, which is exactly why it costs more memory.

Thing to note: a game with a health system that goes from 0 to 100 doesn't need to think about any of this, an int or even a byte handles it just fine. But an incrmental /idle game where numbers climb into the trillions? That's a conversation about whether even long is enough, or whether you need a custom big-number system entirely. Range isn't just trivia, it directly shapes what kind of game systems you can safely build.


Wrapping Up

Variables feel like a small, boring topic right up until you realize everything in programming is built on top of them. Get comfortable with the common types, know when to reach for the less common ones, and understand the difference between local and global scope, and you've already cleared one of the biggest hurdles in learning to code! 

If you feel like something needs to be added, or questions? Let me know in the comments!

 ~happy Coding!



No comments:

Post a Comment