Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tabWidth": 2,
"useTabs": false,
"plugins": ["prettier-plugin-tailwindcss"]
}
6 changes: 6 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions content/docs/arrays.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@ 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,
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?
Expand All @@ -39,20 +42,24 @@ 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).
Expand All @@ -69,10 +76,11 @@ age[1] = 4;
age[2] = 5;
..
```
Elements are stored in the array
Java Arrays initialization

Note:
Elements are stored in the array Java Arrays initialization

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.
<Callout title="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`.
</Callout>
30 changes: 24 additions & 6 deletions content/docs/basic-input-output.mdx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
---
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.

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
Expand Down Expand Up @@ -35,19 +34,23 @@ class AssignmentOperator {
}
}
```

**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) {
Expand All @@ -60,12 +63,15 @@ class Output {
}
}
```

**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
Expand All @@ -81,15 +87,18 @@ class Variables {
}
}
```

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) {
Expand All @@ -101,12 +110,14 @@ class PrintVariables {
}
}
```

**Output:**

```java
I am awesome.
Number = -10.6
```

In the above example, notice the line,

```java
Expand All @@ -120,22 +131,23 @@ 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);
Expand All @@ -145,6 +157,7 @@ int number = input.nextInt();
```

### Example: Get Integer Input From the User

```java
import java.util.Scanner;

Expand All @@ -162,21 +175,25 @@ class Input {
}
}
```

**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.
**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;

Expand Down Expand Up @@ -213,4 +230,5 @@ 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.
5 changes: 2 additions & 3 deletions content/docs/break-statement.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Test {
}
}
```

#### Output

```plaintext
Expand All @@ -53,7 +54,5 @@ 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.



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.
16 changes: 15 additions & 1 deletion content/docs/class-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ An object is any entity that has a state and behavior. For example, a bicycle is
Before we learn about objects, let's first know about classes in Java.

## Java Class

A class is a blueprint for the object. Before we create an object, we first need to define the class.

We can think of the class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.

Since many houses can be made from the same description, we can create many objects from a class.

### Create a class in Java

We can create a class in Java using the class keyword. For example,

```java
Expand Down Expand Up @@ -48,6 +50,7 @@ class Bicycle {
}
}
```

In the above example, we have created a class named `Bicycle`. It contains a field named `gear` and a method named `braking()`.

Here, `Bicycle` is a prototype. Now, we can create any number of bicycles using the prototype. And, all the bicycles will share the fields and methods of the prototype.
Expand All @@ -61,6 +64,7 @@ Here, `Bicycle` is a prototype. Now, we can create any number of bicycles using
---

## Java Objects

An object is called an instance of a class. For example, suppose Bicycle is a class then MountainBicycle, SportsBicycle, TouringBicycle, etc can be considered as objects of the class.

Creating an Object in Java
Expand All @@ -74,6 +78,7 @@ Bicycle sportsBicycle = new Bicycle();

Bicycle touringBicycle = new Bicycle();
```

We have used the `new` keyword along with the constructor of the class to create an object. Constructors are similar to methods and have the same name as the class. For example, `Bicycle()` is the constructor of the `Bicycle` class. To learn more, visit [Java Constructors](/docs/constructors).

Here, `sportsBicycle` and `touringBicycle` are the names of objects. We can use them to access fields and methods of the class.
Expand All @@ -89,6 +94,7 @@ As you can see, we have created two objects of the class. We can create multiple
---

## Access Members of a Class

We can use the name of objects along with the `.` operator to access members of a class. For example,

```java
Expand All @@ -110,11 +116,13 @@ Bicycle sportsBicycle = new Bicycle();
sportsBicycle.gear;
sportsBicycle.braking();
```

In the above example, we have created a class named `Bicycle`. It includes a field named `gear` and a method named `braking()`. Notice the statement,

```java
Bicycle sportsBicycle = new Bicycle();
```

Here, we have created an object of Bicycle named sportsBicycle. We then use the object to access the field and method of the class.

- `sportsBicycle.gear` - access the field gear
Expand All @@ -125,6 +133,7 @@ We have mentioned the word **method** quite a few times. You will learn about [J
Now that we understand what is class and object. Let's see a fully working example.

### Example: Java Class and Objects

```java
class Lamp {

Expand Down Expand Up @@ -164,12 +173,14 @@ public class Main {
}
}
```

#### Output:

```plaintext
Light on? true
Light on? false
```

In the above program, we have created a class named `Lamp`. It contains a variable: `isOn` and two methods: `turnOn()` and `turnOff()`.

Inside the `Main` class, we have created two objects: `led` and `halogen` of the `Lamp` class. We then used the objects to call the methods of the class.
Expand All @@ -182,6 +193,7 @@ The variable `isOn` defined inside the class is also called an instance variable
That is, `led` and `halogen` objects will have their own copy of the `isOn` variable.

### Example: Create objects inside the same class

Note that in the previous example, we have created objects inside another class and accessed the members from that class.

However, we can also create objects inside the same class.
Expand Down Expand Up @@ -211,9 +223,11 @@ class Lamp {
}
}
```

#### Output

```plaintext
Light on? true
```
Here, we are creating the object inside the `main()` method of the same class.

Here, we are creating the object inside the `main()` method of the same class.
Loading
Loading