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
Introduction to Java
MirrorLesson 1 of 10
Lesson 1

Introduction to Java

From procedural to object-oriented

Introduction

In this lesson, you'll learn about introduction to java 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 from procedural to object-oriented.

JV
In Java:

Java has its own approach to from procedural to object-oriented, 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
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Comparing to C

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

C
C (What you know)
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

Everything in Java is inside a class

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

main is a method, not a standalone function

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

No #include - use import for other classes

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

No manual memory management

Step-by-Step Breakdown

1. Classes Everywhere

Java is purely object-oriented. Even a simple program needs a class.

C
C
// C - functions can be standalone
int add(int a, int b) {
    return a + b;
}
JV
Java
// Java - everything in a class
public class Calculator {
    public static int add(int a, int b) {
        return a + b;
    }
}

2. No Pointers

Java has references instead of pointers. No pointer arithmetic, no * or & operators.

C
C
int x = 42;
int* ptr = &x;
*ptr = 100;
JV
Java
// Java - no direct memory access
int x = 42;
// No pointers! Objects use references:
Integer obj = 42;
// References are automatic
Rule of Thumb
In Java, think in terms of objects and references, not memory addresses.

3. Garbage Collection

Java automatically frees unused memory. No malloc/free, no memory leaks from forgetting to free.

C
C
Person* p = malloc(sizeof(Person));
// ... use p ...
free(p); // must remember!
JV
Java
Person p = new Person();
// ... use p ...
// No free needed! GC handles it
p = null; // eligible for garbage collection

Common Mistakes

When coming from C, developers often make these mistakes:

  • Everything in Java is inside a class
  • main is a method, not a standalone function
  • No #include - use import for other classes
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

  • Everything lives in classes
  • No pointers - use object references
  • Automatic garbage collection
  • Filename must match public class name
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Java to practice these concepts.
OverviewNext