Java is a powerful, object-oriented programming language that is widely used for building web applications, mobile apps, and large-scale enterprise systems. Our tutorials are designed to give you a solid understanding of Java fundamentals, along with practical examples and hands-on coding exercises.
Java is a high-level, class-based, object-oriented programming language designed to have as few implementation dependencies as possible. It is a versatile language used in various applications, from web development to mobile apps and large-scale systems.
Java syntax is the set of rules defining how Java programs are written and interpreted. A basic Java program structure includes classes and methods. Here's a simple example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Click to code here..
Variables in Java are used to store data values. Each variable must be declared with a data type. Here's an example:
int number = 10;
String message = "Hello";
Click to code here..
Java supports various data types including primitive types like int, char, double, and non-primitive types like String and Arrays. For example:
int age = 25; // Primitive type
String name = "Alice"; // Non-primitive type
Click to code here..
Type casting is converting a variable from one type to another. Java supports both implicit and explicit casting:
int num = 10;
double result = num; // Implicit casting
double price = 9.99;
int roundedPrice = (int) price; // Explicit casting
Click to code here..
Operators perform operations on variables and values. Java includes arithmetic, relational, and logical operators:
int a = 5 + 3; // Addition
boolean isEqual = (a == 8); // Relational
boolean result = (a > 0) && (isEqual); // Logical
Click to code here..
Strings in Java are objects that represent sequences of characters. They are used for storing text:
String greeting = "Hello, World!";
String name = greeting.toUpperCase(); // Converts to uppercase
Click to code here..
Boolean data type represents one of two values: true or false. It is often used in conditional statements:
boolean isJavaFun = true;
if (isJavaFun) {
System.out.println("Java is fun!");
}
Click to code here..
The if...else statement allows you to execute certain code blocks based on a condition:
int age = 18;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
Click to code here..
Java provides both primitive type int and wrapper class Integer. The wrapper class offers additional methods:
int primitiveInt = 100;
Integer wrapperInt = Integer.valueOf(primitiveInt);
int newPrimitiveInt = wrapperInt.intValue();
Click to code here..
The switch statement allows you to execute one block of code among many based on the value of a variable:
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid day");
}
Click to code here..
The while loop repeats a block of code as long as a condition is true:
int count = 0;
while (count < 5) {
System.out.println(count);
count++;
}
Click to code here..
The for loop allows you to execute a block of code a specific number of times:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Click to code here..
The break statement exits a loop, while the continue statement skips the current iteration:
for (int i = 0; i < 10; i++) {
if (i == 5) break; // Exits the loop
if (i % 2 == 0) continue; // Skips even numbers
System.out.println(i);
}
Click to code here..
Arrays are used to store multiple values in a single variable. Java supports one-dimensional and multi-dimensional arrays:
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}
Click to code here..
Methods are blocks of code that perform a specific task and can be reused. They may or may not return a value:
public class Main {
public static void printMessage() {
System.out.println("Hello from method!");
}
public static void main(String[] args) {
printMessage();
}
}
Click to code here..
Methods can accept parameters, allowing you to pass information into the method:
public class Main {
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
greet("Alice");
}
}
Click to code here..
Method overloading allows you to define multiple methods with the same name but different parameters:
public class Main {
public static void display(int num) {
System.out.println("Number: " + num);
}
public static void display(String message) {
System.out.println("Message: " + message);
}
public static void main(String[] args) {
display(5);
display("Hello");
}
}
Click to code here..
A class is a blueprint for creating objects. It defines a datatype by bundling data and methods:
public class Car {
String color;
int year;
public void start() {
System.out.println("Car is starting");
}
}
Click to code here..
Object-Oriented Programming (OOP) principles include Encapsulation, Inheritance, Polymorphism, and Abstraction. OOP helps in designing modular and reusable code.
Objects are instances of classes. You create objects from a class and interact with their attributes and methods:
Car myCar = new Car();
myCar.color = "Red";
myCar.start();
Click to code here..
Attributes (or fields) are variables defined inside a class. They represent the state or properties of an object:
public class Car {
String color; // Attribute
}
Click to code here..
Class methods are functions defined inside a class that operate on its attributes:
public class Car {
String color;
public void start() {
System.out.println("Car is starting");
}
}
Click to code here..
Modifiers control the visibility and behavior of classes, methods, and variables. Common modifiers include public, private, and protected:
public class Example {
private int number; // Private modifier
}
Click to code here..
Encapsulation is the technique of bundling data and methods that operate on the data within a single unit (class) and restricting access to some of the object's components:
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Click to code here..
Packages are used to group related classes and interfaces. Java APIs provide a set of pre-built classes and methods that you can use in your programs:
import java.util.ArrayList;
public class Example {
ArrayList list = new ArrayList<>();
}
Click to code here..
Inheritance allows a class to inherit attributes and methods from another class. It promotes code reusability:
public class Animal {
public void eat() {
System.out.println("This animal eats food.");
}
}
public class Dog extends Animal {
public void bark() {
System.out.println("Dog barks");
}
}
Click to code here..
Polymorphism allows methods to do different things based on the object it is acting upon. It includes method overriding and method overloading:
public class Animal {
public void makeSound() {
System.out.println("Animal sound");
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Bark");
}
}
Click to code here..
Abstraction hides the complex implementation details and shows only the essential features of an object:
abstract class Animal {
abstract void makeSound();
}
class Dog extends Animal {
void makeSound() {
System.out.println("Bark");
}
}
Click to code here..
An interface is a reference type in Java, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types:
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Bark");
}
}
Click to code here..
Enums are a special Java type used to define collections of constants:
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
Click to code here..
User input can be obtained using the Scanner class:
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name);
}
}
Click to code here..
ArrayList is a resizable array implementation of the List interface:
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list);
}
}
Click to code here..
LinkedList is a doubly-linked list implementation of the List and Deque interfaces:
import java.util.LinkedList;
public class Example {
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list);
}
}
Click to code here..
Lists can be sorted using Collections.sort() method:
import java.util.ArrayList;
import java.util.Collections;
public class Example {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add("Banana");
list.add("Apple");
Collections.sort(list);
System.out.println(list);
}
}
Click to code here..
HashMap is a map-based collection class that is used to store key-value pairs:
import java.util.HashMap;
public class Example {
public static void main(String[] args) {
HashMap map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
System.out.println(map);
}
}
Click to code here..
HashSet is a collection that does not allow duplicate elements:
import java.util.HashSet;
public class Example {
public static void main(String[] args) {
HashSet set = new HashSet<>();
set.add("Apple");
set.add("Banana");
System.out.println(set);
}
}
Click to code here..
Iterator is used to iterate over a collection of objects:
import java.util.ArrayList;
import java.util.Iterator;
public class Example {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Click to code here..
Wrapper classes provide a way to use primitive data types as objects:
int num = 10;
Integer numObj = Integer.valueOf(num);
System.out.println(numObj);
Click to code here..
Exception handling allows a program to deal with runtime errors, so the program can continue execution or terminate gracefully:
public class Example {
public static void main(String[] args) {
try {
int division = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
}
}
Click to code here..
Threads are lightweight processes that can run concurrently with other threads:
public class Example extends Thread {
public void run() {
System.out.println("Thread is running");
}
public static void main(String[] args) {
Example thread = new Example();
thread.start();
}
}
Click to code here..
Lambda expressions provide a clear and concise way to represent one method interface using an expression:
import java.util.function.Function;
public class Example {
public static void main(String[] args) {
Function square = x -> x * x;
System.out.println(square.apply(5));
}
}
Click to code here..
Java provides classes for file handling that allow you to create, read, write, and delete files:
The File class represents file and directory pathnames in an abstract manner:
import java.io.File;
public class Example {
public static void main(String[] args) {
File file = new File("example.txt");
System.out.println(file.exists());
}
}
Click to code here..
You can create and write to files using FileWriter:
import java.io.FileWriter;
import java.io.IOException;
public class Example {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("example.txt");
writer.write("Hello, World!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Click to code here..
You can read from files using FileReader:
import java.io.FileReader;
import java.io.IOException;
public class Example {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("example.txt");
int character;
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Click to code here..
Files can be deleted using the delete() method:
import java.io.File;
public class Example {
public static void main(String[] args) {
File file = new File("example.txt");
if (file.delete()) {
System.out.println("File deleted successfully");
} else {
System.out.println("Failed to delete the file");
}
}
}
Click to code here..