Java Programming for Beginners: From Introduction To Your First Java Project

Concepts: Explain what variables are and how to declare them. Cover Java’s primitive data types (int, double, boolean, char) and the String type for text.

Java Variables

Variables are containers for storing data values. Example: int, double, boolean, char and String.

Declaring (Creating) Variables

To create a variable in Java, you need to:

Choose a type (like int or String), Give the variable a name (like x, age, or name) and Optionally assign it a value using =. For example:

type variableName = value;

Code example:

String name = “Abdul”;
System.out.println(name);

int myInt = 5;
System.out.println(myInt );

Code example:

public class VariablesExample {
public static void main(String[] args) {
int myNumber = 42;
double pi = 3.14;
String greeting = “Hello”;
boolean isJavaFun = true;
System.out.println(“My number is: ” + myNumber);
System.out.println(“Value of Pi: ” + pi);
System.out.println(greeting + “, is Java fun? ” + isJavaFun);
}
}

Quiz: Match data types to their descriptions and predict the output of a variable declaration program.