Welcome to Java Programming Basics
This website provides information and interactive examples for essential Java programming concepts. Click the links below to learn more about each concept.
Accept Input in Java
To accept input from the user in Java, you can use the Scanner
class. Here's an example:
import java.util.Scanner; public class InputExample { 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("Hello, " + name + "!"); System.out.println("Your age is: " + age); } }
Try it out:
Variables & Constants in Java
In Java, you can declare variables using various data types. Here's an example:
public class VariablesExample { public static void main(String[] args) { int age = 30; // Integer variable final double PI = 3.14159; // Constant String name = "John"; // String variable System.out.println("Age: " + age); System.out.println("PI: " + PI); System.out.println("Name: " + name); } }
Try it out:
Output in Java
To produce output in Java, you can use System.out.println()
. Here's an example:
public class OutputExample { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Try it out:
If-Else Statement in Java
You can use the if-else statement for conditional execution. Here's an example:
public class IfElseExample { public static void main(String[] args) { int num = 10; if (num > 0) { System.out.println("Number is positive"); } else { System.out.println("Number is non-positive"); } } }
Try it out:
Explode in Java
public class ExplodeExample { public static void main(String[] args) { String sentence = "This is a sample sentence"; String[] words = sentence.split(" "); for (String word : words) { System.out.println(word); } } }
Try it out:
Loops in Java
Loops are used for repeated execution of a block of code. Here's an example of a for
loop:
public class LoopsExample { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println("Iteration " + i); } } }
Try it out:
Arrays in Java
Arrays are used to store multiple values of the same type. Here's an example:
public class ArraysExample { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5}; for (int num : numbers) { System.out.println(num); } } }
Try it out:
Functions in Java
Functions allow you to modularize your code. Here's an example:
public class FunctionsExample { public static void main(String[] args) { int result = addNumbers(5, 10); System.out.println("Sum: " + result); } static int addNumbers(int a, int b) { return a + b; } }
Try it out:
Switch Statement in Java
The switch statement allows you to perform different actions based on different conditions. Here's an example:
public class SwitchExample { public static void main(String[] args) { int day = 3; String dayName; switch (day) { case 1: dayName = "Monday"; break; case 2: dayName = "Tuesday"; break; case 3: dayName = "Wednesday"; break; // Add cases for other days as needed default: dayName = "Invalid day"; } System.out.println("Day: " + dayName); } }
Try it out:
Classes and Objects in Java
Java is an object-oriented programming language. Here's an example of a simple class and object:
public class Car { String make; String model; int year; public Car(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } public void displayInfo() { System.out.println("Make: " + make); System.out.println("Model: " + model); System.out.println("Year: " + year); } } public class ClassesExample { public static void main(String[] args) { Car myCar = new Car("Toyota", "Camry", 2022); myCar.displayInfo(); } }
Try it out:
Exception Handling in Java
Java provides exception handling to deal with runtime errors. Here's an example:
public class ExceptionExample { public static void main(String[] args) { try { int result = divide(10, 0); System.out.println("Result: " + result); } catch (ArithmeticException e) { System.out.println("Error: " + e.getMessage()); } } static int divide(int a, int b) { return a / b; } }
Try it out: