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
myBox variable. On line 6, I’m printing out my variable by its name. The output of this program is
Line 5: I’m declaring a variable
myBox without assigning a value
Line 6: I’m assigning a value to my variable
Line 9: I’m reassigning a value
Line 10: I’m printing out my variable by its name. Output —
Line 12: I’m reassigning a value
Line 13: I’m printing out my variable by its name. Output —
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:
- 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,
bytecan hold value from -128 to 127, and
intcan 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.
- Primitive variables for floating numbers
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.
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?
byte -> short -> int -> longfloat -> double
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
pricevariable of type double has value
9.99and when we unbox it to
9exactly. 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
Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming
OS, File, and File System
Working with terminal
Welcome to Java Programming Language
Variables and Primitives in Java
Convert String to numeric data type
Input from the terminal in Java
Methods with Java
Java Math Operators and special operators
Conditional branching in Java
Switch statement in Java
Ternary operator in Java
Enum in Java
String class and its methods in Java
Loops in Java
Access modifiers in Java
Static keyword in Java
The final keyword in Java
Class and Object in Java
Object-Oriented Programming in Java
OOP: Encapsulation in Java
OOP: Inheritance in Java
OOP: Abstraction in Java
OOP: Polymorphism in Java
The method Overriding vs Overloading in Java
Array in Java
Data Structures with Java
Collection framework in Java
ArrayList in Java
Set in Java
Map in Java
Date and Time in Java
Exception in Java
How to work with files in Java
Generics in Java
Multithreading in java
Annotations in Java
Reflection in Java
Reflection & Annotations - The Powerful Combination
Run terminal commands from Java
Lambda in Java
Unit Testing in Java
Big O Notation for coding interviews
Top Java coding interview questions for SDET