DEV Community

Cover image for Understanding Java Data Type: Part 3
Cal Afun
Cal Afun

Posted on

Understanding Java Data Type: Part 3

Introduction

In today's blog we conclude on the Understanding Java Data Type series. We will be covering char and boolean types in java. So without further delay let's get into it.

Char In Java

In Java, it is useful to think of a char as a small box that can hold one number between 0x0000 and' 0xFFFF` (these numbers are written in hexadecimal). Technically speaking, a Java char is a single 16-bit code unit from UTF-16.
Many everyday characters fit in one box (A, π, ™, etc.).
Some modern characters (most emoji, rare symbols) don’t fit in one box—they need two boxes (called a surrogate pair). That’s why a char is not always “one visual character.” Think of it this way, certain symbols are too large to fit into char's single box hence they bring a second box to help accommodate them. (Hence, they become surrogate pairs). Once they become surrogate pairs they are now strings but not char.
Look at this example;


char a = 'A'; // OK: 'A' fits in one 16-bit unit
char pi = '\u03C0'; // OK: π is U+03C0 (BMP), fits in one unit
// char smile = '😀'; // ERROR: 😀 is U+1F600 (needs two units)

Useful Tip: When you need to handle characters (as people think of them), prefer String and code-point APIs (codePoints()), not raw char.

'A' vs "A" (char vs String)

'A' uses single quotes → char (one UTF-16 code unit, a number).
"A" uses double quotes → String (a sequence/array of code units).


char c = 'A'; // one 16-bit unit
String s = "A"; // a string of length 1(s.length()); // 1

Why it matters:
You can do numeric things with char (because under the hood it’s a number):


System.out.println((int) 'A'); // 65

Understanding Escape Sequences

There are times in writing code that you may want to perform an action such as moving to a new line but pressing the enter key would break your code and you don't want that. Well escape sequences are here to help you out!

Escape sequences are shortcuts that start with \ and tell Java:
“Hey, this isn’t just a backslash. I mean a special character here.”

For example;
When writing code, sometimes you want things like:
A new line
A tab space
A quotation mark inside text
Or even a backslash \ itself
But if you type them directly, Java will get confused.
That’s why we use escape sequences: little codes that start with \.

Common Escape Sequences

  1. Newline (\n) Moves text to the next line.


System.out.println("Hello\nWorld!");

Output


Hello
World!

  1. Tab (\t) Adds a big space (like pressing the Tab key).


System.out.println("A\tB\tC");

Output


A B C

  1. Double Quote (\") Lets you put " inside a string.

System.out.println("She said, \"Java is fun!\"");

Output

She said, "Java is fun!"

  1. Single Quote (\') Lets you show ' inside text.

System.out.println("It\'s a sunny day.");

Output

It's a sunny day.

  1. Backslash (\\) Shows the backslash itself.

System.out.println("This is a backslash: \\");

Output

This is a backslash: \

Unicode Escape (\uXXXX): These type are very special as they help us bring special characters into our code (characters that we won't ordinarily be able to access on our keyboard).
For example;

System.out.println("Trademark: \u2122");
System.out.println("Heart: \u2665");

Output

Trademark: ™
Heart: ♥

What you need to remember under escape sequences

  1. Always starts with \
  2. Common: \n, \t, \", \', \
  3. Unicode: \uXXXX → lets you write any character

Wrapping Up: The boolean Type
In Java, a boolean can only be true or false. You can’t use numbers like in C++.
This is actually a good thing because it prevents sneaky mistakes.
👉 Example:
In C++, writing if (x = 0) (oops, assignment instead of comparison) still compiles and runs — but it always evaluates to false.
In Java, the same thing won’t even compile, because x = 0 is not a boolean.
This strictness makes Java code safer and easier to read.

Top comments (0)