In this module, learners move from understanding Java fundamentals to actually writing meaningful Java code. Java Basics introduce how data is stored, processed, and displayed in a Java program. Concepts such as variables, data types, operators, type casting, input/output operations, and coding conventions are essential for building clean, readable, and efficient Java applications. Mastering these basics ensures a strong foundation for advanced topics like control statements, object-oriented programming, and data structures.
A variable is a named memory location used to store data that can change during program execution. Every variable in Java must be declared with a specific data type before use.
static keyword; shared across all objects of a class.Data types define what kind of data a variable can store and how much memory it occupies. Java data types are divided into primitive and reference data types.
Primitive data types store simple values directly in memory. Java has eight primitive data types.
| Data Type | Description |
|---|---|
byte | Small integer values |
short | Medium integer values |
int | Most commonly used integer |
long | Very large integers |
float | Decimal values (single precision) |
double | Decimal values (double precision) |
char | Single character |
boolean | True or false |
Key Points
Reference data types store memory addresses of objects rather than actual values.
Examples
StringKey Points
nullOperators are symbols used to perform operations on variables and values.
Used for mathematical calculations.
| Operator | Operation |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
Used to compare values and return boolean results.
| Operator | Description |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
Used to combine boolean expressions.
| Operator | Meaning |
|---|---|
&& | Logical AND |
| ` | |
! | Logical NOT |
Used to perform operations at the bit level.
| Operator | Operation |
|---|---|
& | Bitwise AND |
| ` | ` |
^ | Bitwise XOR |
~ | Bitwise Complement |
<< | Left shift |
>> | Right shift |
Use Case: Low-level programming, performance optimization.
Type casting is the process of converting one data type into another.
Example:int → long → float → double
Example:double → int
| Implicit | Explicit |
|---|---|
| Automatic | Manual |
| No data loss | Possible data loss |
| Safe | Risky |
Input and output operations allow interaction between the user and the program.
System.out.print() – prints outputSystem.out.println() – prints output with new lineUsed to display messages, results, and debugging information.
Java uses the Scanner class to read user input.
Common Inputs
Key Points
Scanner belongs to java.util packageComments are used to explain code and improve readability. They are ignored by the compiler.
/** */)Naming conventions improve code readability and professionalism.
StudentDetailstotalMarksMAX_VALUEcom.company.projectBy completing this module, learners will:
java
import java.util.Scanner;
public class BasicOperations {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Variables and data types
int age = 25;
double salary = 50000.50;
char grade = 'A';
boolean isEmployed = true;
// Input
System.out.print("Enter your name: ");
String name = sc.nextLine();
// Operators
int x = 10, y = 3;
System.out.println("Sum: " + (x + y));
System.out.println("Division: " + (x / y));
System.out.println("Modulus: " + (x % y));
// Type casting
double result = (double) x / y;
System.out.println("Precise division: " + result);
}
}