diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..0fb25db6 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "tabWidth": 2, + "useTabs": false, + "plugins": ["prettier-plugin-tailwindcss"] +} diff --git a/bun.lock b/bun.lock index 8408e2d0..67c7af9c 100644 --- a/bun.lock +++ b/bun.lock @@ -16,6 +16,8 @@ "motion": "^12.23.12", "next": "15.4.2", "ogl": "^1.0.11", + "prettier": "^3.6.2", + "prettier-plugin-tailwindcss": "^0.6.14", "react": "^19.1.0", "react-dom": "^19.1.0", "sharp": "^0.34.3", @@ -1099,6 +1101,10 @@ "prelude-ls": ["prelude-ls@1.2.1", "", {}, "sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g=="], + "prettier": ["prettier@3.6.2", "", { "bin": { "prettier": "bin/prettier.cjs" } }, "sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ=="], + + "prettier-plugin-tailwindcss": ["prettier-plugin-tailwindcss@0.6.14", "", { "peerDependencies": { "@ianvs/prettier-plugin-sort-imports": "*", "@prettier/plugin-hermes": "*", "@prettier/plugin-oxc": "*", "@prettier/plugin-pug": "*", "@shopify/prettier-plugin-liquid": "*", "@trivago/prettier-plugin-sort-imports": "*", "@zackad/prettier-plugin-twig": "*", "prettier": "^3.0", "prettier-plugin-astro": "*", "prettier-plugin-css-order": "*", "prettier-plugin-import-sort": "*", "prettier-plugin-jsdoc": "*", "prettier-plugin-marko": "*", "prettier-plugin-multiline-arrays": "*", "prettier-plugin-organize-attributes": "*", "prettier-plugin-organize-imports": "*", "prettier-plugin-sort-imports": "*", "prettier-plugin-style-order": "*", "prettier-plugin-svelte": "*" }, "optionalPeers": ["@ianvs/prettier-plugin-sort-imports", "@prettier/plugin-hermes", "@prettier/plugin-oxc", "@prettier/plugin-pug", "@shopify/prettier-plugin-liquid", "@trivago/prettier-plugin-sort-imports", "@zackad/prettier-plugin-twig", "prettier-plugin-astro", "prettier-plugin-css-order", "prettier-plugin-import-sort", "prettier-plugin-jsdoc", "prettier-plugin-marko", "prettier-plugin-multiline-arrays", "prettier-plugin-organize-attributes", "prettier-plugin-organize-imports", "prettier-plugin-sort-imports", "prettier-plugin-style-order", "prettier-plugin-svelte"] }, "sha512-pi2e/+ZygeIqntN+vC573BcW5Cve8zUB0SSAGxqpB4f96boZF4M3phPVoOFCeypwkpRYdi7+jQ5YJJUwrkGUAg=="], + "prop-types": ["prop-types@15.8.1", "", { "dependencies": { "loose-envify": "^1.4.0", "object-assign": "^4.1.1", "react-is": "^16.13.1" } }, "sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg=="], "property-information": ["property-information@7.1.0", "", {}, "sha512-TwEZ+X+yCJmYfL7TPUOcvBZ4QfoT5YenQiJuX//0th53DE6w0xxLEtfK3iyryQFddXuvkIk51EEgrJQ0WJkOmQ=="], diff --git a/content/docs/arrays.mdx b/content/docs/arrays.mdx index 5b933c07..a5ce491d 100644 --- a/content/docs/arrays.mdx +++ b/content/docs/arrays.mdx @@ -3,7 +3,6 @@ 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. @@ -11,21 +10,25 @@ For example, if we want to store the names of 100 people then we can create an a ```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? @@ -39,6 +42,7 @@ 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, @@ -46,13 +50,16 @@ In Java, we can declare and allocate the memory of an array in one single statem ```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). @@ -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. + + 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`. + diff --git a/content/docs/basic-input-output.mdx b/content/docs/basic-input-output.mdx index fa2485b7..5cbabb09 100644 --- a/content/docs/basic-input-output.mdx +++ b/content/docs/basic-input-output.mdx @@ -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 @@ -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) { @@ -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 @@ -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) { @@ -101,12 +110,14 @@ class PrintVariables { } } ``` + **Output:** ```java I am awesome. Number = -10.6 ``` + In the above example, notice the line, ```java @@ -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); @@ -145,6 +157,7 @@ int number = input.nextInt(); ``` ### Example: Get Integer Input From the User + ```java import java.util.Scanner; @@ -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. -**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. ### Example: Get float, double and String Input + ```java import java.util.Scanner; @@ -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. diff --git a/content/docs/break-statement.mdx b/content/docs/break-statement.mdx index bfe1b685..381c9271 100644 --- a/content/docs/break-statement.mdx +++ b/content/docs/break-statement.mdx @@ -37,6 +37,7 @@ class Test { } } ``` + #### Output ```plaintext @@ -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. diff --git a/content/docs/class-objects.mdx b/content/docs/class-objects.mdx index 776fc5e5..d31cf3d1 100644 --- a/content/docs/class-objects.mdx +++ b/content/docs/class-objects.mdx @@ -13,6 +13,7 @@ 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. @@ -20,6 +21,7 @@ We can think of the class as a sketch (prototype) of a house. It contains all th 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 @@ -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. @@ -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 @@ -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. @@ -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 @@ -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 @@ -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 { @@ -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. @@ -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. @@ -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. \ No newline at end of file + +Here, we are creating the object inside the `main()` method of the same class. diff --git a/content/docs/comments.mdx b/content/docs/comments.mdx index 3f06180c..d5fa262c 100644 --- a/content/docs/comments.mdx +++ b/content/docs/comments.mdx @@ -1,9 +1,8 @@ --- title: Java Comments -description: In this tutorial, you will learn about Java comments, why we use them, and how to use comments in right way. +description: In this tutorial, you will learn about Java comments, why we use them, and how to use comments in right way. --- - In computer programming, comments are a portion of the program that are completely ignored by Java compilers. They are mainly used to help programmers to understand the code. For example, ```java @@ -14,6 +13,7 @@ int b = 3; // print the output System.out.println("This is output"); ``` + Here, we have used the following comments, - declare and initialize two variables @@ -41,11 +41,13 @@ class Main { } } ``` + Output: ```plaintext Hello, World! ``` + Here, we have used two single-line comments: - `"Hello, World!" program example` @@ -69,11 +71,13 @@ class HelloWorld { } } ``` + Output: ```plaintext Hello, World! ``` + Here, we have used the multi-line comment: ```java @@ -81,6 +85,7 @@ Here, we have used the multi-line comment: * The program prints "Hello, World!" to the standard output. */ ``` + This type of comment is also known as **Traditional Comment**. In this type of comment, the Java compiler ignores everything from `/*` to `*/`. ## Use Comments the Right Way diff --git a/content/docs/constructors.mdx b/content/docs/constructors.mdx index 86dc9c4e..7d558080 100644 --- a/content/docs/constructors.mdx +++ b/content/docs/constructors.mdx @@ -14,9 +14,11 @@ class Test { } } ``` + Here, `Test()` is a constructor. It has the same name as that of the class and doesn't have a return type. ## Example: Java Constructor + ```java class Main { private String name; @@ -36,12 +38,14 @@ class Main { } } ``` + ### Output: ```plaintext Constructor Called: The name is Javaistic ``` + In the above example, we have created a constructor named `Main()`. Inside the constructor, we are initializing the value of the `name` [variable](/docs/variables-literals). @@ -51,11 +55,13 @@ Notice the statement creating an object of the `Main` class. ```java Main obj = new Main(); ``` + Here, when the object is created, the `Main()` constructor is called. And the value of the name variable is initialized. Hence, the program prints the value of the name variables as Javaistic. ## Types of Constructor + In Java, constructors can be divided into three types: - [No-Arg Constructor](#1-java-no-arg-constructors) @@ -73,7 +79,9 @@ private Constructor() { // body of the constructor } ``` + ### Example: Java Private No-arg Constructor + ```java class Main { @@ -93,12 +101,14 @@ class Main { } } ``` + #### Output: ```plaintext Constructor is called Value of i: 5 ``` + In the above example, we have created a constructor Main(). Here, the constructor does not accept any parameters. Hence, it is known as a no-arg constructor. Notice that we have declared the constructor as private. @@ -111,6 +121,7 @@ Hence, the program is able to access the constructor. To learn more, visit Java However, if we want to create objects outside the class, then we need to declare the constructor as public. ### Example: Java Public no-arg Constructors + ```java class Company { String name; @@ -130,16 +141,19 @@ class Main { } } ``` + #### Output ```plaintext Company name = Javaistic ``` + ## 2. Java Parameterized Constructor A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors (constructors with parameters). ### Example: Parameterized Constructor + ```java class Main { @@ -160,6 +174,7 @@ class Main { } } ``` + #### Output ```plaintext @@ -167,12 +182,14 @@ Java Programming Language Python Programming Language C Programming Language ``` + In the above example, we have created a constructor named `Main()`. Here, the constructor takes a single parameter. Notice the expression: ```java Main obj1 = new Main("Java"); ``` + Here, we are passing the single value to the constructor. Based on the argument passed, the language variable is initialized inside the constructor. @@ -182,6 +199,7 @@ If we do not create any constructor, the Java compiler automatically creates a n This constructor is called the default constructor. ### Example: Default Constructor + ```java class Main { @@ -199,6 +217,7 @@ class Main { } } ``` + #### Output ```plaintext @@ -206,23 +225,24 @@ Default Value: a = 0 b = false ``` + Here, we haven't created any constructors. Hence, the Java compiler automatically creates the default constructor. The default constructor initializes any uninitialized instance variables with default values. -| Type | Default | Value | -|:-----:|:-------:|:-----:| -| boolean | | false | -| byte | | 0 | -| short | | 0 | -| int | | 0 | -| long | | 0L | -| char | | \u0000 | -| float | | 0.0f | -| double | | 0.0d | -| object| Reference | null | +| Type | Default | Value | +| :-----: | :-------: | :----: | +| boolean | | false | +| byte | | 0 | +| short | | 0 | +| int | | 0 | +| long | | 0L | +| char | | \u0000 | +| float | | 0.0f | +| double | | 0.0d | +| object | Reference | null | To learn more, visit [Java Data Types](/docs/variables-primitive-data-types). In the above program, the variables a and b are initialized with default value `0` and `false` respectively. @@ -250,6 +270,7 @@ class Main { } } ``` + #### Output ```plaintext @@ -257,6 +278,7 @@ Default Value: a = 0 b = false ``` + ### Important Notes on Java Constructors Constructors are invoked implicitly when you instantiate objects. @@ -269,6 +291,7 @@ The two rules for creating a constructor are: If a class doesn't have a constructor, the Java compiler automatically creates a default constructor during run-time. The default constructor initializes instance variables with default values. For example, the int variable will be initialized to 0 ### Constructor types: + No-Arg Constructor - a constructor that does not accept any arguments Parameterized constructor - a constructor that accepts arguments Default Constructor - a constructor that is automatically created by the Java compiler if it is not explicitly defined. @@ -276,9 +299,11 @@ A constructor cannot be abstract or static or final. A constructor can be overloaded but can not be overridden. ## Constructors Overloading in Java + Similar to Java method overloading, we can also create two or more constructors with different parameters. This is called constructor overloading. ### Example: Java Constructor Overloading + ```java class Main { @@ -311,12 +336,14 @@ class Main { } } ``` + #### Output ```plaintext Programming Language: Java Programming Language: Python ``` + In the above example, we have two constructors: `Main()` and `Main(String language)`. Here, both the constructors initialize the value of the variable language with different values. @@ -327,4 +354,4 @@ It is also possible to call one constructor from another constructor. To learn m **Note:** We have used this keyword to specify the variable of the class. To know more about this keyword, visit [Java this keyword](/docs/this-keyword). - \ No newline at end of file + diff --git a/content/docs/continue-statement.mdx b/content/docs/continue-statement.mdx index 6c44eccc..6284db97 100644 --- a/content/docs/continue-statement.mdx +++ b/content/docs/continue-statement.mdx @@ -7,7 +7,6 @@ While working with loops, sometimes you might want to skip some statements or te To learn about the break statement, visit Java break. Here, we will learn about the continue statement. - ## Java continue The continue statement skips the current iteration of a loop (for, while, do...while, etc). @@ -22,8 +21,8 @@ continue; Note: The continue statement is almost always used in decision-making statements (if...else Statement). - ## Working of Java continue statement + The working of continue statement with Java while, do...while, and for loop. Working of Java continue Statement @@ -57,6 +56,7 @@ class Main { 9 10 ``` + 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 Java for loop. Notice the statement, ```java @@ -64,6 +64,7 @@ if (i > 4 && i < 9) { continue; } ``` + Here, the continue statement is executed when the value of i becomes more than 4 and less than 9. It then skips the print statement for those values. Hence, the output skips the values 5, 6, 7, and 8. @@ -98,6 +99,7 @@ class Main { } } ``` + #### Output: ```plaintext @@ -108,6 +110,7 @@ Enter number 4: -2.4 Enter number 5: -3 Sum = 7.8 ``` + In the above example, we have used the for loop to print the sum of 5 positive numbers. Notice the line, ```java @@ -115,6 +118,7 @@ if (number < 0.0) { continue; } ``` + Here, when the user enters a negative number, the continue statement is executed. This skips the current iteration of the loop and takes the program control to the update expression of the loop. Note: To take input from the user, we have used the Scanner object. To learn more, visit Java Scanner. diff --git a/content/docs/enhanced-for-loop.mdx b/content/docs/enhanced-for-loop.mdx index 416ae06b..eab1768e 100644 --- a/content/docs/enhanced-for-loop.mdx +++ b/content/docs/enhanced-for-loop.mdx @@ -6,6 +6,7 @@ description: In this tutorial, we will learn about the Java for-each loop and it In Java, the **for-each loop** is used to iterate through elements of [arrays](/docs/arrays) and collections (like [ArrayList](/docs/arraylist)). It is also known as the enhanced for loop. ## `for-each` Loop Syntax + The syntax of the Java **for-each** loop is: ```java @@ -13,6 +14,7 @@ for(dataType item : array) { ... } ``` + Here, - **array** - an array or a collection @@ -39,6 +41,7 @@ class Main { } } ``` + #### Output ```plaintext @@ -47,6 +50,7 @@ class Main { 5 -5 ``` + Here, we have used the **for-each loop** to print each element of the numbers array one by one. - In the first iteration, the item will be 3. @@ -77,6 +81,7 @@ class Main { } } ``` + #### Output: ```plaintext @@ -85,14 +90,14 @@ Sum = 19 In the above program, the execution of the for-each loop looks as: -| Iteration | Variables | -|----------|----------| -|1| `number` = 3 `sum` = 0 + 3 = 3| -|2| `number` = 4 `sum` = 3 + 4 = 7 -|3| `number` = 5 `sum` = 7 + 5 = 12| -|4| `number` = -5 `sum` = 12 + (-5) = 7| -|5| `number` = 0 `sum` = 7 + 0 = 7| -|6| `number` = 12 `sum` = 7 + 12 = 19| +| Iteration | Variables | +| --------- | ----------------------------------- | +| 1 | `number` = 3 `sum` = 0 + 3 = 3 | +| 2 | `number` = 4 `sum` = 3 + 4 = 7 | +| 3 | `number` = 5 `sum` = 7 + 5 = 12 | +| 4 | `number` = -5 `sum` = 12 + (-5) = 7 | +| 5 | `number` = 0 `sum` = 7 + 0 = 7 | +| 6 | `number` = 12 `sum` = 7 + 12 = 19 | As we can see, we have added each element of the `numbers` array to the `sum` variable in each iteration of the loop. @@ -117,6 +122,7 @@ class Main { } } ``` + #### Output: ```plaintext @@ -126,6 +132,7 @@ i o u ``` + ### 2. Using for-each Loop #### Input @@ -143,6 +150,7 @@ class Main { } } ``` + #### Output: ```plaintext @@ -152,6 +160,7 @@ i o u ``` + Here, the output of both programs is the same. However, the **for-each** loop is easier to write and understand. This is why the **for-each** loop is preferred over the **for** loop when working with arrays and collections. diff --git a/content/docs/expressions-statements-blocks.mdx b/content/docs/expressions-statements-blocks.mdx index a987060c..a428249c 100644 --- a/content/docs/expressions-statements-blocks.mdx +++ b/content/docs/expressions-statements-blocks.mdx @@ -1,6 +1,6 @@ --- title: Java Expressions, Statements and Blocks -description: In this tutorial, you will learn about Java expressions, Java statements, difference between expression and statement, and Java blocks with the help of examples. +description: In this tutorial, you will learn about Java expressions, Java statements, difference between expression and statement, and Java blocks with the help of examples. --- In previous chapters, we have used expressions, statements, and blocks without much explaining about them. Now that you know about variables, operators, and literals, it will be easier to understand these concepts. @@ -13,18 +13,21 @@ A Java expression consists of variables, operators, literals, and method calls. int score; score = 90; ``` + Here, `score = 90` is an expression that returns an `int`. Consider another example, ```java Double a = 2.2, b = 3.4, result; result = a + b - 3.4; ``` + Here, `a + b - 3.4` is an expression. ```java if (number1 == number2) System.out.println("Number 1 is larger than number 2"); ``` + Here, `number1 == number2` is an expression that returns a boolean value. Similarly, `"Number 1 is larger than number 2"` is a string expression. ## Java Statements @@ -34,6 +37,7 @@ In Java, each statement is a complete unit of execution. For example, ```java int score = 9*5; ``` + Here, we have a statement. The complete execution of this statement involves multiplying integers `9` and `5` and then assigning the result to the variable `score`. In the above statement, we have an expression `9 * 5`. In Java, expressions are part of statements. @@ -48,6 +52,7 @@ number = 10 // statement number = 10; ``` + In the above example, we have an expression `number = 10`. Here, by adding a semicolon (`;`), we have converted the expression into a statement (`number = 10;`). Consider another example, @@ -68,7 +73,9 @@ In Java, declaration statements are used for declaring variables. For example, ```java Double tax = 9.5; ``` + The statement above declares a variable tax which is initialized to 9.5. + **Note:** There are control flow statements that are used in decision making and looping in Java. You will learn about control flow statements in later chapters. @@ -92,11 +99,13 @@ class Main { } } ``` + Output: ```plaintext Hey Jude! ``` + In the above example, we have a block `if {....}`. Here, inside the block we have two statements: @@ -116,6 +125,7 @@ class Main { } } ``` + This is a valid Java program. Here, we have a block `if {...}`. However, there is no any statement inside this block. ```java @@ -125,4 +135,5 @@ class AssignmentOperator { } // end of block } ``` + Here, we have block `public static void main() {...}`. However, similar to the above example, this block does not have any statement. diff --git a/content/docs/final-keyword.mdx b/content/docs/final-keyword.mdx index a85257e5..1c57f58d 100644 --- a/content/docs/final-keyword.mdx +++ b/content/docs/final-keyword.mdx @@ -34,12 +34,14 @@ cannot assign a value to final variable AGE AGE = 45; ^ ``` - -The `final` keyword is often used to define constants in Java, which are typically named in uppercase letters. - + + The `final` keyword is often used to define constants in Java, which are + typically named in uppercase letters. + ## 2. final Method in Java + A `final` method cannot be overridden by subclasses. For example: ```java @@ -73,6 +75,7 @@ display() in Main cannot override display() in FinalDemo ``` ## 3. final Class in Java + A `final` class cannot be extended by any other class. For example: ```java @@ -102,4 +105,4 @@ In this example, `FinalClass` is declared `final`, so it cannot be subclassed by cannot inherit from final FinalClass class Main extends FinalClass { ^ -``` \ No newline at end of file +``` diff --git a/content/docs/for-loop.mdx b/content/docs/for-loop.mdx index 9c51c700..80e0fa47 100644 --- a/content/docs/for-loop.mdx +++ b/content/docs/for-loop.mdx @@ -36,14 +36,14 @@ To learn more about the conditions, visit [Java relational](operators#3-java-rel
- ![Working of Java if statement](/img/docs/working-of-for-loop.svg) -

Flowchart of Java for loop

+

+ Flowchart of Java for loop +

- ### Example 1: Display a Text Five Times #### Input @@ -76,13 +76,13 @@ Java is fun Here is how this program works. | Iteration | Variable | Condition: i `<=` n | Action | -| :-------: | :--------------: | :---------------: | :-------------------------------------------------: | -| 1st | `i = 1`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **2**. | -| 2nd | `i = 2`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **3**. | -| 3rd | `i = 3`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **4**. | -| 4th | `i = 4`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **5**. | -| 5th | `i = 5`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **6**. | -| 6th | `i = 6`, `n = 5` | `false` | The loop is terminated. | +| :-------: | :--------------: | :-----------------: | :-------------------------------------------------: | +| 1st | `i = 1`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **2**. | +| 2nd | `i = 2`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **3**. | +| 3rd | `i = 3`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **4**. | +| 4th | `i = 4`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **5**. | +| 5th | `i = 5`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **6**. | +| 6th | `i = 6`, `n = 5` | `false` | The loop is terminated. | ### Example 2: Display numbers from 1 to 5 @@ -116,13 +116,13 @@ class Main { Here is how the program works. | Iteration | Variable | Condition: i `<=` n | Action | -| :-------: | :--------------: | :---------------: | :---------------------------------------: | -| 1st | `i = 1`, `n = 5` | `true` | `1` is printed `i` is increased to **2**. | -| 2nd | `i = 2`, `n = 5` | `true` | `2` is printed `i` is increased to **3**. | -| 3rd | `i = 3`, `n = 5` | `true` | `3` is printed `i` is increased to **4**. | -| 4th | `i = 4`, `n = 5` | `true` | `4` is printed `i` is increased to **5**. | -| 5th | `i = 5`, `n = 5` | `true` | `5` is printed `i` is increased to **6**. | -| 6th | `i = 6`, `n = 5` | `false` | The loop is terminated. | +| :-------: | :--------------: | :-----------------: | :---------------------------------------: | +| 1st | `i = 1`, `n = 5` | `true` | `1` is printed `i` is increased to **2**. | +| 2nd | `i = 2`, `n = 5` | `true` | `2` is printed `i` is increased to **3**. | +| 3rd | `i = 3`, `n = 5` | `true` | `3` is printed `i` is increased to **4**. | +| 4th | `i = 4`, `n = 5` | `true` | `4` is printed `i` is increased to **5**. | +| 5th | `i = 5`, `n = 5` | `true` | `5` is printed `i` is increased to **6**. | +| 6th | `i = 6`, `n = 5` | `false` | The loop is terminated. | ### Example 3: Display Sum of n Natural Numbers diff --git a/content/docs/hello-world.mdx b/content/docs/hello-world.mdx index 75b16d2a..afdbb600 100644 --- a/content/docs/hello-world.mdx +++ b/content/docs/hello-world.mdx @@ -63,12 +63,10 @@ Notice the print statement is inside the main function, which is inside the clas ## Things to take away - - Every valid Java Application must have a class definition (that matches the filename) - The main method must be inside the class definition. - The compiler executes the codes starting from the main function. - This is a valid Java program that does nothing. ```java diff --git a/content/docs/if-else-statement.mdx b/content/docs/if-else-statement.mdx index a11e8ba8..e7db83be 100644 --- a/content/docs/if-else-statement.mdx +++ b/content/docs/if-else-statement.mdx @@ -3,7 +3,6 @@ title: Java if...else Statement description: In this tutorial, you will learn about control flow statements using Java if and if...else statements with the help of examples. --- - In programming, we use the `if..else` statement to run a block of code among more than one alternatives. For example, assigning grades (A, B, C) based on the percentage obtained by a student. @@ -13,6 +12,7 @@ For example, assigning grades (A, B, C) based on the percentage obtained by a st - if the percentage is above **65**, assign grade **C** ## 1. Java if (if-then) Statement + The syntax of an **if-then** statement is: ```java @@ -20,23 +20,24 @@ if (condition) { // statements } ``` + Here, `condition` is a boolean expression such as `age >= 18`. - if `condition` evaluates to `true` , statements are executed - if `condition` evaluates to `false` , statements are skipped - ## Working of if Statement
![Working of Java if statement](/img/docs/wroking-of-java-if-statement.svg) -

Working of Java if statement

+

+ Working of Java if statement +

- ### Example 1: Java if Statement ```java @@ -54,6 +55,7 @@ class IfStatement { } } ``` + #### Output ```plaintext @@ -85,6 +87,7 @@ class Main { } } ``` + #### Output ```plaintext @@ -94,6 +97,7 @@ Best Programming Language In the above example, we are comparing two strings in the `if` block. ## 2. Java if...else (if-then-else) Statement + The `if` statement executes a certain section of code if the test expression is evaluated to `true`. However, if the test expression is evaluated to `false`, it does nothing. In this case, we can use an optional `else` block. Statements inside the body of `else` block are executed if the test expression is evaluated to `false`. This is known as the **if-...else** statement in Java. @@ -108,6 +112,7 @@ else { // codes in else block } ``` + Here, the program will do one task (codes inside if block) if the condition is true and another task (codes inside else block) if the condition is false. ## How the if...else statement works? @@ -115,11 +120,12 @@ Here, the program will do one task (codes inside if block) if the condition is t
![Working of Java if statement](/img/docs/wroking-of-if-else-statement.svg) -

Working of Java if-else statements

+

+ Working of Java if-else statements +

- ### Example 3: Java if...else Statement ```java @@ -142,12 +148,14 @@ class Main { } } ``` + #### Output ```plaintext The number is positive. Statement outside if...else block ``` + In the above example, we have a variable named `number`. Here, the test expression `number > 0` checks if `number` is greater than 0. Since the value of the `number` is `10`, the test expression evaluates to `true`. Hence code inside the body of `if` is executed. @@ -157,15 +165,18 @@ Now, change the value of the `number` to a negative integer. Let's say `-5`. ```java int number = -5; ``` + If we run the program with the new value of `number`, the output will be: ```plaintext The number is not positive. Statement outside if...else block ``` + Here, the value of number is `-5`. So the test expression evaluates to `false`. Hence code inside the body of `else` is executed. ## 3. Java if...else...if Statement + In Java, we have an if...else...if ladder, that can be used to execute one block of code among multiple other blocks. ```java @@ -184,6 +195,7 @@ else { // codes } ``` + Here, `if` statements are executed from the top towards the bottom. When the test condition is `true`, codes inside the body of that `if` block is executed. And, program control jumps outside the **if...else...if** ladder. If all test expressions are `false`, codes inside the body of `else` are executed. @@ -224,6 +236,7 @@ class Main { ```plaintext The number is 0. ``` + In the above example, we are checking whether `number` is **positive**, **negative**, or **zero**. Here, we have two condition expressions: - `number > 0` - checks if `number` is greater than `0`. @@ -281,11 +294,13 @@ class Main { } } ``` + #### Output: ```plaintext Largest Number: 4.5 ``` + In the above programs, we have assigned the value of variables ourselves to make this easier. However, in real-world applications, these values may come from user input data, log files, form submission, etc. diff --git a/content/docs/index.mdx b/content/docs/index.mdx index 6705043e..a86de6d1 100644 --- a/content/docs/index.mdx +++ b/content/docs/index.mdx @@ -10,30 +10,51 @@ Welcome to the Javaistic documentation! Here, you can find everything you need t } href="/docs/introduction"> - Start with the fundamentals of Java programming, including syntax, data types, and control structures. + Start with the fundamentals of Java programming, including syntax, data + types, and control structures. - }> - Learn about control flow statements in Java, including if-else, switch-case, and loops. + } + > + Learn about control flow statements in Java, including if-else, switch-case, + and loops. }> - Understand how to work with arrays in Java, including declaration, initialization, and common operations. + Understand how to work with arrays in Java, including declaration, + initialization, and common operations. - }> - Dive into object-oriented programming concepts such as classes, objects, inheritance, and polymorphism. + } + > + Dive into object-oriented programming concepts such as classes, objects, + inheritance, and polymorphism. ## Get Involved - } href="https://github.com/javaistic/javaistic/discussions" > - Join the community on GitHub Discussions to ask questions, share ideas, and get help. + } + href="https://github.com/javaistic/javaistic/discussions" + > + Join the community on GitHub Discussions to ask questions, share ideas, and + get help. - } > - Join the Javaistic Discord server to chat with other Java enthusiasts in real-time. + }> + Join the Javaistic Discord server to chat with other Java enthusiasts in + real-time. - } > + } + > Follow us on Twitter for the latest updates, news, and announcements. - diff --git a/content/docs/installation.mdx b/content/docs/installation.mdx index 595260cb..f7bb7a72 100644 --- a/content/docs/installation.mdx +++ b/content/docs/installation.mdx @@ -1,10 +1,9 @@ --- title: Installation description: Learn how to get Java up and running in your project. -icon: Album +icon: Download --- - ## Installation Since installation process is slightly different in different devices. So we prefer that you should check the [official installation documentation](https://www.java.com/en/download/help/download_options.html) to install **☕ Java** on your device. @@ -13,19 +12,15 @@ Since installation process is slightly different in different devices. So we pre There are various types of code editors for programming in Java. The popular ones are listed below : - -| Bluej | VS Code | IntelliJ Idea | -| :----------------------------------------------:|:---------------------------------------------------------------: | :----------------------------------------------------------------------------: | +| Bluej | VS Code | IntelliJ Idea | +| :---------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------: | | [![BlueJ](/img/docs/installation/bluej.png)](https://bluej.org?ref=javaistic) | [![VS Code](/img/docs/installation/vs-code.png)](https://code.visualstudio.com/?ref=javaistic) | [![Intellij Idea](/img/docs/installation/intellij-idea.png)](https://www.jetbrains.com/idea/?ref=javaistic) | -| Free| Free | Free, Paid | - - You use any of the popular code editors for Java. But BlueJ is best as it is simple and fast. - +| Free | Free | Free, Paid | +You use any of the popular code editors for Java. But BlueJ is best as it is simple and fast. ## Learning After installing Java and setting up your preferred code editor start [learning](/docs/introduction) from our website. -
- +
diff --git a/content/docs/introduction.mdx b/content/docs/introduction.mdx index 2164df3f..d9ffa046 100644 --- a/content/docs/introduction.mdx +++ b/content/docs/introduction.mdx @@ -1,6 +1,7 @@ --- title: Introduction description: A guick intro to Java programming language +icon: Album --- **Java** is a powerful **general-purpose programming language**. It is used to **develop desktop and mobile applications, big data processing, embedded systems**, and so on. According to **Oracle**, the company that owns **Java**, **Java runs on 3 billion devices worldwide**, which makes Java one of the most popular programming languages. @@ -9,7 +10,6 @@ To get started with Java programming, visit [Java Tutorials](/programs/introduct ## Features of Java Programming - #### 1. Java is platform-independent Java was built with the philosophy of "write once, run anywhere" (WORA). The Java code you write on one platform (operating system) will run on other platforms with no modification. @@ -79,7 +79,12 @@ Besides these applications, Java is also used for game development, scientific a #### 1. Learn Java from Javaistic -Javaistic offers a complete series of easy to follow Java tutorials along with suitable examples. These tutorials are targeted for absolute beginners with no prior knowledge of Java programming language. + + Javaistic + +offers a complete series of easy to follow Java tutorials along with suitable +examples. These tutorials are targeted for absolute beginners with no prior +knowledge of Java programming language. #### 2. Learn Java from Books @@ -99,11 +104,18 @@ Oracle, the company that owns Java, provides quality Java documentation. The off You must be eager to learn Java by now. However, here are some tips and best practices to follow before you learn Java. - -- Don't read Java tutorials and examples like a novel : The only way to get better in programming is by writing a lot of code. -- Learn Java language the right way : If you are switching from another programming language (let's say C#), don't write C# style code in Java. Also, check this article on How to write good Java code? -- Join Java communities: Once you get the hang of writing simple Java programs, join Java communities and forums. Then, try to solve other programmer's Java problems. It's a good way to expand your Java knowledge. Also, you can get help when you are stuck. - +- Don't read Java tutorials and examples like a novel : The only way to + get better in programming is by writing a lot of code. +- Learn Java language the right way : If you are switching from another + programming language (let's say C#), don't write C# style code in Java. Also, + check this article on + + How to write good Java code? + +- Join Java communities: Once you get the hang of writing simple Java + programs, join Java communities and forums. Then, try to solve other + programmer's Java problems. It's a good way to expand your Java knowledge. + Also, you can get help when you are stuck. ## Final Words diff --git a/content/docs/jvm-jre-jdk.mdx b/content/docs/jvm-jre-jdk.mdx index 48dc254a..e09cab0d 100644 --- a/content/docs/jvm-jre-jdk.mdx +++ b/content/docs/jvm-jre-jdk.mdx @@ -1,6 +1,7 @@ --- title: Java JDK, JRE and JVM description: Definations of JDK, JRE and JVM +icon: Cpu --- In this tutorial, you will learn about [JDK](#what-is-jdk), [JRE](#what-is-jre), and [JVM](#what-is-jvm). You will also learn the key differences between them. @@ -14,8 +15,10 @@ When you run the Java program, Java compiler first compiles your Java code to by Java is a **platform-independent language**. It's because when you write Java code, it's ultimately written for JVM but not your physical machine (computer). Since JVM ​executes the Java bytecode which is platform-independent, Java is platform-independent.
-![Working of a Java Program](/img/docs/jvm-jre-jdk/1.svg) -

Working of a Java Program

+ ![Working of a Java Program](/img/docs/jvm-jre-jdk/1.svg) +

+ Working of a Java Program +

If you are interested in learning about JVM Architecture, visit [The JVM Architecture Explained](https://dzone.com/articles/jvm-architecture-explained). @@ -27,13 +30,12 @@ If you are interested in learning about JVM Architecture, visit [The JVM Archite JRE is the superset of JVM.
-![Java Runtime Environment](/img/docs/jvm-jre-jdk/2.svg) -

- Java Runtime Environment -

+ ![Java Runtime Environment](/img/docs/jvm-jre-jdk/2.svg) +

+ Java Runtime Environment +

- If you need to run Java programs, but not develop them, JRE is what you need. You can download JRE from [Java SE Runtime Environment 8 Downloads page](http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html). ## What is JDK? @@ -46,7 +48,7 @@ In addition to JRE, JDK also contains a number of development tools (compilers, ![Java Development Kit](/img/docs/jvm-jre-jdk/3.svg) -

+

Java Development Kit

@@ -61,7 +63,7 @@ If you want to develop Java applications, [download JDK](http://www.oracle.com/t ![Relationship between JVM, JRE, and JDK](/img/docs/jvm-jre-jdk/4.svg) -

+

Relationship between JVM, JRE, and JDK

diff --git a/content/docs/method-overloading.mdx b/content/docs/method-overloading.mdx index 31c518a8..168734f3 100644 --- a/content/docs/method-overloading.mdx +++ b/content/docs/method-overloading.mdx @@ -3,8 +3,8 @@ title: Java Method Overloading description: In this tutorial, we will learn how to implements methods overloading or function overloading in Java. --- - ## What is Method Overloading in Java? + In Java, method overloading allows multiple methods with the same name but different parameter lists to coexist in the same class. This can involve variations in the number or types of parameters, or both. Overloaded methods provide flexibility by offering different ways to call a method, depending on the parameters passed. ### Example: @@ -15,18 +15,22 @@ void func(int a) {} float func(double a) {} float func(int a, float b) {} ``` + In this example, the `func()` method is overloaded with different parameter configurations. While the return types differ, this does not affect method overloading; overloading is purely based on parameters. ## Why Use Method Overloading? + Consider the scenario where you need to sum numbers but could have different parameter requirements (e.g., 2 or 3 numbers). You could create separate methods like `sum2(int, int)` and `sum3(int, int, int)`. However, using method overloading allows a more readable approach by keeping the method name the same: ```java int sum(int a, int b) { ... } int sum(int a, int b, int c) { ... } ``` + Here, `sum()` is overloaded to handle different numbers of arguments. ## Performing Method Overloading in Java + Method overloading can be achieved in two main ways: ### 1. Changing the Number of Parameters @@ -47,6 +51,7 @@ class MethodOverloading { } } ``` + ### Output ```bash @@ -72,13 +77,16 @@ class MethodOverloading { } } ``` + ### Output ```bash Got Integer data. Got String object. ``` + ## Real-World Example + In a utility class, you might use overloading to format numbers: ```java @@ -103,7 +111,9 @@ class HelperService { } } ``` + ### Output + ```bash 500 89.993 @@ -111,12 +121,13 @@ class HelperService { ``` ## Important Points + - Method overloading requires methods with different parameters within the same class. - Overloading is achieved by varying either the number or type of parameters. - Changing only the return type does not constitute overloading; there must be a parameter difference. -**Tip:** *Constructor overloading in Java works similarly to method overloading.* +**Tip:** _Constructor overloading in Java works similarly to method overloading._ diff --git a/content/docs/methods.mdx b/content/docs/methods.mdx index e35541f6..5fc74f28 100644 --- a/content/docs/methods.mdx +++ b/content/docs/methods.mdx @@ -3,7 +3,6 @@ title: Java Methods description: In this tutorial, we will learn how to implements methods or functions in Java. --- - ## What is a Method in Java? In Java, a method is a collection of statements designed to perform specific tasks and, optionally, return a result to the caller. A Java method can also execute a task without returning any value. Methods enable code reuse by eliminating the need to retype code. Importantly, in Java, every method must belong to a class. @@ -22,6 +21,7 @@ returnType methodName() { // method body } ``` + 1. **returnType:** Specifies the type of value returned by the method. If it doesn’t return a value, the return type is void. 2. **methodName:** The identifier used to call the method. 3. **method body:** Code inside `{ }` that defines the task to be performed. @@ -33,6 +33,7 @@ int addNumbers() { // code } ``` + In this example, `addNumbers()` is the method name, and its return type is `int`. ### For a more detailed declaration: @@ -42,14 +43,17 @@ modifier static returnType methodName(parameter1, parameter2, ...) { // method body } ``` + 1. **modifier:** Defines access type (e.g., public, private). 2. **static:** If included, the method can be called without creating an object. 3. **parameter1, parameter2, ...parameterN:** Values passed to the method. ### Example 2: + The `sqrt()` method in the Math class is static, so it can be accessed as `Math.sqrt()` without creating an instance of Math. ## Calling a Method in Java + To use a method, call it by name followed by parentheses. ### Java Method Call Example @@ -78,6 +82,7 @@ Sum is: 40 Here, the `addNumbers()` method takes two parameters and returns their sum. Since it's not static, it requires an object to be called. ## Method Return Types + A method may or may not return a value. The return statement is used to return a value. ### Example 1: Method returns integer value @@ -98,15 +103,18 @@ public void printSquare(int num) { ``` ## Method Parameters + Methods can accept parameters, which are values passed to the method. ### Examples + - **With Parameters:** `int addNumbers(int a, int b)` - **Without Parameters:** `int addNumbers()` When calling a method with parameters, you must provide values for each parameter. ### Example of Method Parameters + ```java class Main { public void display1() { @@ -124,6 +132,7 @@ class Main { } } ``` + ### Output ```bash @@ -133,14 +142,16 @@ Method with a single parameter: 24 -**Note:** *Java requires that argument types match the parameter types.* +**Note:** _Java requires that argument types match the parameter types._ ## Standard Library Methods + Java includes built-in methods available in the Java Class Library (JCL), which comes with the [JVM and JRE](/docs/jvm-jre-jdk). ### Example + ```java public class Main { public static void main(String[] args) { @@ -148,25 +159,30 @@ public class Main { } } ``` + ### Output + ```bash Square root of 4 is: 2.0 ``` ## Advantages of Using Methods + ### 1. Code Reusability + Define once, use multiple times. - ```java +```java private static int getSquare(int x) { - return x * x; + return x * x; } public static void main(String[] args) { - for (int i = 1; i <= 5; i++) { - System.out.println("Square of " + i + " is: " + getSquare(i)); - } + for (int i = 1; i <= 5; i++) { + System.out.println("Square of " + i + " is: " + getSquare(i)); + } } ``` + ### Output: ```bash @@ -178,4 +194,5 @@ Square of 5 is: 25 ``` ### 2. Improved Readability + Grouping code into methods makes it easier to read and debug. diff --git a/content/docs/multidimensional-arrays.mdx b/content/docs/multidimensional-arrays.mdx index c5e5a008..f4b34899 100644 --- a/content/docs/multidimensional-arrays.mdx +++ b/content/docs/multidimensional-arrays.mdx @@ -3,7 +3,6 @@ title: Java Multidimensional Arrays description: In this tutorial, we will learn how to use the Java continue statement to skip the current iteration of a loop. --- -## Java Multidimensional Arrays Before we learn about the multidimensional array, make sure you know about Java array. A multidimensional array is an array of arrays. Each element of a multidimensional array is an array itself. For example, @@ -11,6 +10,7 @@ A multidimensional array is an array of arrays. Each element of a multidimension ```java int[][] a = new int[3][4]; ``` + Here, we have created a multidimensional array named a. It is a 2-dimensional array, that can hold a maximum of 12 elements, [2-dimensional array in Java] @@ -23,9 +23,11 @@ Let's take another example of the multidimensional array. This time we will be c ```java String[][][] data = new String[3][4][2]; ``` + Here, data is a 3d array that can hold a maximum of 24 (3*4*2) elements of type [String](/docs/string). ### How to initialize a 2d array in Java? + Here is how we can initialize a 2-dimensional array in Java. ```java @@ -35,13 +37,14 @@ int[][] a = { {7}, }; ``` -As we can see, each element of the multidimensional array is an array itself. And also, unlike C/C++, each row of the multidimensional array in Java can be of different lengths. +As we can see, each element of the multidimensional array is an array itself. And also, unlike C/C++, each row of the multidimensional array in Java can be of different lengths. [2d array example in Java with variable length] Initialization of 2-dimensional Array ### Example: 2-dimensional Array + ```java class MultidimensionalArray { public static void main(String[] args) { @@ -60,6 +63,7 @@ class MultidimensionalArray { } } ``` + #### Output: ```plaintext @@ -67,6 +71,7 @@ Length of row 1: 3 Length of row 2: 4 Length of row 3: 1 ``` + In the above example, we are creating a multidimensional array named a. Since each component of a multidimensional array is also an array (`a[0]`,`a[1]` and `a[2]` are also arrays). Here, we are using the `length` attribute to calculate the length of each row. @@ -91,6 +96,7 @@ class MultidimensionalArray { } } ``` + #### Output: ```plaintext @@ -103,6 +109,7 @@ class MultidimensionalArray { 9 7 ``` + We can also use the [for...each loop](/docs/enhanced-for-loop) to access elements of the multidimensional array. For example, ```java @@ -127,6 +134,7 @@ class MultidimensionalArray { } } ``` + Output: ```plaintext @@ -139,9 +147,11 @@ Output: 9 7 ``` + In the above example, we are have created a 2d array named `a`. We then used `for` loop and `for...each` loop to access each element of the array. ### How to initialize a 3d array in Java? + Let's see how we can use a 3d array in Java. We can initialize a 3d array similar to the 2d array. For example, ```java @@ -158,9 +168,11 @@ int[][][] test = { } }; ``` + Basically, a 3d array is an array of 2d arrays. The rows of a 3d array can also vary in length just like in a 2d array. ### Example: 3-dimensional Array + ```java class ThreeArray { public static void main(String[] args) { @@ -189,6 +201,7 @@ class ThreeArray { } } ``` + #### Output: ```plaintext @@ -205,4 +218,4 @@ class ThreeArray { 1 2 3 -``` \ No newline at end of file +``` diff --git a/content/docs/operators.mdx b/content/docs/operators.mdx index 9d8c1dab..d42afe89 100644 --- a/content/docs/operators.mdx +++ b/content/docs/operators.mdx @@ -1,12 +1,8 @@ --- title: Java Operators description: In this tutorial, you'll learn about different types of operators in Java, their syntax and how to use them with the help of examples. - --- - - - Operators are symbols that perform operations on variables and values. For example,`+` is an operator used for addition, while `*` is also an operator used for multiplication. Operators in Java can be classified into 5 types: @@ -18,24 +14,23 @@ Operators in Java can be classified into 5 types: 5. Unary Operators 6. Bitwise Operators - ## 1. Java Arithmetic Operators + Arithmetic operators are used to perform arithmetic operations on variables and data. For example, ```java a + b; ``` -Here, the `+` operator is used to add two variables `a` and `b`. Similarly, there are various other arithmetic operators in Java. +Here, the `+` operator is used to add two variables `a` and `b`. Similarly, there are various other arithmetic operators in Java. -| Operator | Operation | -| :---------: | :-------: | -| **+** |Addition | -| **-** |Substraction | -| ***** |Multiplication | -| **/** |Division | -| **%** |Modulus Operation (Remainder after division) | - +| Operator | Operation | +| :------: | :------------------------------------------: | +| **+** | Addition | +| **-** | Substraction | +| **\*** | Multiplication | +| **/** | Division | +| **%** | Modulus Operation (Remainder after division) | #### Example 1: Arithmetic Operators @@ -63,6 +58,7 @@ class Main { } } ``` + #### Output ```plaintext @@ -72,6 +68,7 @@ a * b = 60 a / b = 2 a % b = 2 ``` + In the above example, we have used `+`, `-`, and `*` operators to compute addition, subtraction, and multiplication operations. ### / Division Operator @@ -88,6 +85,7 @@ In Java, (9 / 2.0) is 4.5 (9.0 / 2.0) is 4.5 ``` + ### % Modulo Operator The modulo operator `%` computes the remainder. When `a = 7` is divided by `b = 4`, the remainder is **3**. @@ -99,24 +97,26 @@ The modulo operator `%` computes the remainder. When `a = 7` is divided by `b = ## 2. Java Assignment Operators + Assignment operators are used in Java to assign values to variables. For example, ```java int age; age = 5; ``` + Here, = is the assignment operator. It assigns the value on its right to the variable on its left. That is, 5 is assigned to the variable age. Let's see some more assignment operators available in Java. -|Operator | Example | Equivalent to | -|:-------:|:-------:|:-------------:| -| = | a = b; | a = b; | -| += | a += b; | a = a + b; | -| -= | a -= b; | a = a - b; | -| *= | a *= b; | a = a * b; | -| /= | a /= b; | a = a / b; | -| %= | a %= b; | a = a % b; | +| Operator | Example | Equivalent to | +| :------: | :------: | :-----------: | +| = | a = b; | a = b; | +| += | a += b; | a = a + b; | +| -= | a -= b; | a = a - b; | +| \*= | a \*= b; | a = a \* b; | +| /= | a /= b; | a = a / b; | +| %= | a %= b; | a = a % b; | #### Example 2: Assignment Operators @@ -142,6 +142,7 @@ class Main { } } ``` + Output ```plaintext @@ -151,24 +152,26 @@ var using *=: 32 ``` ## 3. Java Relational Operators + Relational operators are used to check the relationship between two operands. For example, ```java // check is a is less than b a < b; ``` + Here, `>` operator is the relational operator. It checks if `a` is less than `b` or not. It returns either true or false. -| Operator | Description | Example | -| :------: | :---------: | :------: | -| == | Is Equal To | 3 == 5 returns false | -| != | Not Equal To | 3 != 5 returns true | -| `>` | Greater Than | 3 `>` 5 returns false | -| `<` | Less Than | 3 `<` 5 returns true | -| `>=` | Greater Than or Equal To | 3 `>=` 5 returns false | -| `<=` | Less Than or Equal To | 3 `<=` 5 returns true | +| Operator | Description | Example | +| :------: | :----------------------: | :--------------------: | +| == | Is Equal To | 3 == 5 returns false | +| != | Not Equal To | 3 != 5 returns true | +| `>` | Greater Than | 3 `>` 5 returns false | +| `<` | Less Than | 3 `<` 5 returns true | +| `>=` | Greater Than or Equal To | 3 `>=` 5 returns false | +| `<=` | Less Than or Equal To | 3 `<=` 5 returns true | #### Example 3: Relational Operators @@ -202,7 +205,8 @@ class Main { } } ``` -
+ +
@@ -211,17 +215,18 @@ class Main { ## 4. Java Logical Operators + Logical operators are used to check whether an expression is true or false. They are used in decision making. - + - - + + @@ -237,7 +242,7 @@ Logical operators are used to check whether an expression is true or false. They - +
Operator Example Meaning
&&(Logical AND) expression1 && expression2 !expression true if expression is false and vice versa
#### Example 4: Logical Operators @@ -273,19 +278,21 @@ class Main { - !(5 > 3) returns false because 5 > 3 is true. ## 5. Java Unary Operators + Unary operators are used with only one operand. For example, ++ is a unary operator that increases the value of a variable by 1. That is, ++5 will return 6. Different types of unary operators are: -| Operator | Meaning | -| :------: | :------: | -| + | Unary plus: not necessary to use since numbers are positive without using it | -| - | Unary minus: inverts the sign of an expression | -| ++| Increment operator: increments value by 1 | -|-- | Decrement operator: decrements value by 1 | -| ! | Logical complement operator: inverts the value of a boolean | +| Operator | Meaning | +| :------: | :--------------------------------------------------------------------------: | +| + | Unary plus: not necessary to use since numbers are positive without using it | +| - | Unary minus: inverts the sign of an expression | +| ++ | Increment operator: increments value by 1 | +| -- | Decrement operator: decrements value by 1 | +| ! | Logical complement operator: inverts the value of a boolean | ### Increment and Decrement Operators + Java also provides increment and decrement operators: **++** and **- -** respectively. **++** increases the value of the operand by **1**, while **- -** decrease it by **1**. For example, ```java @@ -294,6 +301,7 @@ int num = 5; // increase num by 1 ++num; ``` + Here, the value of `num` gets increased to **6** from its initial value of **5**. #### Example 5: Increment and Decrement Operators @@ -321,6 +329,7 @@ class Main { } } ``` + #### Output ```plaintext @@ -329,12 +338,13 @@ After increment: 13 Value of b: 12 After decrement: 11 ``` + In the above program, we have used the ++ and - - operator as **prefixes (++a, - -b)**. We can also use these operators as **postfix (a++, b++)**. There is a slight difference when these operators are used as prefix versus when they are used as a postfix. - ## 6. Java Bitwise Operators + Bitwise operators in Java are used to perform operations on individual bits. For example, ```plaintext @@ -351,21 +361,23 @@ Here, `~` is a bitwise operator. It inverts the value of each bit (**0** to **1* The various bitwise operators present in Java are: -| Operator | Description | -| :------: | :------: | -| ~ | Bitwise Complement | -| `<<` | Left Shift | -| >> | Right Shift | -|>>> | Unsigned Right Shift | -| & | Bitwise AND | -| ^ | Bitwise exclusive OR | +| Operator | Description | +| :------: | :------------------: | +| ~ | Bitwise Complement | +| `<<` | Left Shift | +| >> | Right Shift | +| >>> | Unsigned Right Shift | +| & | Bitwise AND | +| ^ | Bitwise exclusive OR | These operators are not generally used in Java. To learn more, visit [Java Bitwise and Bit Shift Operators](/bitwise-and-bit-shift-operators-in-java). ## 7. Other operators + Besides these operators, there are other additional operators in Java. ### Java instanceof Operator + The `instanceof` operator checks whether an object is an instanceof a particular class. For example, ```java @@ -382,24 +394,28 @@ class Main { } } ``` + Output ```plaintext Is str an object of String? true ``` + Here, str is an instance of the String class. Hence, the instanceof operator returns true. To learn more, visit Java instanceof. ### Java Ternary Operator + The ternary operator (conditional operator) is shorthand for the if-then-else statement. For example, ```java variable = Expression ? expression1 : expression2 ``` + Here's how it works. - If the `Expression` is `true`, `expression1` is assigned to the `variable`. - If the `Expression` is `false`, `expression2` is assigned to the `variable`. -Let's see an example of a ternary operator. + Let's see an example of a ternary operator. ```java class Java { @@ -414,10 +430,13 @@ class Java { } } ``` + Output + ```plaintext Leap year ``` + In the above example, we have used the ternary operator to check if the year is a leap year or not. To learn more, visit the [Java ternary operator](/docs/operators#java-ternary-operator). Now that you know about Java operators, it's time to know about the order in which operators are evaluated. To learn more, visit [Java Operator Precedence](/java-operator-precedence). diff --git a/content/docs/static-keyword.mdx b/content/docs/static-keyword.mdx index 2e962a74..94a9dd1b 100644 --- a/content/docs/static-keyword.mdx +++ b/content/docs/static-keyword.mdx @@ -23,6 +23,7 @@ public class Main { } } ``` + ### Output: ```bash @@ -31,6 +32,7 @@ Value of PI = 3.141592653589793 Value of E = 2.718281828459045 2^2 = 4.0 ``` + In the above example, we have not created any instances of the Math class. But we are able to access its methods: `abs()` and `pow()` and variables: `PI` and `E`. It is possible because the methods and variables of the Math class are static. diff --git a/content/docs/switch-statement.mdx b/content/docs/switch-statement.mdx index da6f345f..99fe64e0 100644 --- a/content/docs/switch-statement.mdx +++ b/content/docs/switch-statement.mdx @@ -25,6 +25,7 @@ switch (expression) { // default statements } ``` + ## How does the switch-case statement work? The `expression` is evaluated once and compared with the values of each case. @@ -81,11 +82,13 @@ class Main { } } ``` + #### Output: ```plaintext Size: Large ``` + In the above example, we have used the switch statement to find the size. Here, we have a variable `number`. The variable is compared with the value of each case statement. Since the value matches with **44**, the code of `case 44` is executed. @@ -94,8 +97,8 @@ Since the value matches with **44**, the code of `case 44` is executed. size = "Large"; break; ``` -Here, the `size` variable is assigned with the value `Large`. +Here, the `size` variable is assigned with the value `Large`. ## Flowchart of switch Statement @@ -103,7 +106,9 @@ Here, the `size` variable is assigned with the value `Large`. ![Working of Java if statement](/img/docs/switch-case-flowchart.svg) -

Flow chart of the Java switch statement

+

+ Flow chart of the Java switch statement +

@@ -118,6 +123,7 @@ case 29: break; ... ``` + The `break` statement is used to terminate the **switch-case** statement. If `break` is not used, all the cases after the matching case are also executed. For example, #### Input @@ -154,6 +160,7 @@ Case 2 Case 3 Default case ``` + In the above example, `expression` matches with `case 2`. Here, we haven't used the break statement after each case. Hence, all the cases after `case 2` are also executed. @@ -189,6 +196,7 @@ class Main { } } ``` + #### Output ```plaintext @@ -203,17 +211,33 @@ Hence, the code inside the **default case** is executed. default: System.out.println("Unknown Size); ``` -
+ +
**Note:** The Java switch statement only works with:
    -
  • Primitive data types: byte, short, char, and int
  • -
  • Enumerated types
  • -
  • String Class
  • -
  • Wrapper Classes: Character, Byte, Short, and Integer.
  • +
  • + {" "} + + Primitive data types: + {" "} + byte, short, char, and int{" "} +
  • +
  • + {" "} + Enumerated types{" "} +
  • +
  • + {" "} + String Class{" "} +
  • +
  • + {" "} + Wrapper Classes: Character, Byte, Short, and Integer. +
diff --git a/content/docs/this-keyword.mdx b/content/docs/this-keyword.mdx index b886788a..7ada17b2 100644 --- a/content/docs/this-keyword.mdx +++ b/content/docs/this-keyword.mdx @@ -4,9 +4,11 @@ description: In this tutorial, we will learn the use of 'this' keyword in Java. --- ## What is 'this' keyword in Java? + In Java, the `this` keyword is used within a class to refer to the current instance (object) of that class. It’s commonly used in methods or constructors to reference the current object’s properties or to call other constructors within the same class. ### Example + ```java class Main { int instVar; @@ -22,11 +24,14 @@ class Main { } } ``` + ### Output + ```bash this reference = Main@23fc625e object reference = Main@23fc625e ``` + In this example, `this` refers to the `obj` instance of Main. Both `this` and `obj` share the same memory reference, indicating that this represents the current object. ## Common Uses of the this Keyword @@ -44,6 +49,7 @@ class MyClass { } } ``` + Without `this`: ```java @@ -59,6 +65,7 @@ class Main { } } ``` + ### 2. Using this with Getters and Setters `this` is often used in setter methods to differentiate between instance variables and parameters: @@ -82,12 +89,15 @@ class Main { } } ``` + ### Output + ```bash obj.name: Toshiba ``` ### 3. Constructor Overloading with this() + In constructor overloading, `this()` can be used to call another constructor within the same class, reducing code duplication: ```java @@ -126,9 +136,11 @@ class Complex { } } ``` + Here, `this()` helps to manage multiple constructors, improving readability and reducing code duplication. However, it should be used carefully, as excessive use can slow down the program. ### 4. Passing this as an Argument + You can use `this` to pass the current object as an argument to other methods: ```java @@ -159,6 +171,7 @@ class ThisExample { } } ``` + ### Output ```bash @@ -167,9 +180,11 @@ x = 1, y = -2 After passing this: x = 3, y = 0 ``` + In this example, `this` allows the `add()` method to modify the current instance variables directly. ## Key Points + - **Variable Disambiguation:** `this` resolves conflicts when instance variables and parameters share the same name. - **Getters and Setters:** `this` is often used in setters to distinguish instance variables. - **Constructor Overloading:** `this()` can call other constructors within the same class, helping to reduce code redundancy. diff --git a/content/docs/variables-and-literals.mdx b/content/docs/variables-and-literals.mdx index a485aa8f..031085c3 100644 --- a/content/docs/variables-and-literals.mdx +++ b/content/docs/variables-and-literals.mdx @@ -35,7 +35,8 @@ You can declare variables and assign variables separately. For example, int speedLimit; speedLimit = 80; ``` -

+ +

@@ -184,12 +185,13 @@ int binNumber = 0b10010; // 0b represents binary In Java, binary starts with **0b**, octal starts with **0**, and hexadecimal starts with **0x**. -

+

**Note :** Integer literals are used to initialize variables of integer types like `byte` , `short` , `int` , and `long` . + --- @@ -213,7 +215,8 @@ class Main { } } ``` -

+ +

@@ -223,6 +226,7 @@ type variables. --- + ### 4. Character Literals Character literals are unicode character enclosed inside single quotes. For example, diff --git a/content/docs/variables-primitive-data-types.mdx b/content/docs/variables-primitive-data-types.mdx index 25b048fb..986cae32 100644 --- a/content/docs/variables-primitive-data-types.mdx +++ b/content/docs/variables-primitive-data-types.mdx @@ -21,178 +21,178 @@ There are 8 data types predefined in Java programming language, known as primiti - **Note :** In addition to primitive data types, there are also referenced - types (object type). +**Note :** In addition to primitive data types, there are also referenced +types (object type). ## 8 Primitive Data Types - ### 1. boolean type +### 1. boolean type - - The `boolean` data type has two possible values, either true or false. - - They are usually used for **true/false** conditions. - - **Default value : `false`**. +- The `boolean` data type has two possible values, either true or false. +- They are usually used for **true/false** conditions. +- **Default value : `false`**. - #### Example 1: Java `boolean` data type +#### Example 1: Java `boolean` data type - ```java - class Main { - public static void main(String[] args) { - boolean flag = true; - System.out.println(flag); // prints true - } - } - ``` +```java +class Main { + public static void main(String[] args) { + boolean flag = true; + System.out.println(flag); // prints true + } +} +``` ### 2. byte type - - The `byte` data type can have values from **-128** to **127** (8-bit signed two's complement integer). - - If it's certain that the value of a variable will be within -128 to 127, then it is used instead of int to save memory. - - **Default value : 0** +- The `byte` data type can have values from **-128** to **127** (8-bit signed two's complement integer). +- If it's certain that the value of a variable will be within -128 to 127, then it is used instead of int to save memory. +- **Default value : 0** - #### Example 2: Java `byte` data type +#### Example 2: Java `byte` data type - ```java - class Main { - public static void main(String[] args) { - byte range; - range = 124; - System.out.println(range); // prints 124 - } - } - ``` +```java +class Main { + public static void main(String[] args) { + byte range; + range = 124; + System.out.println(range); // prints 124 + } +} +``` ### 3. short type - - The `short` data type in Java can have values from **-32768** to **32767** (16-bit signed two's complement integer). - - If it's certain that the value of a variable will be within -32768 and 32767, then it is used instead of other integer data types (`int`, `long`). - - **Default value : 0** +- The `short` data type in Java can have values from **-32768** to **32767** (16-bit signed two's complement integer). +- If it's certain that the value of a variable will be within -32768 and 32767, then it is used instead of other integer data types (`int`, `long`). +- **Default value : 0** - #### Example 3: Java `short` data type +#### Example 3: Java `short` data type - ```java - class Main { - public static void main(String[] args) { - short temperature; - temperature = -200; - System.out.println(temperature); // prints -200 - } - } - ``` +```java +class Main { + public static void main(String[] args) { + short temperature; + temperature = -200; + System.out.println(temperature); // prints -200 + } +} +``` ### 4. int type - - The int data type can have values from **-231** to **231 -1** (32-bit signed two's complement integer). - - If you are using Java 8 or later, you can use an unsigned 32-bit integer. This will have a minimum value of 0 and a maximum value of 232-1. To learn more, visit [How to use the unsigned integer in java 8?](http://stackoverflow.com/questions/25556017/how-to-use-the-unsigned-integer-in-java-8) - - **Default value : 0** +- The int data type can have values from **-231** to **231 -1** (32-bit signed two's complement integer). +- If you are using Java 8 or later, you can use an unsigned 32-bit integer. This will have a minimum value of 0 and a maximum value of 232-1. To learn more, visit [How to use the unsigned integer in java 8?](http://stackoverflow.com/questions/25556017/how-to-use-the-unsigned-integer-in-java-8) +- **Default value : 0** - #### Example 4: Java `int` data type +#### Example 4: Java `int` data type - ```java - class Main { - public static void main(String[] args) { - int range = -4250000; - System.out.println(range); // print -4250000 - } - } - ``` +```java +class Main { + public static void main(String[] args) { + int range = -4250000; + System.out.println(range); // print -4250000 + } +} +``` ### 5. long type - - The `long` data type can have values from **-263** to **263 -1** (64-bit signed two's complement integer). - - If you are using Java 8 or later, you can use an unsigned 64-bit integer with a minimum value of 0 and a maximum value of 264 -1. - - **Default value : 0** +- The `long` data type can have values from **-263** to **263 -1** (64-bit signed two's complement integer). +- If you are using Java 8 or later, you can use an unsigned 64-bit integer with a minimum value of 0 and a maximum value of 264 -1. +- **Default value : 0** - #### Example 5: Java `long` data type +#### Example 5: Java `long` data type - ```java - class LongExample { - public static void main(String[] args) { - long range = -42332200000L; - System.out.println(range); // prints -42332200000 - } - } - ``` +```java +class LongExample { + public static void main(String[] args) { + long range = -42332200000L; + System.out.println(range); // prints -42332200000 + } +} +``` - Notice, the use of `L` at the end of `-42332200000`. This represents that it's an integral literal of the `long` type. You will learn about integral literals later in this article. +Notice, the use of `L` at the end of `-42332200000`. This represents that it's an integral literal of the `long` type. You will learn about integral literals later in this article. ### 6. double type - - The `double` data type is a double-precision 64-bit floating-point. - - It should never be used for precise values such as currency. - - **Default value : 0.0 (0.0d)** +- The `double` data type is a double-precision 64-bit floating-point. +- It should never be used for precise values such as currency. +- **Default value : 0.0 (0.0d)** - #### Example 6: Java `double` data type +#### Example 6: Java `double` data type - ```java - class Main { - public static void main(String[] args) { - double number = -42.3; - System.out.println(number); // prints -42.3 - } - } - ``` +```java +class Main { + public static void main(String[] args) { + double number = -42.3; + System.out.println(number); // prints -42.3 + } +} +``` ### 7. float type - - The `float` data type is a single-precision 32-bit floating-point.Learn more about [single-precision and double-precision floating-point](http://stackoverflow.com/questions/801117/whats-the-difference-between-a-single-precision-and-double-precision-floating-p) if you are interested. - - It should never be used for precise values such as currency. - - **Default value : 0.0 (0.0f)** +- The `float` data type is a single-precision 32-bit floating-point.Learn more about [single-precision and double-precision floating-point](http://stackoverflow.com/questions/801117/whats-the-difference-between-a-single-precision-and-double-precision-floating-p) if you are interested. +- It should never be used for precise values such as currency. +- **Default value : 0.0 (0.0f)** - #### Example 7: Java `float` data type +#### Example 7: Java `float` data type - ```java highlight=3 - class Main { - public static void main(String[] args) { - float number = -42.3f; - System.out.println(number); // prints -42.3 - } - } - ``` +```java highlight=3 +class Main { + public static void main(String[] args) { + float number = -42.3f; + System.out.println(number); // prints -42.3 + } +} +``` - Notice that, we have used `-42.3f` instead of `-42.3` in the above program. It's because `-42.3` is a `double` literal. - To tell the compiler to treat `-42.3` as `float` rather than `double`, you need to use `f` or `F`. - If you want to know about single-precision and double-precision, visit Java single-precision and double-precision floating-point. +Notice that, we have used `-42.3f` instead of `-42.3` in the above program. It's because `-42.3` is a `double` literal. +To tell the compiler to treat `-42.3` as `float` rather than `double`, you need to use `f` or `F`. +If you want to know about single-precision and double-precision, visit Java single-precision and double-precision floating-point. ### 8. char type - - It's a 16-bit Unicode character. - - The minimum value of the `char` data type is `'\u0000'` (0) and the maximum value of the is `'\uffff'`. - - **Default value : '\u0000'** +- It's a 16-bit Unicode character. +- The minimum value of the `char` data type is `'\u0000'` (0) and the maximum value of the is `'\uffff'`. +- **Default value : '\u0000'** - #### Example 8: Java `char` data type +#### Example 8: Java `char` data type - ```java - class Main { - public static void main(String[] args) { - char letter = '\u0051'; - System.out.println(letter); // prints Q - } - } - ``` - - Here, the Unicode value of Q is `\u0051`. Hence, we get Q as the output. +```java +class Main { + public static void main(String[] args) { + char letter = '\u0051'; + System.out.println(letter); // prints Q + } +} +``` - Here is another example: +Here, the Unicode value of Q is `\u0051`. Hence, we get Q as the output. - ```java - class Main { - public static void main(String[] args) { - char letter1 = '9'; - System.out.println(letter1); // prints 9 - char letter2 = 65; - System.out.println(letter2); // prints A +Here is another example: - } - } - ``` +```java +class Main { + public static void main(String[] args) { + char letter1 = '9'; + System.out.println(letter1); // prints 9 + char letter2 = 65; + System.out.println(letter2); // prints A + } +} +``` Here, we have assigned 9 as a character (specified by single quotes) to the letter1 variable. However, the letter2 variable is assigned 65 as an integer number (no single quotes). Hence, A is printed to the output. It is because Java treats characters as integral types and the ASCII value of A is 65. To learn more about ASCII, visit What is ASCII Code?. ## String type + Java also provides support for character strings via java.lang.String class. Strings in Java are not primitive types. Instead, they are objects. For example, ```java diff --git a/content/docs/while-and-do-while-loop.mdx b/content/docs/while-and-do-while-loop.mdx index 0ed813a6..2fd1b350 100644 --- a/content/docs/while-and-do-while-loop.mdx +++ b/content/docs/while-and-do-while-loop.mdx @@ -7,7 +7,6 @@ In computer programming, loops are used to repeat a block of code. For example, In the previous tutorial, you learned about [Java for loop](./for-loop). Here, you are going to learn about `while` and `do...while` loops. - ## Java while loop Java while loop is used to run a specific code until a certain condition is met. The syntax of the while loop is: @@ -17,6 +16,7 @@ while (testExpression) { // body of loop } ``` + Here, 1. A `while` loop evaluates the **textExpression** inside the parenthesis `()`. @@ -30,7 +30,8 @@ To learn more about the conditions, visit [Java relational](./operators#3-java-r --- ## Flowchart of while loop -Flowchart of Java while loop + +Flowchart of Java while loop --- @@ -54,7 +55,6 @@ class Main { } ``` - #### Output ```plaintext @@ -67,14 +67,14 @@ class Main { Here is how this program works. -| Iteration | Variable | Condition: i `<=` n | Action | -|-----------|-------------------|-------------------|--------------------------------------------| -| 1st | `i = 1` `n = 5` | `true` | `1` is printed. `i` is increased to **2**. | -| 2nd | `i = 2` `n = 5 ` | `true` | `2` is printed. `i` is increased to **3**. | -| 3rd | `i = 3` ` n = 5 ` | `true` | `3` is printed.`i` is increased to **4**. | -| 4th | `i = 4` ` n = 5 ` | `true` | `4` is printed. `i` is increased to **5**. | -| 5th | `i = 5` ` n = 5` | `true` | `5` is printed. `i` is increased to **6**. | -| 6th | `i = 6` `n = 5` | `false` | The loop is terminated | +| Iteration | Variable | Condition: i `<=` n | Action | +| --------- | ---------------- | ------------------- | ------------------------------------------ | +| 1st | `i = 1` `n = 5` | `true` | `1` is printed. `i` is increased to **2**. | +| 2nd | `i = 2` `n = 5 ` | `true` | `2` is printed. `i` is increased to **3**. | +| 3rd | `i = 3` `n = 5` | `true` | `3` is printed.`i` is increased to **4**. | +| 4th | `i = 4` `n = 5` | `true` | `4` is printed. `i` is increased to **5**. | +| 5th | `i = 5` ` n = 5` | `true` | `5` is printed. `i` is increased to **6**. | +| 6th | `i = 6` `n = 5` | `false` | The loop is terminated | ### Example 2: Sum of Positive Numbers Only @@ -133,11 +133,13 @@ When the user enters a negative number, the loop terminates. Finally, the total ## Java do...while loop The `do...while` loop is similar to while loop. However, the body of `do...while` loop is executed once before the test expression is checked. For example, + ```java do { // body of loop } while(textExpression); ``` + Here, 1. The body of the loop is executed at first. Then the **textExpression** is evaluated. @@ -147,6 +149,7 @@ Here, 5. This process continues until the **textExpression** evaluates to false. Then the loop stops. ## Flowchart of do...while loop + {/* ![Flowchart of Java do...while loop](https://picsum.photos/600x400?text=Flowchart+of+Java+do...while+loop) */} Let's see the working of `do...while` loop. @@ -175,6 +178,7 @@ class Main { } } ``` + #### Output ```plaintext @@ -187,14 +191,14 @@ class Main { Here is how this program works. -| Iteration | Variable | Condition: i `<=` n | Action | -|-----------|-------------------|-------------------|--------------------------------------------| -| | `i = 1` `n = 5` | not checked | `1` is printed. `i` is increased to **2**. | -| 1st | `i = 2` `n = 5 ` | `true` | `2` is printed. `i` is increased to **3**. | -| 2nd | `i = 3` ` n = 5 ` | `true` | `3` is printed.`i` is increased to **4**. | -| 3rd | `i = 4` ` n = 5 ` | `true` | `4` is printed. `i` is increased to **5**. | -| 4th | `i = 5` ` n = 5` | `true` | `5` is printed. `i` is increased to **6**. | -| 5th | `i = 6` `n = 5` | `false` | The loop is terminated | +| Iteration | Variable | Condition: i `<=` n | Action | +| --------- | ---------------- | ------------------- | ------------------------------------------ | +| | `i = 1` `n = 5` | not checked | `1` is printed. `i` is increased to **2**. | +| 1st | `i = 2` `n = 5 ` | `true` | `2` is printed. `i` is increased to **3**. | +| 2nd | `i = 3` `n = 5` | `true` | `3` is printed.`i` is increased to **4**. | +| 3rd | `i = 4` `n = 5` | `true` | `4` is printed. `i` is increased to **5**. | +| 4th | `i = 5` ` n = 5` | `true` | `5` is printed. `i` is increased to **6**. | +| 5th | `i = 6` `n = 5` | `false` | The loop is terminated | --- @@ -227,6 +231,7 @@ class Main { } } ``` + #### Output 1 ```plaintext @@ -240,6 +245,7 @@ Enter a number -3 Sum = 39 ``` + Here, the user enters a positive number, that number is added to the `sum` variable. And this process continues until the number is negative. When the number is negative, the loop terminates and displays the sum without adding the negative number. #### Output 2 @@ -249,6 +255,7 @@ Enter a number -8 Sum is 0 ``` + Here, the user enters a negative number. The test condition will be `false` but the code inside of the loop executes once. ## Infinite while loop @@ -261,6 +268,7 @@ while(true){ // body of loop } ``` + ## Infinite do...while loop Here is an example of an infinite `do...while` loop. @@ -272,6 +280,7 @@ do { // body of loop } while(count == 1) ``` + In the above programs, the **textExpression** is always `true`. Hence, the loop body will run for infinite times. --- diff --git a/content/programs/add-two-integers.mdx b/content/programs/add-two-integers.mdx index 588d1668..d6ae2028 100644 --- a/content/programs/add-two-integers.mdx +++ b/content/programs/add-two-integers.mdx @@ -44,6 +44,7 @@ Enter two numbers 10 20 The sum is: 30 ``` + In this program, two integers `10` and `20` are stored in integer variables `first` and `second` respectively. Then, `first` and `second` are added using the `+` operator, and its result is stored in another variable `sum`. diff --git a/content/programs/calculate-compound-interest.mdx b/content/programs/calculate-compound-interest.mdx index dba3fb82..752c5f84 100644 --- a/content/programs/calculate-compound-interest.mdx +++ b/content/programs/calculate-compound-interest.mdx @@ -15,7 +15,6 @@ To understand this example, you should have the knowledge of the following Java A java program to calculate Compound Interest - ## Java Code ```java diff --git a/content/programs/calculate-power-of-a-number.mdx b/content/programs/calculate-power-of-a-number.mdx index b7e20e24..6f76e7b4 100644 --- a/content/programs/calculate-power-of-a-number.mdx +++ b/content/programs/calculate-power-of-a-number.mdx @@ -9,7 +9,9 @@ To understand this example, you should have the knowledge of the following Java - [Java Operators](/docs/operators) - [Java Basic Input and Output](/docs/basic-input-output) - [Control Flow statement - For-Loop in Java](/docs/for-loop) + ## Calculating power of a number + A java program to calculate the power of a number is as follows: ```java diff --git a/content/programs/calculate-simple-interest.mdx b/content/programs/calculate-simple-interest.mdx index 20797124..ae7298c6 100644 --- a/content/programs/calculate-simple-interest.mdx +++ b/content/programs/calculate-simple-interest.mdx @@ -52,10 +52,9 @@ Total amount person receive after 5 year: 2100. To Calculate Simple interest , `SimpleInterest = (P*R*T)/100` - here, P = principleAmount - R = Rate of interest (%) - T = Time Period (per annum), +R = Rate of interest (%) +T = Time Period (per annum), We need to take user input for following (P,R,T) and then apply the formulae using `*,/` operator. And then, print the output to user's terminal using `println()` funciton. diff --git a/content/programs/check-even-or-odd.mdx b/content/programs/check-even-or-odd.mdx index d9b659dd..79cafb67 100644 --- a/content/programs/check-even-or-odd.mdx +++ b/content/programs/check-even-or-odd.mdx @@ -27,6 +27,7 @@ class CheckEvenOdd } } ``` + #### Output 1: ```plaintext diff --git a/content/programs/factorial-in-java.mdx b/content/programs/factorial-in-java.mdx index 1aa113a5..2d8d3a9a 100644 --- a/content/programs/factorial-in-java.mdx +++ b/content/programs/factorial-in-java.mdx @@ -38,8 +38,6 @@ The factorial of 5 is : 120. To Calculate Factorial , `Factorial(5) = (5*4*3*2*1)` - - Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input) diff --git a/content/programs/find-quotient-and-reminder.mdx b/content/programs/find-quotient-and-reminder.mdx index 1a06c230..f4ec2c36 100644 --- a/content/programs/find-quotient-and-reminder.mdx +++ b/content/programs/find-quotient-and-reminder.mdx @@ -4,7 +4,6 @@ shortTitle: Calculate Quotient and reminder description: In this program you'll learn, How you can calculate the quotient and reminder of a number (divident) by dividing a number (divisor) --- - To understand this example, you should have the knowledge of the following Java programming topics: - [Java Operators](/docs/operators) @@ -32,6 +31,7 @@ public class quotient_reminder { } } ``` + ### Output 1: ```plaintext @@ -42,6 +42,7 @@ Reminder: 11 ``` ### Output 2: + ```plaintext Enter the dividend: 100 Enter the divisor: 8 @@ -57,7 +58,6 @@ Thus, here we perform the divison operation using `/` operator and storing the v And what about reminder ? many time the divison isn't the absolute. So inorder to calculate the reminder we are calculating it using modulas operator and storing it in `reminder` variable. After getting Quotient and Reminder we just printing these. - Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input) diff --git a/content/programs/index.mdx b/content/programs/index.mdx index 90f7d2f3..d4798e05 100644 --- a/content/programs/index.mdx +++ b/content/programs/index.mdx @@ -9,31 +9,60 @@ Welcome to the Javaistic Programs section! Here, you can explore a variety of ba ## Learning Resources - } href="/programs/print-an-integer"> - Start with fundamental Java programs that cover basic input/output operations, arithmetic calculations, and simple algorithms. + } + href="/programs/print-an-integer" + > + Start with fundamental Java programs that cover basic input/output + operations, arithmetic calculations, and simple algorithms. - } href="/programs/add-two-integers"> - Dive into more complex Java programs that involve data structures, algorithms, and problem-solving techniques. + } + href="/programs/add-two-integers" + > + Dive into more complex Java programs that involve data structures, + algorithms, and problem-solving techniques. - } href="/programs/java-program-to-add-two-binary-numbers"> - Challenge yourself with advanced Java programs that require deeper understanding of algorithms, data manipulation, and optimization. + } + href="/programs/java-program-to-add-two-binary-numbers" + > + Challenge yourself with advanced Java programs that require deeper + understanding of algorithms, data manipulation, and optimization. - } href="/programs/java-program-to-check-Leap-year"> - Test your skills with practice problems that cover a wide range of topics, from basic syntax to advanced concepts. + } + href="/programs/java-program-to-check-Leap-year" + > + Test your skills with practice problems that cover a wide range of topics, + from basic syntax to advanced concepts. ## Get Involved - } href="https://github.com/javaistic/javaistic/discussions" > - Join the community on GitHub Discussions to ask questions, share ideas, and get help. + } + href="https://github.com/javaistic/javaistic/discussions" + > + Join the community on GitHub Discussions to ask questions, share ideas, and + get help. - } > - Join the Javaistic Discord server to chat with other Java enthusiasts in real-time. + }> + Join the Javaistic Discord server to chat with other Java enthusiasts in + real-time. - } > + } + > Follow us on Twitter for the latest updates, news, and announcements. - diff --git a/content/programs/java-program-to-add-two-binary-numbers.mdx b/content/programs/java-program-to-add-two-binary-numbers.mdx index 158c8700..fe0ab47b 100644 --- a/content/programs/java-program-to-add-two-binary-numbers.mdx +++ b/content/programs/java-program-to-add-two-binary-numbers.mdx @@ -54,6 +54,7 @@ public class JavaExample { } } ``` + ### Output: ```plaintext diff --git a/content/programs/java-program-to-add-two-complex-numbers.mdx b/content/programs/java-program-to-add-two-complex-numbers.mdx index ee6651c7..9f672d1a 100644 --- a/content/programs/java-program-to-add-two-complex-numbers.mdx +++ b/content/programs/java-program-to-add-two-complex-numbers.mdx @@ -47,6 +47,7 @@ public class ComplexNumber{ } } ``` + ### Output: ```plaintext diff --git a/content/programs/java-program-to-check-Leap-year.mdx b/content/programs/java-program-to-check-Leap-year.mdx index 6d5851f8..3f88da9a 100644 --- a/content/programs/java-program-to-check-Leap-year.mdx +++ b/content/programs/java-program-to-check-Leap-year.mdx @@ -4,9 +4,8 @@ ShortTitle: Check Leap year description: In this program, you'll learn to check wheather the given Year (integer) is a leap year or not. --- - - To understand this example, you should have the knowledge of the following Java programming topics: + - [Java Operator](/docs/operator) - [Java Basic Input and Output](/docs/basic-input-output) @@ -45,13 +44,16 @@ class checkLeapYear{ Enter the year to check: 2003 2003 isn't leap year ``` + #### Output 2 + ```plaintext Enter the year to check: 2004 2004 is leap year ``` To check weather a `year` is `leap` or not we only need to check weather it's `divisible by 4` or not if it doesn't leave reminder then leap year else not a leap year + Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input) diff --git a/content/programs/java-program-to-check-divisbility.mdx b/content/programs/java-program-to-check-divisbility.mdx index b302c938..36b345a1 100644 --- a/content/programs/java-program-to-check-divisbility.mdx +++ b/content/programs/java-program-to-check-divisbility.mdx @@ -4,19 +4,15 @@ ShortTitle: Divisibility Checker description: This Java program checks the divisibility of a number by 2, 3, 5, 7, or 11. --- - - - To understand this example, you should have the knowledge of the following Java programming topics: + - [Java Operator](/docs/operator) - [Java Basic Input and Output](/docs/basic-input-output) - ## Check Divisbilty A java program that checks wheather the User Given Number is Divisble by 2,3,5,7,11 is as follows - ```java import java.util.Scanner; @@ -82,6 +78,7 @@ public class DivisibilityChecker { } ``` + #### Output 1 ```plaintext @@ -96,7 +93,9 @@ Choose divisibility checks: Enter option (1-5): 1 14 is divisible by 2. ``` + #### Output 2 + ```plaintext Checking the divisbilty by 3 Enter a number: 27 @@ -110,7 +109,9 @@ Enter option (1-5): 2 27 is divisible by 3. ``` + #### Output 3 + ```plaintext Checking the divisbilty by 5 Enter a number: 45 @@ -125,7 +126,9 @@ Enter option (1-5): 3 ``` + #### Output 4 + ```plaintext Checking the divisbilty by 11 Enter a number: 33 @@ -141,7 +144,9 @@ Enter option (1-5): 5 ``` + #### Output 5 + ```plaintext Invalid Option Enter a number: 18 @@ -158,10 +163,9 @@ Invalid option. Please choose a number between 1 and 5. ``` + Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input) - - diff --git a/content/programs/java-program-to-find-nth-fibonacci-number.mdx b/content/programs/java-program-to-find-nth-fibonacci-number.mdx index 64fa4e3f..9ad7e5e8 100644 --- a/content/programs/java-program-to-find-nth-fibonacci-number.mdx +++ b/content/programs/java-program-to-find-nth-fibonacci-number.mdx @@ -16,7 +16,6 @@ To understand this example, you should have the knowledge of the following Java A java program to find the Nth Fibonacci number. - ## Java Code ```java diff --git a/content/programs/multiply-two-numbers.mdx b/content/programs/multiply-two-numbers.mdx index c12e49f0..a93f69f2 100644 --- a/content/programs/multiply-two-numbers.mdx +++ b/content/programs/multiply-two-numbers.mdx @@ -4,8 +4,6 @@ shortTitle: Multiply Two Numbers description: In this program, you'll learn to store and multiply two integer numbers in Java. After multiplication, the final value is displayed on the screen. --- - - To understand this example, you should have the knowledge of the following Java programming topics: - [Java Operators](/docs/operators) @@ -65,7 +63,7 @@ Here two input numbers are taken from user one after another with space in betwe ### Example 2: Program to Multiply Two Decimal Numbers -*Multiplication can be performed between positive, negative numbers as well.* +_Multiplication can be performed between positive, negative numbers as well._ ```java import java.util.Scanner; diff --git a/content/programs/print-an-integer.mdx b/content/programs/print-an-integer.mdx index bef4b579..ac05fdaa 100644 --- a/content/programs/print-an-integer.mdx +++ b/content/programs/print-an-integer.mdx @@ -4,7 +4,6 @@ shortTitle: Print an Integer description: In this program, you'll learn to print a predefined number aw well as a nummber entered by the user in Java. --- - The integer is stored in a variable using `System.in`, and is displayed on the screen using `System.out`. To understand this example, you should have the knowledge of the following Java programming topics: @@ -12,7 +11,6 @@ To understand this example, you should have the knowledge of the following Java - [Java Hello World Program](/docs/hello-world) - [Java Basic Input and Output](/docs/basic-input-output) - ## 1. Print an Integer A Java program that prints a number predefined by the user is as follows: @@ -41,6 +39,7 @@ public class HelloWorld { ```plaintext The number is: 10 ``` + In this program we are printing a constant integer value which is definrd from first. The integer is stored in an integer variable `number` using the keyword `int` and printed using the keyword `println`. @@ -81,7 +80,7 @@ Enter a number: 10 You entered: 10 ``` -In this program, an object of `Scanner` class, `reader` is created to take inputs from standard input, which is `keyboard`. +In this program, an object of `Scanner` class, `reader` is created to take inputs from standard input, which is `keyboard`. Then, `Enter a number` prompt is printed to give the user a visual cue as to what they should do next. diff --git a/package.json b/package.json index 6c8cd85d..084401f0 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "build": "next build", "dev": "next dev --turbo", "start": "next start", + "format": "prettier {src,content}/**/*.{ts,tsx,mdx,css} --write", "postinstall": "fumadocs-mdx" }, "dependencies": { @@ -21,6 +22,8 @@ "motion": "^12.23.12", "next": "15.4.2", "ogl": "^1.0.11", + "prettier": "^3.6.2", + "prettier-plugin-tailwindcss": "^0.6.14", "react": "^19.1.0", "react-dom": "^19.1.0", "sharp": "^0.34.3", diff --git a/src/app/(site)/page.tsx b/src/app/(site)/page.tsx index 0dfbcc1b..ce9acc61 100644 --- a/src/app/(site)/page.tsx +++ b/src/app/(site)/page.tsx @@ -1,56 +1,56 @@ -'use client' +"use client"; import { GitHubIcon } from "@/components/icons"; import { Button } from "@/components/ui/button"; +import { easeOut, motion } from "framer-motion"; import { ArrowRightIcon, CodeIcon, ExternalLinkIcon } from "lucide-react"; import Image from "next/image"; import Link from "next/link"; -import Particles from '../../components/Particles'; -import { useEffect, useState } from 'react'; -import { motion, easeOut } from 'framer-motion'; +import { useEffect, useState } from "react"; +import Particles from "@/components/Particles"; export default function HomePage() { const [isDarkMode, setIsDarkMode] = useState(false); useEffect(() => { - const checkDarkMode = () => { - if (typeof window !== 'undefined') { + if (typeof window !== "undefined") { + const systemPrefersDark = window.matchMedia( + "(prefers-color-scheme: dark)", + ).matches; + + const hasDarkClass = + document.documentElement.classList.contains("dark"); - const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches; - + const dataTheme = document.documentElement.getAttribute("data-theme"); - const hasDarkClass = document.documentElement.classList.contains('dark'); - - const dataTheme = document.documentElement.getAttribute('data-theme'); - - setIsDarkMode(systemPrefersDark || hasDarkClass || dataTheme === 'dark'); + setIsDarkMode( + systemPrefersDark || hasDarkClass || dataTheme === "dark", + ); } }; checkDarkMode(); + const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)"); + mediaQuery.addEventListener("change", checkDarkMode); - const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); - mediaQuery.addEventListener('change', checkDarkMode); - - const observer = new MutationObserver(checkDarkMode); observer.observe(document.documentElement, { attributes: true, - attributeFilter: ['class', 'data-theme'] + attributeFilter: ["class", "data-theme"], }); return () => { - mediaQuery.removeEventListener('change', checkDarkMode); + mediaQuery.removeEventListener("change", checkDarkMode); observer.disconnect(); }; }, []); // Define particle properties based on theme - const particleConfig = isDarkMode + const particleConfig = isDarkMode ? { // Dark mode properties - enhanced for better visibility - particleColors: ['#00FFFF',' #FF00FF','#00BFFF'], + particleColors: ["#00FFFF", " #FF00FF", "#00BFFF"], particleCount: 500, particleSpread: 10, speed: 0.4, @@ -60,11 +60,11 @@ export default function HomePage() { disableRotation: true, particleHoverFactor: 2, sizeRandomness: 1.0, - cameraDistance: 18 + cameraDistance: 18, } : { // Light mode properties - particleColors: ['#1E3A8A', '#4B0082', '#4a4a4a'], + particleColors: ["#1E3A8A", "#4B0082", "#4a4a4a"], particleCount: 500, particleSpread: 10, speed: 0.4, @@ -74,7 +74,7 @@ export default function HomePage() { disableRotation: true, particleHoverFactor: 1.5, sizeRandomness: 1.0, - cameraDistance: 15 + cameraDistance: 15, }; // Animation variants @@ -84,9 +84,9 @@ export default function HomePage() { opacity: 1, transition: { duration: 0.4, - staggerChildren: 0.1 - } - } + staggerChildren: 0.1, + }, + }, }; const itemVariants = { @@ -96,9 +96,9 @@ export default function HomePage() { y: 0, transition: { duration: 0.4, - ease: easeOut - } - } + ease: easeOut, + }, + }, }; const titleVariants = { @@ -108,9 +108,9 @@ export default function HomePage() { y: 0, transition: { duration: 0.5, - ease: easeOut - } - } + ease: easeOut, + }, + }, }; const buttonVariants = { @@ -120,15 +120,15 @@ export default function HomePage() { scale: 1, transition: { duration: 0.3, - ease: easeOut - } + ease: easeOut, + }, }, hover: { scale: 1.05, transition: { - duration: 0.2 - } - } + duration: 0.2, + }, + }, }; const imageVariants = { @@ -138,9 +138,9 @@ export default function HomePage() { x: 0, transition: { duration: 0.5, - ease: easeOut - } - } + ease: easeOut, + }, + }, }; const textVariants = { @@ -150,15 +150,24 @@ export default function HomePage() { x: 0, transition: { duration: 0.5, - ease: easeOut - } - } + ease: easeOut, + }, + }, }; return (

{/* Particles Background */} -
+
- -
+ +
{/* Hero Section */} - - Master{" "} @@ -195,33 +204,36 @@ export default function HomePage() { Javaistic{" "} - Interactive, fast-paced learning for absolute beginners to advanced learners. A free and open-source platform to learn Java programming. - - + diff --git a/src/app/docs/layout.tsx b/src/app/docs/layout.tsx index ce2e9463..7fe91ff7 100644 --- a/src/app/docs/layout.tsx +++ b/src/app/docs/layout.tsx @@ -1,14 +1,11 @@ -import { DocsLayout, DocsLayoutProps } from "fumadocs-ui/layouts/notebook"; -import type { ReactNode } from "react"; import { baseOptions } from "@/app/layout.config"; import { source } from "@/lib/source"; +import { DocsLayout, DocsLayoutProps } from "fumadocs-ui/layouts/notebook"; +import type { ReactNode } from "react"; const docsLayoutOptions: DocsLayoutProps = { tree: source.pageTree, ...baseOptions, - themeSwitch: { - mode: "light-dark", - }, }; export default function Layout({ children }: { children: ReactNode }) { diff --git a/src/components/Particles.tsx b/src/components/Particles.tsx index 134aaa69..c59abf18 100644 --- a/src/components/Particles.tsx +++ b/src/components/Particles.tsx @@ -21,7 +21,10 @@ const defaultColors: string[] = ["#ffffff", "#ffffff", "#ffffff"]; const hexToRgb = (hex: string): [number, number, number] => { hex = hex.replace(/^#/, ""); if (hex.length === 3) { - hex = hex.split("").map((c) => c + c).join(""); + hex = hex + .split("") + .map((c) => c + c) + .join(""); } const int = parseInt(hex, 16); const r = ((int >> 16) & 255) / 255; @@ -142,7 +145,10 @@ const Particles: React.FC = ({ const positions = new Float32Array(count * 3); const randoms = new Float32Array(count * 4); const colors = new Float32Array(count * 3); - const palette = particleColors && particleColors.length > 0 ? particleColors : defaultColors; + const palette = + particleColors && particleColors.length > 0 + ? particleColors + : defaultColors; for (let i = 0; i < count; i++) { let x: number, y: number, z: number, len: number; @@ -154,7 +160,10 @@ const Particles: React.FC = ({ } while (len > 1 || len === 0); const r = Math.cbrt(Math.random()); positions.set([x * r, y * r, z * r], i * 3); - randoms.set([Math.random(), Math.random(), Math.random(), Math.random()], i * 4); + randoms.set( + [Math.random(), Math.random(), Math.random(), Math.random()], + i * 4, + ); const col = hexToRgb(palette[Math.floor(Math.random() * palette.length)]); colors.set(col, i * 3); } @@ -237,10 +246,7 @@ const Particles: React.FC = ({ ]); return ( -
+
); }; diff --git a/src/components/footer.tsx b/src/components/footer.tsx index 26d207f4..aaf66bb8 100644 --- a/src/components/footer.tsx +++ b/src/components/footer.tsx @@ -35,11 +35,11 @@ const footerNav = { export function Footer() { return ( -