They are so primitive that I keep forgetting about them
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 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 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 **/
Java has core 8 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.
- 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 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 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:
- 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.
- 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 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. Conditional branching, comparision and logical operators
8. Switch statemet and ternary operator
10. String and its methods in Java
11. Loops in Java
12. Class, Object and constructor in Java
13. Object Oriented Programming in Java
14. Encapsulation in Java
15. Inheritance in Java
16. Abstraction in Java
17. Polymorphism in Java
18. Overriding vs Overloading in Java
19. OOP Design Principles in Java
20. Array in Java
21. Data Structures with Java
22. Collection framework in Java
23. ArrayList in Java
24. Set in Java
25. Map in Java
26. LocalDate in Java
27. Exception in Java
28. IO in Java
29. Design Patterns