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.
In C, you're familiar with from procedural to object-oriented.
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:
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:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}You may be used to different syntax or behavior.
Everything in Java is inside a class
You may be used to different syntax or behavior.
main is a method, not a standalone function
You may be used to different syntax or behavior.
No #include - use import for other classes
You may be used to different syntax or behavior.
No manual memory management
Step-by-Step Breakdown
1. Classes Everywhere
Java is purely object-oriented. Even a simple program needs a class.
// C - functions can be standalone
int add(int a, int b) {
return a + b;
}// 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.
int x = 42;
int* ptr = &x;
*ptr = 100;// Java - no direct memory access
int x = 42;
// No pointers! Objects use references:
Integer obj = 42;
// References are automatic3. Garbage Collection
Java automatically frees unused memory. No malloc/free, no memory leaks from forgetting to free.
Person* p = malloc(sizeof(Person));
// ... use p ...
free(p); // must remember!Person p = new Person();
// ... use p ...
// No free needed! GC handles it
p = null; // eligible for garbage collectionCommon 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
Key Takeaways
- Everything lives in classes
- No pointers - use object references
- Automatic garbage collection
- Filename must match public class name