Hey there! 👋
Starting your journey with Java? Great choice. Let's talk about the basic syntax — the building blocks you'll use all the time. This is one of the first posts talking about basic concepts. We'll explore the whole Java Roadmap. Think of this like learning the grammar of a new language. Once you get it down, everything else starts making sense.
🧱 The Java Structure
A basic Java program looks something like this:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Let’s break that down:
-
public class HelloWorld
: Everything in Java lives inside a class. This one is namedHelloWorld
. -
public static void main(String[] args)
: This is the main method. It's where Java starts running your code. -
System.out.println("Hello, world!");
: Prints stuff to the console. You’ll use this a lot for debugging.
Variables & Data Types
Java is a statically typed language, which means you need to declare the type of every variable.
int age = 25;
double price = 19.99;
boolean isJavaFun = true;
String name = "Alice";
Some common data types:
-
int
: Whole numbers -
double
: Decimal numbers -
boolean
:true
orfalse
-
String
: Text
Control Structures
Loops and conditionals keep your code from being boring.
If-Else:
if (age > 18) {
System.out.println("You're an adult!");
} else {
System.out.println("You're a minor.");
}
For Loop:
for (int i = 0; i < 5; i++) {
System.out.println("i is: " + i);
}
While Loop:
int count = 0;
while (count < 3) {
System.out.println("Counting: " + count);
count++;
}
Methods (aka Functions)
Methods let you reuse code. Here’s how you write one:
public static int add(int a, int b) {
return a + b;
}
And to call it:
int result = add(5, 3); // result is 8
Don’t Forget the Semicolon!
In Java, most lines end with a ;
. It’s easy to forget at first, but trust me, the compiler won’t let it slide. 😅
So, What’s Next?
Once you're comfy with the basic syntax, in the next post we're going to talk about another basic concept that you must know in Java. Spoiler Strings on Java
Stay curious, and keep coding! 💻🔥
#JavaRoadmap #JavaBeginner #LearnToCode
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.