Fundamentals of Java for Junior Devs 2.

 


1. Variables and Data Types

In Java, you must tell the computer exactly what kind of data you are storing. This is called Strong Typing. Think of variables as labeled boxes.

Primitive Types (The basics)

  • int: For whole numbers (e.g., 25, -10).

  • double: For decimal numbers (e.g., 19.99, 3.14).

  • boolean: For true/false values.

  • char: For a single character (e.g., 'A'). Use single quotes.

Reference Types

  • String: For text (e.g., "Hello"). Use double quotes.


2. Declaring and Assigning

To use a variable, you follow this pattern: type name = value;

Java
 
public class DataBasics {
    public static void main(String[] args) {
        int age = 6; // Representing Adrianna's age
        double price = 10.50;
        boolean isJavaFun = true;
        String name = "Martina";

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

3. Control Flow: The if Statement

Programs need to make decisions. We use if and else to run code only when certain conditions are met.

Comparison Operators:

  • == (Equal to)

  • != (Not equal to)

  • > (Greater than)

  • < (Less than)

Java
 
int speed = 80;

if (speed > 100) {
    System.out.println("Too fast!");
} else if (speed < 40) {
    System.out.println("Too slow!");
} else {
    System.out.println("Perfect speed.");
}

4. Logical Operators

You can combine conditions to make complex decisions:

  • && (AND): Both must be true.

  • || (OR): At least one must be true.

  • ! (NOT): Reverses the value.

Java
 
boolean hasLicense = true;
int driverAge = 20;

if (hasLicense && driverAge >= 18) {
    System.out.println("You can drive the Dell truck!");
}

5. Coding Challenge: The Calculator

Try to write a small program that declares two numbers (int a and int b) and prints their sum, but only if the sum is greater than 10.

Steps to follow:

  1. Open your terminal in Debian.

  2. nano Calculator.java

  3. Write the class and the main method.

  4. Declare the variables and the if logic.

  5. javac Calculator.java and java Calculator.


Pro-Tip: Comments

As you study DAM, get into the habit of commenting on your code. It helps you (and your teachers) understand your logic later.

  • // for a single line.

  • /* ... */ for multiple lines.

Designed and curated by G.Galli | OpenLearningNode EU