
Learning a new programming language can feel difficult at the start. Many beginners find it hard to understand how code blocks connect, how data moves in a program, and why syntax rules are important. This article makes Java Basics simple by focusing on practical concepts and easy examples.
To build any working application in Java programming, you must first understand its basic structure. Java is a class-based and object-oriented language that follows the principle “Write Once, Run Anywhere”, which means the same program can run on any system that has a Java Virtual Machine (JVM).
Every Java program follows a fixed structure. Without this structure, the program will not run. The simplest Java program looks like this:
Java
public class Main {
public static void main(String[] args) {
// Your code starts executing here
}
}
This basic structure is made up of important parts that work together to run the program.
In Java, all code must be written inside a class. A class is like a container that holds code and defines the structure of the program. The class name usually matches the file name. For example, a class named Main is saved in a file called Main.java.
The line public static void main(String[] args) is the starting point of any Java program. This is where execution begins. When you run a Java program, the JVM looks for this method first and starts running the code inside it.
Curly braces { } are used to define code blocks in Java. They show where a class or method starts and ends. All statements inside these braces belong to that specific block.
A statement is a single instruction in Java. Every statement ends with a semicolon. If you forget to add a semicolon, the program will show a compilation error.
Understanding this basic structure is the first step in learning Java programming and building more advanced applications. It helps you write clean, organized, and error-free code..
Interacting with users is a basic part of Java programming. Java input output allows a program to take data from the user and show results on the screen in a simple and clear way.
Java provides built-in methods in the System class to print output on the screen. These methods are used to show messages, results, or data.
System.out.print(): This prints text on the console but does not move to a new line. The next output continues on the same line.
System.out.println(): This prints text and then moves the cursor to a new line. It is useful for clean and structured output.
Java
System.out.print("Hello ");
System.out.println("World!");
System.out.println("Welcome to Java Basics.");
To take input from the user, Java uses the Scanner class. This class is part of the java.util package and reads data from the keyboard using System.in.
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("User: " + name + " | Age: " + age);
scanner.close();
}
}
The Scanner class provides different methods to read different types of input:
nextLine(): Reads a full line of text including spaces.
next(): Reads only a single word until space.
nextInt(): Reads integer values.
nextDouble(): Reads decimal numbers.
Understanding these methods helps in handling different types of user input correctly in Java programs.
Programs need safe containers to hold information while processing tasks. In Java programming, you manage this information using strongly typed data placeholders known as Java variables. Because Java is statically typed, you must declare what kind of data a container holds before you can store values inside it.
Primitive types are predefined, basic units of data built directly into the core language. They differ by the specific amount of memory they use and the range of values they can hold:
|
Data Type |
Memory Allocated |
Default Storage Purpose |
Example Values |
|
byte |
1 byte |
Extremely small integer numbers |
100, -128 |
|
short |
2 bytes |
Small whole numbers |
30000 |
|
int |
4 bytes |
Standard integer values for calculations |
100000, -500 |
|
long |
8 bytes |
Exceptionally large whole integers |
15000000000L |
|
float |
4 bytes |
Standard single-precision fractional numbers |
3.14f |
|
double |
8 bytes |
High-precision double-fractional numbers |
3.14159265359 |
|
char |
2 bytes |
Single letters, symbols, or Unicode values |
'A', '7', '$' |
|
boolean |
1 bit |
Basic logical state flags |
true, false |
Non-primitive types refer to objects and complex structures that point to specific memory addresses rather than holding raw values directly. Examples include arrays, custom classes, and the highly popular String object used for handling collections of text characters.
The position where you define a container inside your file controls where that data remains accessible:
Local Variables: These are declared directly inside a specific method or code block. They only exist while that specific block runs and cannot be accessed outside of it.
Instance Variables: These are declared inside a class but outside of any individual method. Each unique object instance maintains its own separate copy of this data.
Static Variables: These are defined using the static keyword inside a class block. Every object created from the class shares this exact same variable copy in memory.
Once you have stored information inside variables, you need tools to alter, compare, and evaluate those values. You can execute these functions seamlessly by using Java operators.
These tools perform standard mathematical calculations on numerical variables:
Addition (+): Adds values together or links separate text strings.
Subtraction (-): Deducts the right value from the left value.
Multiplication (*): Multiplies numbers together.
Division (/): Divides one number by another. Note that dividing two integers discards the fractional remainder.
Modulus (%): Divides two numbers and returns only the remaining fractional leftover value.
Relational tools compare two values to determine a logical relationship, always returning a result of true or false:
Equal to (==): Verifies if two separate values are perfectly identical.
Not equal to (!=): Checks if two values are different from each other.
Greater than (>) / Less than (<): Compares which numerical value is larger or smaller.
Greater than or equal to (>=) / Less than or equal to (<=): Confirms limits or boundary conditions.
Logical tools combine multiple individual comparison rules to help your program make complex decisions:
Logical AND (&&): Evaluates to true only if every single individual condition is true.
Logical OR (||): Evaluates to true if at least one of the conditions is true.
Logical NOT (!): Reverses the current logical state of a boolean condition (turning true to false and vice versa).

