- by Adam
Before we head over to the basics of Java. Did you know that Java is created by James Gosling at Sun Microsystems (now owned by Oracle Corporation), is celebrated for its simplicity, portability, and robustness. It’s an object-oriented, high-level language known for its “Write Once, Run Anywhere” capability, which means Java code can run on any platform that has a compatible Java Virtual Machine (JVM).
Now, let’s delve into the basics:
Variables and Data Types
At the core of Java programming are variables and data types. Variables are like containers that hold data, and data types define what kind of data a variable can store. Java has several built-in data types, including:
- int: Used for integers, like whole numbers.
- double: Used for floating-point numbers, which have decimal places.
- char: Used for single characters, like ‘A’ or ‘1’.
- boolean: Used for true/false values.
- String: Used for text, a sequence of characters.
Example:
See code here:
int age = 25;
double temperature = 98.6;
char grade = ‘A’;
boolean isJavaFun = true;
String greeting = “Hello, World!”;
Control Structures
Java offers various control structures to manage the flow of your program. These include:
- if-else : For decision-making.
- for loops: For iterating through a set of instructions.
- while loops: For repeating a block of code while a condition is true.
Example:
See code here:
if (age >= 18) {
System.out.println(“You can vote!”);
} else {
System.out.println(“You cannot vote yet.”);
}
for (int i = 1; i <= 5; i++) {
System.out.println(“Count: ” + i);
}
int i = 1;
while (i <= 5) {
System.out.println(“Count: ” + i);
i++;
}
Methods
Methods are reusable blocks of code that perform specific tasks. They are a crucial part of Java programming, enabling you to break down complex problems into smaller, manageable pieces.
Object-Oriented Programming (OOP)
Java is an object-oriented language which is one of the basics of Java, which means it revolves around objects and classes. A class defines the blueprint for objects, and objects are instances of classes. OOP principles include inheritance, encapsulation, abstraction, and polymorphism.
Tips on how to easily learn Java?
Read on my previous blog here. Hope you enjoy this blog about The Basics of Java 🙂
You can also try and practice Java using online compilers, which I use sometimes – Replit