Java Basics
Introduction
Java is a versatile and powerful programming language widely used for various types of software development. Understanding the basics is crucial for building a strong foundation in Java programming. In this blog, we will explore Java's data types and variables, operators and expressions, and control flow statements.
Data Types and Variables
In Java, data types specify the different sizes and values that can be stored in a variable. Java has two categories of data types: primitive and non-primitive (reference) data types.
Primitive Data Types
Java provides eight primitive data types:
- byte: 8-bit signed integer. Range: -128 to 127.
- short: 16-bit signed integer. Range: -32,768 to 32,767.
- int: 32-bit signed integer. Range: -2^31 to 2^31 - 1.
- long: 64-bit signed integer. Range: -2^63 to 2^63 - 1.
- float: 32-bit floating point. Useful for saving memory in large arrays of floating-point numbers.
- double: 64-bit floating point. Default data type for decimal values.
- boolean: Represents one bit of information. Only two possible values: true and false.
- char: 16-bit Unicode character. Range: '\u0000' to '\uffff'.
Non-Primitive Data Types
Non-primitive data types are objects that store references to values rather than the values themselves. Examples include:
- String
- Arrays
- Classes
- Interfaces
Variables
Variables are containers for storing data values. In Java, you must declare a variable before you can use it. A variable declaration includes the data type and the variable name.
int age;
double price;
char initial;
You can also initialize variables at the time of declaration.
int age = 25;
double price = 19.99;
char initial = 'A';
Operators and Expressions
Operators are special symbols that perform operations on variables and values. Java provides various operators categorized into the following types:
Arithmetic Operators
Used to perform basic arithmetic operations:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
Example:
int sum = 10 + 5;
int difference = 10 - 5;
int product = 10 * 5;
int quotient = 10 / 5;
int remainder = 10 % 3;
Relational Operators
Used to compare two values:
- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
Example:
boolean isEqual = (10 == 5); // false
boolean isNotEqual = (10 != 5); // true
boolean isGreater = (10 > 5); // true
boolean isLesser = (10 < 5); // false
Logical Operators
Used to combine multiple boolean expressions:
- AND (&&)
- OR (||)
- NOT (!)
Example:
boolean result = (10 > 5) && (5 < 3); // false
boolean result = (10 > 5) || (5 < 3); // true
boolean result = !(10 > 5); // false
Assignment Operators
Used to assign values to variables:
- Assignment (=)
- Addition assignment (+=)
- Subtraction assignment (-=)
- Multiplication assignment (*=)
- Division assignment (/=)
- Modulus assignment (%=)
Example:
int a = 10;
a += 5; // a = a + 5; a = 15
a -= 3; // a = a - 3; a = 12
a *= 2; // a = a * 2; a = 24
a /= 4; // a = a / 4; a = 6
a %= 3; // a = a % 3; a = 0
Increment and Decrement Operators
Used to increase or decrease the value of a variable by 1:
- Increment (++)
- Decrement (--)
Example:
int a = 10;
a++; // a = 11
a--; // a = 10
Control Flow Statements
Control flow statements determine the order in which statements are executed in a program. Java provides several control flow statements, including if-else, switch-case, and loops.
If-Else Statement
The if-else statement allows you to execute a block of code if a specified condition is true. If the condition is false, another block of code can be executed.
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is not positive.");
}
Switch-Case Statement
The switch-case statement allows you to execute one block of code out of many based on the value of a variable.
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day";
break;
}
System.out.println(dayName);
Loops
Loops allow you to execute a block of code repeatedly. Java provides three types of loops: for, while, and do-while.
For Loop
The for loop is used when the number of iterations is known.
for (int i = 0; i < 5; i++) {
System.out.println("Iteration: " + i);
}
While Loop
The while loop is used when the number of iterations is not known in advance. The loop continues to execute as long as the specified condition is true.
int i = 0;
while (i < 5) {
System.out.println("Iteration: " + i);
i++;
}
Do-While Loop
The do-while loop is similar to the while loop, but it guarantees that the block of code is executed at least once.
int i = 0;
do {
System.out.println("Iteration: " + i);
i++;
} while (i < 5);
Conclusion
Understanding the basics of Java, including data types and variables, operators and expressions, and control flow statements, is essential for becoming a proficient Java developer. These fundamentals provide the building blocks for more advanced concepts and applications. Stay tuned for more posts in this series as we continue to explore the Java programming language.