Skip to content

Add comprehensive Java tutorials and improve documentation structure #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(docs): Add tutorials for Java static keyword, switch statement, …
…this keyword, variables and literals, and primitive data types; remove obsolete test file; implement while and do...while loop examples
  • Loading branch information
uiuxarghya committed Jul 19, 2025
commit 616ac8979745a5fb95a68c1a6a3f41a9a91a9da7
78 changes: 78 additions & 0 deletions content/docs/arrays.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: Java Arrays
description: In this tutorial, we will learn how to use the Java continue statement to skip the current iteration of a loop.
---

## Java Arrays
An array is a collection of similar types of data.

For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names.

```java
String[] array = new String[100];
```
Here, the above array cannot store more than 100 names. The number of values in a Java array is always fixed.

### How to declare an array in Java?
In Java, here is how we can declare an array.

```java
dataType[] arrayName;
```
- `dataType` - it can be primitive data types like `int`, `char`, `double`, `byte`, etc. or Java objects
- `arrayName` - it is an identifier
For example,

```java
double[] data;
```
Here, data is an array that can hold values of type double.

But, how many elements can array this hold?

Good question! To define the number of elements that an array can hold, we have to allocate memory for the array in Java. For example,

```java
// declare an array
double[] data;

// allocate memory
data = new double[10];
```
Here, the array can store 10 elements. We can also say that the size or length of the array is 10.

In Java, we can declare and allocate the memory of an array in one single statement. For example,

```java
double[] data = new double[10];
```
### How to Initialize Arrays in Java?
In Java, we can initialize arrays during declaration. For example,

```java
//declare and initialize and array
int[] age = {12, 4, 5, 2, 5};
```
Here, we have created an array named age and initialized it with the values inside the curly brackets.

Note that we have not provided the size of the array. In this case, the Java compiler automatically specifies the size by counting the number of elements in the array (i.e. 5).

In the Java array, each memory location is associated with a number. The number is known as an array index. We can also initialize arrays in Java, using the index number. For example,

```java
// declare an array
int[] age = new int[5];

// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;
..
```
Elements are stored in the array
Java Arrays initialization

Note:

Array indices always start from 0. That is, the first element of an array is at index 0.
If the size of an array is n, then the last element of the array will be at index n-1.
216 changes: 216 additions & 0 deletions content/docs/basic-input-output.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
---
title: Java Basic Input and Output
description: In this tutorial, you will learn simple ways to display output to users and take input from users in Java.

---


## Java Output
In Java, you can simply use

```java
System.out.println(); or

System.out.print(); or

System.out.printf();
```

to send output to standard output (screen).

Here,

- `System` is a class
- `out` is a `public` `static` field: it accepts output data.

Don't worry if you don't understand it. We will discuss `class`, `public`, and `static` in later chapters.

Let's take an example to output a line.

```java
class AssignmentOperator {
public static void main(String[] args) {

System.out.println("Java programming is interesting.");
}
}
```
**Output:**

```plaintext
Java programming is interesting.
```
Here, we have used the `println()` method to display the string.

## Difference between println(), print() and printf()
- `print()` - It prints string inside the quotes.
- `println()` - It prints string inside the quotes similar like `print()` method. Then the cursor moves to the beginning of the next line.
- `printf()` - It provides string formatting.

### Example: print() and println()
```java
class Output {
public static void main(String[] args) {

System.out.println("1. println ");
System.out.println("2. println ");

System.out.print("1. print ");
System.out.print("2. print");
}
}
```
**Output:**
```plaintext
1. println
2. println
1. print 2. print
```
In the above example, we have shown the working of the `print()` and `println()` methods. To learn about the `printf()` method, visit Java [**printf()**](https://www.cs.colostate.edu/~cs160/.Summer16/resources/Java_printf_method_quick_reference.pdf).

### Example: Printing Variables and Literals

```java
class Variables {
public static void main(String[] args) {

Double number = -10.6;

System.out.println(5);
System.out.println(number);
}
}
```
When you run the program, the output will be:

```plaintext
5
-10.6
```
Here, you can see that we have not used the quotation marks. It is because to display integers, variables and so on, we don't use quotation marks.

### Example: Print Concatenated Strings
```java
class PrintVariables {
public static void main(String[] args) {

Double number = -10.6;

System.out.println("I am " + "awesome.");
System.out.println("Number = " + number);
}
}
```
**Output:**

```java
I am awesome.
Number = -10.6
```
In the above example, notice the line,

```java
System.out.println("I am " + "awesome.");
```

Here, we have used the + operator to concatenate (join) the two strings: `"I am "` and `"awesome."`.

And also, the line,

```java
System.out.println("Number = " + number);
```
Here, first the value of variable `number` is evaluated. Then, the value is concatenated to the string: `"Number = "`.

## Java Input
Java provides different ways to get input from the user. However, in this tutorial, you will learn to get input from user using the object of `Scanner` class.

In order to use the object of `Scanner`, we need to import `java.util.Scanner` package.


```java
import java.util.Scanner;
```
To learn more about importing packages in Java, visit [Java Import Packages]().

Then, we need to create an object of the `Scanner` class. We can use the object to take input from the user.


```java
// create an object of Scanner
Scanner input = new Scanner(System.in);

// take input from the user
int number = input.nextInt();
```

### Example: Get Integer Input From the User
```java
import java.util.Scanner;

class Input {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter an integer: ");
int number = input.nextInt();
System.out.println("You entered " + number);

// closing the scanner object
input.close();
}
}
```
**Output:**

```plaintext
Enter an integer: 23
You entered 23
```
In the above example, we have created an object named `input` of the `Scanner` class. We then call the `nextInt()` method of the `Scanner` class to get an integer input from the user.

Similarly, we can use `nextLong()`, `nextFloat()`, `nextDouble()`, and `next()` methods to get `long`, `float`, `double`, and `string` input respectively from the user.

<Callout>
**Note:** We have used the `close()` method to close the object. It is recommended to close the scanner object once the input is taken.
</Callout>

### Example: Get float, double and String Input
```java
import java.util.Scanner;

class Input {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

// Getting float input
System.out.print("Enter float: ");
float myFloat = input.nextFloat();
System.out.println("Float entered = " + myFloat);

// Getting double input
System.out.print("Enter double: ");
double myDouble = input.nextDouble();
System.out.println("Double entered = " + myDouble);

// Getting String input
System.out.print("Enter text: ");
String myString = input.next();
System.out.println("Text entered = " + myString);
}
}
```

**Output:**

```plaintext
Enter float: 2.343
Float entered = 2.343
Enter double: -23.4
Double entered = -23.4
Enter text: Hey!
Text entered = Hey!
```
As mentioned, there are other several ways to get input from the user. To learn more about `Scanner`, visit Java Scanner.
59 changes: 59 additions & 0 deletions content/docs/break-statement.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
title: Java break Statement
description: In this tutorial, we will learn how to use the Java break statement to terminate a loop or switch statement.
---

While working with loops, it is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression.

In such cases, `break` and `continue` statements are used. You will learn about the Java continue statement in the next tutorial.

The break statement in Java terminates the loop immediately, and the control of the program moves to the next statement following the loop.

It is almost always used with decision-making statements (Java if...else Statement).

Here is the syntax of the break statement in Java:

```java
break;
```

## Example 1: Java break statement

Java while loop is used to run a specific code until a certain condition is met. The syntax of the while loop is:

```java
class Test {
public static void main(String[] args) {

// for loop
for (int i = 1; i <= 10; ++i) {

// if the value of i is 5 the loop terminates
if (i == 5) {
break;
}
System.out.println(i);
}
}
}
```
#### Output

```plaintext
1
2
3
4
```

In the above program, we are using the `for` loop to print the value of i in each iteration. To know how for loop works, visit the Java `for` loop. Here, notice the statement,

```java
if (i == 5) {
break;
}
```
This means when the value of `i` is equal to 5, the loop terminates. Hence we get the output with values less than 5 only.



Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy