Variables and Primitives in Java

First, let’s talk about variables, and then we will slowly jump to primitives in java.


A variable is a container that can hold a piece of data. We can think of the variable as a box where we can put different staff and take them when we need them. Also, we can put a label on our box so we don’t get confused with other boxes. Finally, we can get different types of boxes for different goods. For books, we will have one, for clothes we will have another one.

In programming, we can create such a box — variable. Variables have a name and can hold a value. We can assign a value to our variable while creating it and reassign a different value in the next steps of our program. To get a value of a variable, we can use a variable name.

In Java, before using a variable we need to create one. We refer to a variable creation as a variable declaration.

In the above code, I’m declaring a variable myBox on line 5 and in the same line(statement) I’m assigning a value 3 to myBox variable. On line 6, I’m printing out my variable by its name. The output of this program is 3.

Line 5: I’m declaring a variable myBox without assigning a value

Line 6: I’m assigning a value to my variable myBox as 9

Line 9: I’m reassigning a value 103 to myBox

Line 10: I’m printing out my variable by its name. Output — 103

Line 12: I’m reassigning a value 77 to myBox

Line 13: I’m printing out my variable by its name. Output — 77

So far we have learned:

  • We need to declare variables first in order to use it.
  • We can reassign a value of variables.
  • We use variables by their name.

Now, let’s talk about variable types. We have talked about a different box for different goods. The same logic goes here. So far we used a variable for numbers only but there are other data types also.

int here is the data type of our variable. Int is a specific data type in java for whole numbers. count is an identifier(name), and 99 is a value.

Java is a strongly-typed language. It means we need to specify a type of our variable while creating it.

Let’s see more examples. If we want to have a variable that can hold words or sentences, we can use String. It is a data type that can hold a sequence of characters. String is not a primitive variable, it’s an object. More about objects in the future.

As you can see the way we work with String variables is the same, but the data type of variable got changed. Instead of int we have String. Because we wanted our variable to hold words(better to say string — a sequence of characters).

We can put comments in our source code and a compiler will not consider them as part of the code and it will just ignore it. There are three ways of making comments in Java:
// this is comment
/* this is comment */
/** this is comment **/


They are so primitive that I keep forgetting about them :)

Java has 8 core primitive data types. We refer to them as primitives. They are basic data types in Java.

We can divide them into four categories:

  1. Primitive variables for whole numbers byte, short, int, long . They can only hold a whole number. Why do we need 4 types of variables for whole numbers? Why don’t have only one? That’s a really good question. Each type can hold a different range of values. For example, byte can hold value from -128 to 127, and int can hold -2147483648 to 2147483647. Of course, they will take a different amount of memory. Most of the time we just go with int , but if you are developing a program for a device where memory is a concern, you might consider using a smaller type when you know the approximate value of a variable.
  2. Primitive variables for floating numbers flaot, double
  3. Character char. It can store a single character. char ch = 'a'; That’s how we declare a char variable. Note, char value goes between the single quotes. We know that every single char has a corresponding number value from the ASCII table.
  4. Boolean boolean. Boolean can hold only true or false. It is used with conditional branching where a program should decide which path to take. boolean isWhite = true; That’s how we declare a boolean variable.

On line 9: We put L after our value of long because when we just type the value by default it will be considered as int and when we have a value bigger than int can take, we need to specify that it’s actually long by adding L after the value.

On line 12: We put F to tell that value is for float and not for double. By default, it will be considered double.

Primitive type casting

Sometimes, we need to assign a value from one primitive type to another.

  • The smaller primitive data can be assigned to bigger primitive data automatically.
  • When we try to put a bigger data type to a smaller one, it needs explicit casting.

What primitive is bigger and what is smaller?

Above, it’s a flow of their sizes.

Examples of implicit casting:

  • we can see byte can be assigned to an int automatically. Because java knows that byte cannot hold a bigger number than int so it can safely just assign.
  • long can be assigned to double automatically as well.

Examples of explicit casting:

  • if we will try to assign an int to a byte, it will not compile. Even though our int has a value that for sure can fit into a byte, a compiler doesn’t know the actual value of a variable. Potentially int can hold a bigger number than byte so we cannot assign to byte automatically.
  • If we really want to assign an int to a byte, we can unbox it by adding (byte) in front of the value. By doing so we are telling to compiler I know an int can hold a bigger number but trust me, in this case, it’s not so it’s safe to assign.
  • Be extremely careful when unboxing floating numbers. In our case price variable of type double has value 9.99 and when we unbox it to int the wholeProce will have 9 exactly. It will always truncate the actual value.

String and concatenation

  • We can concatenate(add) strings by using + operator.
  • If we concatenate the String data type with other data types output will be String.

Primitives in Java



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store