Java Data Types: A Detailed Guide to Primitive and Reference Types
Meta Description (155 characters):
Learn the primitive and reference data types in Java's static typed structure with JShell examples; Optimize your code and catch errors before compilation.
🎯 What Will You Learn in This Guide?
In this guide, you will learn in detail data types, which are the basic building blocks of Java.
We'll cover all the important topics, from primitive types to reference types, literals, and the modern var keyword.
You will see how you can increase the performance of your code and catch errors before compilation by choosing the right data type.
⚙️ Java's Static Type Structure
Java is a statically typed programming language.
This requires that when defining a variable, you must specify the type of information (data type) that variable will hold.
Unlike dynamically typed languages such as PHP or Python, this convention gives us the following advantages:
- Resource Optimization: Memory usage of each data type is different. Specifying a type allows you to use system resources more efficiently.
- Early Error Detection: A compilation error is received when the wrong type of data is assigned; This way you can catch the problem before it gets into the testing phase.
There are two main categories of data types in Java:
- Primitive Types – Numbers, characters, logical values.
- Reference Types – Represents objects (e.g.
String,Integer).
💡 Prerequisites
To try the examples in this guide, you need:
- Java 11 or higher version
- JDK (Java Development Kit) installed system
- JShell tool running from terminal
To start JShell, type the following command in the terminal:
jshell
➡️ Description: This command starts Java's interactive shell.
🔢 Primitive Data Types
Primitive types hold simple values and always start with a lowercase letter. Most commonly used: int, boolean, char.
🧮 1. Integers (int)
int stores positive and negative integers (approximately −2.1 billion to +2.1 billion).
int cevap = 42;
System.out.println("Bütün soruların cevabı: " + cevap);
➡️ Explanation: the response variable is defined and its value is printed on the screen.
✅ 2. Boolean Values (boolean)
boolean can only take the values true or false.
boolean javaEglenceliMi = true;
System.out.println("Java eğlenceli mi: " + javaEglenceliMi);
➡️ Description: javaEntertainmentMi variable is set to true and printed to the screen.
🔤 3. Characters (char)
char holds a single character. The value is written in single quotes.
char ilkHarf = 'A';
System.out.println(ilkHarf);
➡️ Description: The character 'A' is stored and printed. char forms the cornerstone of the String structure.
🧩 Reference Data Types
Reference types refer to objects and start with a capital letter. The most common are: String, Integer, Boolean, Character.
🧷 1. Text String (String)
String represents strings of characters.
String selam = new String("Merhaba");
System.out.println(selam);
➡️ Description: the hello variable is assigned to a new String object. For short writing, it is generally used as follows:
String selam = "Merhaba";
🧱 2. Wrapper Classes
Provides object versions of primitive types:
| 🧩 Primitive Type | 🧱 Immersive Classroom |
|---|---|
int | Integer |
boolean | Boolean |
char | Character |
Integer cevapNesnesi = Integer.valueOf(42);
System.out.println(cevapNesnesi);
➡️ Description: Integer.valueOf() method returns an object containing the value 42.
💎 Literals (Literals)
Literals are literals written directly into the code.
📦 Primitive Type Literals
int yas = 25;
char harf = 'A';
boolean aktif = true;
🧰 String Literal
String merhaba = "Merhaba, Dünya!";
🚫 null Literal
null indicates that a reference does not point to any object.
String baslangictaNull = null;
baslangictaNull = "Artık null değil";
System.out.println("Sınıf Adı: " + baslangictaNull.getClass());
➡️ Description: accessing null causes an error; You must first assign a new value.
🧪 Local Variable Type Inference (var)
The one that came with Java 10 automatically determines the type of local variables.
var mesaj = "Selam";
System.out.println(mesaj);
➡️ Description: Java automatically detects that the message variable is String.
⚠️ var is only valid on variables within the method. If you try to use it outside the class, the compiler will throw an error.
🚫 Reserved Keywords
Some words are reserved in Java and cannot be used as variable names:
abstract, continue, default, for, break, new, return, static, while ...
Faulty example:
int new = 1; // HATA: 'new' anahtar kelime olarak saklanmıştır
🙋♂️ Frequently Asked Questions (FAQ)
- Why should I use Integer instead of int in Java?
int is fast and lightweight, while Integer offers object capabilities (methods, nullability).
- Why isn't String a primitive type?
Because String is a class; It is an object consisting of characters.
- Does var slow down the code?
No, type inference is done at compile time, it makes no difference at runtime.
- What is the difference between null and 0?
0, an int value; null is when an object does not exist.
🏁 Conclusion
In this guide, you learned about primitive and reference data types, literals, and the modern var property in Java. Understanding Java's static type system is the first step to writing more optimized and bug-free code.
Try your own examples in JShell — and start your Java development journey by applying this knowledge on the GenixNode platform today. 🚀

