C
JV

C to Java

10 lessons

Progress0%
1Introduction to Java2Data Types3Strings4Arrays and Collections5Object-Oriented Programming6Exception Handling7Collections and Generics8Modern Java Features9Interfaces and Polymorphism10Threads and Concurrency
All Mirror Courses
C
JV
Data Types
MirrorLesson 2 of 10
Lesson 2

Data Types

Primitive and reference types

Introduction

In this lesson, you'll learn about data types in Java. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
C
From C:

In C, you're familiar with primitive and reference types.

JV
In Java:

Java has its own approach to primitive and reference types, which we'll explore step by step.

The Java Way

Let's see how Java handles this concept. Here's a typical example:

JV
Java Example
// Java types - guaranteed sizes
byte b = 127;           // 8 bits, signed
short s = 100;          // 16 bits
int i = 1000;           // 32 bits
long l = 100000L;       // 64 bits
float f = 3.14f;
double d = 3.14159;
char c = 'A';           // 16 bits, Unicode
boolean flag = true;    // true or false

Comparing to C

Here's how you might have written similar code in C:

C
C (What you know)
// C types - platform dependent
char c = 'A';           // 8 bits
short s = 100;          // at least 16 bits
int i = 1000;           // at least 16 bits
long l = 100000L;       // at least 32 bits
float f = 3.14f;
double d = 3.14159;
int flag = 1;           // no boolean
Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

Java has boolean type (true/false)

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

char is 16-bit Unicode, not 8-bit ASCII

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

Type sizes are guaranteed across platforms

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

byte type for 8-bit signed integers

Step-by-Step Breakdown

1. Boolean Type

Java has a real boolean type. No more using 0 and 1.

C
C
int isValid = 1;
if (isValid) { ... }
JV
Java
boolean isValid = true;
if (isValid) { ... }
Common Pitfall
Unlike C, you can't use integers as booleans in Java. if (1) is a compile error.

2. Character Type

Java char is 16-bit Unicode, supporting international characters.

C
C
char c = 'A'; // ASCII only
JV
Java
char c = 'A';      // ASCII works
char unicode = 'δΈ­'; // Unicode too!
char emoji = 'πŸ˜€';  // Even emojis (with limits)

3. Wrapper Classes

Each primitive has a wrapper class for when you need an object.

JV
Java
// Primitives
int x = 42;
double d = 3.14;

// Wrapper classes (needed for generics, null)
Integer xObj = 42;       // auto-boxing
Double dObj = 3.14;

// Useful methods
int parsed = Integer.parseInt("42");
String str = Integer.toString(42);
Rule of Thumb
Use primitives for performance, wrappers when you need objects (collections, nullability).

Common Mistakes

When coming from C, developers often make these mistakes:

  • Java has boolean type (true/false)
  • char is 16-bit Unicode, not 8-bit ASCII
  • Type sizes are guaranteed across platforms
Common Pitfall
Don't assume Java works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Java has boolean (true/false)
  • char is 16-bit Unicode
  • All type sizes are guaranteed
  • Wrapper classes provide object versions
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Java to practice these concepts.
PreviousNext