0% found this document useful (0 votes)
5 views3 pages

Java Basics Arrays Strings Recursion

The document covers the basics of Java programming, including a simple 'Hello, World!' program, the use of arrays, string operations, and recursion. It provides examples demonstrating array declaration and manipulation, string methods, and a recursive function for calculating factorials. Each section includes sample code and expected output to illustrate the concepts effectively.

Uploaded by

kfakeid9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Java Basics Arrays Strings Recursion

The document covers the basics of Java programming, including a simple 'Hello, World!' program, the use of arrays, string operations, and recursion. It provides examples demonstrating array declaration and manipulation, string methods, and a recursive function for calculating factorials. Each section includes sample code and expected output to illustrate the concepts effectively.

Uploaded by

kfakeid9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Basics, Arrays, Strings, and

Recursion
1. Java Basics
Java is an object-oriented programming language that is widely used for building
applications. Here’s a simple Java program:

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

This program prints 'Hello, World!' to the console.

2. Arrays in Java
An array is a collection of elements of the same type stored in contiguous memory locations.

Example: Declaring and Using an Array

public class ArrayExample {


public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50}; // Declare and initialize an array

// Accessing array elements


System.out.println("First element: " + numbers[0]);

// Loop through the array


System.out.println("Array elements:");
for (int num : numbers) {
System.out.println(num);
}
}
}

Output:
First element: 10
Array elements:
10
20
30
40
50

3. Strings in Java
A string is a sequence of characters. Java provides the `String` class to handle strings.

Example: String Operations

public class StringExample {


public static void main(String[] args) {
String str = "Hello, Java!";

// String length
System.out.println("Length: " + str.length());

// Convert to uppercase
System.out.println("Uppercase: " + str.toUpperCase());

// Substring
System.out.println("Substring: " + str.substring(7));

// Concatenation
String newStr = str + " Welcome!";
System.out.println("Concatenated String: " + newStr);
}
}

Output:

Length: 12
Uppercase: HELLO, JAVA!
Substring: Java!
Concatenated String: Hello, Java! Welcome!
4. Recursion in Java
Recursion is a technique where a function calls itself to solve a problem.

Example: Factorial using Recursion

public class RecursionExample {


// Recursive function to calculate factorial
public static int factorial(int n) {
if (n == 0) {
return 1; // Base case
}
return n * factorial(n - 1); // Recursive call
}

public static void main(String[] args) {


int num = 5;
System.out.println("Factorial of " + num + " is " + factorial(num));
}
}

Output:

Factorial of 5 is 120

You might also like