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;
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)
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.
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:
Open your terminal in Debian.
nano Calculator.javaWrite the class and the
mainmethod.Declare the variables and the
iflogic.javac Calculator.javaandjava 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.