Java is a powerful, versatile programming language widely used in software development. This module covers essential core Java concepts that form the foundation for mastering the language. Core Java concepts such as variables, data types, control flow statements, loops, functions, methods, arrays, and collections are fundamental for any Java developer.
Table of Contents
Variables and Data Types in Java
Variables are used to store data in a program. Java is a statically-typed language, meaning the type of a variable must be declared before use.
Data Types in Java:
- Primitive Data Types:
byte
,short
,int
,long
(integer types)float
,double
(floating-point types)char
(character type)boolean
(true/false values)
- Non-Primitive Data Types:
String
,Arrays
,Classes
,Interfaces
Example:
public class VariableExample {
public static void main(String[] args) {
int age = 25;
double salary = 55000.50;
char grade = 'A';
boolean isJavaFun = true;
String name = "John";
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Grade: " + grade);
System.out.println("Is Java Fun? " + isJavaFun);
}
}
Control Flow – If-Else, Loops
Control flow statements help direct the flow of execution based on conditions or repetitions in Java programming.
If-Else Statement
The if-else
statement is used for decision-making.
Example:
public class IfElseExample {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("Positive number");
} else {
System.out.println("Negative number");
}
}
}
Loops
Loops are used to execute a block of code multiple times.
For Loop Example:
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
}
}
While Loop Example:
public class WhileLoopExample {
public static void main(String[] args) {
int count = 1;
while (count <= 5) {
System.out.println("Count: " + count);
count++;
}
}
}
Functions and Methods in Java
A method (or function) is a reusable block of code that performs a specific task.
Example:
public class MethodExample {
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
greet("Alice");
greet("Bob");
}
}
Arrays and Collections
Arrays
An array is a fixed-size collection of elements of the same data type.
Example:
public class ArrayExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println(number);
}
}
}
Collections
Java Collections Framework provides dynamic data structures like ArrayList
, HashSet
, and HashMap
.
ArrayList Example:
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
for (String name : names) {
System.out.println(name);
}
}
}
ALL IN ALL
Mastering core Java concepts such as variables, data types, control flow statements, loops, functions, and data structures lays a strong foundation for advanced Java programming. Core Java concepts are essential for software development success. Practice with real-world examples and build small projects to enhance your understanding. Stay tuned for the next module on Object-Oriented Programming (OOP) in Java!
Core Java Concepts: Key Takeaways
- Understand variables and data types.
- Master control flow using if-else and loops.
- Learn functions and methods for code reusability.
- Explore arrays and collections for data storage.
By focusing on these core Java concepts, you’ll be well-prepared for more advanced topics in Java development. Happy coding!
Fun and Challenging Practice Questions :
- Write a Java program using an R26B-style variable to calculate the sum of all odd numbers from 1 to 100.
- Create a Java method (R26B style) that checks if a given string is a palindrome.
- Implement an R26B-style for loop that prints the Fibonacci series up to 50.
- Write an R26B-style Java class that simulates a basic quiz game with 5 random questions.
- Develop an R26B-style Java program that finds the second largest number in an array of integers.
I hope you enjoyed diving into Object-Oriented Programming (OOP) with R26B! It’s a powerful concept that, when mastered, will make your code elegant, reusable, and fun. Happy coding!