Variables and Primitives in Java

They are so primitive that I keep forgetting about them

Variable is a container that can hold a piece of data

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


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 file

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.

in file

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

The flow of our program and values of myBox variable

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 is the same, but the data type of variable got changed. Instead of int we have String. Because we wanted a variable that can hold words(better to say string — a sequence of characters). Different data types should go to different types of variables.

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 **/

in file


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

There are 8 primitives 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.
  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 used with conditional branching where a program should decide which path to take. boolean isWhite = true; That’s how we declare a boolean variable.
in file

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 as double.

Autoboxing and Unboxing

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

  • Autoboxing — smaller primitive data can be assigned to bigger primitive data automatically.
  • Unboxing — when we try to put a bigger data type to smaller, it needs explicit casting.

What primitive is bigger and what is smaller?

byte -> short -> int -> long -> float -> double

Above, it’s a flow of their sizes.

Examples of autoboxing:

  • 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.

Examples of unboxing:

it does not compile
  • if we will try to assign an int to 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.
Unboxing floating numbers
  • 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 String data type with other data type output will be String.

Primitives in Java

This article is part of the series of articles to learn Java programming language from Tech Lead Academy:
1. Introduction to programming
2. OS, File, and File System
3. Working with terminal
4. Welcome to Java Programming Language
5. Variables and Primitives in Java
6. Methods with Java
7. Java Math Operators and special operators
8. Conditional branching in Java
9. Switch statement in Java
10. Ternary operator in Java
11. Enum in Java
12. String class and its methods in Java
13. Loops in Java
14. Access modifiers in Java
15. Static keyword in Java
16. The final keyword in Java
17. Class and Object in Java
18. Object Oriented Programming in Java
19. OOP: Encapsulation in Java
20. Inheritance in Java
21. Abstraction in Java
22. Polymorphism in Java
23. Overriding vs Overloading in Java
24. OOP Design Principles in Java
25. Array in Java
26. Data Structures with Java
27. Collection framework in Java
28. ArrayList in Java
29. Set in Java
30. Map in Java
31. LocalDate in Java
32. Exception in Java
33. IO in Java
34. Design Patterns
35. Generics in Java
36. Multithreading in java
37. JUnit
38. Big O Notation for coding interviews
39. Top 17 Java coding interview questions for SDET

Software Developer, Java Instructor