Skip to content

Commit 616ac89

Browse files
committed
feat(docs): Add tutorials for Java static keyword, switch statement, this keyword, variables and literals, and primitive data types; remove obsolete test file; implement while and do...while loop examples
1 parent 5035aee commit 616ac89

28 files changed

+4485
-17
lines changed

content/docs/arrays.mdx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: Java Arrays
3+
description: In this tutorial, we will learn how to use the Java continue statement to skip the current iteration of a loop.
4+
---
5+
6+
## Java Arrays
7+
An array is a collection of similar types of data.
8+
9+
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.
10+
11+
```java
12+
String[] array = new String[100];
13+
```
14+
Here, the above array cannot store more than 100 names. The number of values in a Java array is always fixed.
15+
16+
### How to declare an array in Java?
17+
In Java, here is how we can declare an array.
18+
19+
```java
20+
dataType[] arrayName;
21+
```
22+
- `dataType` - it can be primitive data types like `int`, `char`, `double`, `byte`, etc. or Java objects
23+
- `arrayName` - it is an identifier
24+
For example,
25+
26+
```java
27+
double[] data;
28+
```
29+
Here, data is an array that can hold values of type double.
30+
31+
But, how many elements can array this hold?
32+
33+
Good question! To define the number of elements that an array can hold, we have to allocate memory for the array in Java. For example,
34+
35+
```java
36+
// declare an array
37+
double[] data;
38+
39+
// allocate memory
40+
data = new double[10];
41+
```
42+
Here, the array can store 10 elements. We can also say that the size or length of the array is 10.
43+
44+
In Java, we can declare and allocate the memory of an array in one single statement. For example,
45+
46+
```java
47+
double[] data = new double[10];
48+
```
49+
### How to Initialize Arrays in Java?
50+
In Java, we can initialize arrays during declaration. For example,
51+
52+
```java
53+
//declare and initialize and array
54+
int[] age = {12, 4, 5, 2, 5};
55+
```
56+
Here, we have created an array named age and initialized it with the values inside the curly brackets.
57+
58+
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).
59+
60+
In the Java array, each memory location is associated with a number. The number is known as an array index. We can also initialize arrays in Java, using the index number. For example,
61+
62+
```java
63+
// declare an array
64+
int[] age = new int[5];
65+
66+
// initialize array
67+
age[0] = 12;
68+
age[1] = 4;
69+
age[2] = 5;
70+
..
71+
```
72+
Elements are stored in the array
73+
Java Arrays initialization
74+
75+
Note:
76+
77+
Array indices always start from 0. That is, the first element of an array is at index 0.
78+
If the size of an array is n, then the last element of the array will be at index n-1.

content/docs/basic-input-output.mdx

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
---
2+
title: Java Basic Input and Output
3+
description: In this tutorial, you will learn simple ways to display output to users and take input from users in Java.
4+
5+
---
6+
7+
8+
## Java Output
9+
In Java, you can simply use
10+
11+
```java
12+
System.out.println(); or
13+
14+
System.out.print(); or
15+
16+
System.out.printf();
17+
```
18+
19+
to send output to standard output (screen).
20+
21+
Here,
22+
23+
- `System` is a class
24+
- `out` is a `public` `static` field: it accepts output data.
25+
26+
Don't worry if you don't understand it. We will discuss `class`, `public`, and `static` in later chapters.
27+
28+
Let's take an example to output a line.
29+
30+
```java
31+
class AssignmentOperator {
32+
public static void main(String[] args) {
33+
34+
System.out.println("Java programming is interesting.");
35+
}
36+
}
37+
```
38+
**Output:**
39+
40+
```plaintext
41+
Java programming is interesting.
42+
```
43+
Here, we have used the `println()` method to display the string.
44+
45+
## Difference between println(), print() and printf()
46+
- `print()` - It prints string inside the quotes.
47+
- `println()` - It prints string inside the quotes similar like `print()` method. Then the cursor moves to the beginning of the next line.
48+
- `printf()` - It provides string formatting.
49+
50+
### Example: print() and println()
51+
```java
52+
class Output {
53+
public static void main(String[] args) {
54+
55+
System.out.println("1. println ");
56+
System.out.println("2. println ");
57+
58+
System.out.print("1. print ");
59+
System.out.print("2. print");
60+
}
61+
}
62+
```
63+
**Output:**
64+
```plaintext
65+
1. println
66+
2. println
67+
1. print 2. print
68+
```
69+
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).
70+
71+
### Example: Printing Variables and Literals
72+
73+
```java
74+
class Variables {
75+
public static void main(String[] args) {
76+
77+
Double number = -10.6;
78+
79+
System.out.println(5);
80+
System.out.println(number);
81+
}
82+
}
83+
```
84+
When you run the program, the output will be:
85+
86+
```plaintext
87+
5
88+
-10.6
89+
```
90+
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.
91+
92+
### Example: Print Concatenated Strings
93+
```java
94+
class PrintVariables {
95+
public static void main(String[] args) {
96+
97+
Double number = -10.6;
98+
99+
System.out.println("I am " + "awesome.");
100+
System.out.println("Number = " + number);
101+
}
102+
}
103+
```
104+
**Output:**
105+
106+
```java
107+
I am awesome.
108+
Number = -10.6
109+
```
110+
In the above example, notice the line,
111+
112+
```java
113+
System.out.println("I am " + "awesome.");
114+
```
115+
116+
Here, we have used the + operator to concatenate (join) the two strings: `"I am "` and `"awesome."`.
117+
118+
And also, the line,
119+
120+
```java
121+
System.out.println("Number = " + number);
122+
```
123+
Here, first the value of variable `number` is evaluated. Then, the value is concatenated to the string: `"Number = "`.
124+
125+
## Java Input
126+
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.
127+
128+
In order to use the object of `Scanner`, we need to import `java.util.Scanner` package.
129+
130+
131+
```java
132+
import java.util.Scanner;
133+
```
134+
To learn more about importing packages in Java, visit [Java Import Packages]().
135+
136+
Then, we need to create an object of the `Scanner` class. We can use the object to take input from the user.
137+
138+
139+
```java
140+
// create an object of Scanner
141+
Scanner input = new Scanner(System.in);
142+
143+
// take input from the user
144+
int number = input.nextInt();
145+
```
146+
147+
### Example: Get Integer Input From the User
148+
```java
149+
import java.util.Scanner;
150+
151+
class Input {
152+
public static void main(String[] args) {
153+
154+
Scanner input = new Scanner(System.in);
155+
156+
System.out.print("Enter an integer: ");
157+
int number = input.nextInt();
158+
System.out.println("You entered " + number);
159+
160+
// closing the scanner object
161+
input.close();
162+
}
163+
}
164+
```
165+
**Output:**
166+
167+
```plaintext
168+
Enter an integer: 23
169+
You entered 23
170+
```
171+
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.
172+
173+
Similarly, we can use `nextLong()`, `nextFloat()`, `nextDouble()`, and `next()` methods to get `long`, `float`, `double`, and `string` input respectively from the user.
174+
175+
<Callout>
176+
**Note:** We have used the `close()` method to close the object. It is recommended to close the scanner object once the input is taken.
177+
</Callout>
178+
179+
### Example: Get float, double and String Input
180+
```java
181+
import java.util.Scanner;
182+
183+
class Input {
184+
public static void main(String[] args) {
185+
186+
Scanner input = new Scanner(System.in);
187+
188+
// Getting float input
189+
System.out.print("Enter float: ");
190+
float myFloat = input.nextFloat();
191+
System.out.println("Float entered = " + myFloat);
192+
193+
// Getting double input
194+
System.out.print("Enter double: ");
195+
double myDouble = input.nextDouble();
196+
System.out.println("Double entered = " + myDouble);
197+
198+
// Getting String input
199+
System.out.print("Enter text: ");
200+
String myString = input.next();
201+
System.out.println("Text entered = " + myString);
202+
}
203+
}
204+
```
205+
206+
**Output:**
207+
208+
```plaintext
209+
Enter float: 2.343
210+
Float entered = 2.343
211+
Enter double: -23.4
212+
Double entered = -23.4
213+
Enter text: Hey!
214+
Text entered = Hey!
215+
```
216+
As mentioned, there are other several ways to get input from the user. To learn more about `Scanner`, visit Java Scanner.

content/docs/break-statement.mdx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: Java break Statement
3+
description: In this tutorial, we will learn how to use the Java break statement to terminate a loop or switch statement.
4+
---
5+
6+
While working with loops, it is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression.
7+
8+
In such cases, `break` and `continue` statements are used. You will learn about the Java continue statement in the next tutorial.
9+
10+
The break statement in Java terminates the loop immediately, and the control of the program moves to the next statement following the loop.
11+
12+
It is almost always used with decision-making statements (Java if...else Statement).
13+
14+
Here is the syntax of the break statement in Java:
15+
16+
```java
17+
break;
18+
```
19+
20+
## Example 1: Java break statement
21+
22+
Java while loop is used to run a specific code until a certain condition is met. The syntax of the while loop is:
23+
24+
```java
25+
class Test {
26+
public static void main(String[] args) {
27+
28+
// for loop
29+
for (int i = 1; i <= 10; ++i) {
30+
31+
// if the value of i is 5 the loop terminates
32+
if (i == 5) {
33+
break;
34+
}
35+
System.out.println(i);
36+
}
37+
}
38+
}
39+
```
40+
#### Output
41+
42+
```plaintext
43+
1
44+
2
45+
3
46+
4
47+
```
48+
49+
In the above program, we are using the `for` loop to print the value of i in each iteration. To know how for loop works, visit the Java `for` loop. Here, notice the statement,
50+
51+
```java
52+
if (i == 5) {
53+
break;
54+
}
55+
```
56+
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.
57+
58+
59+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

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

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


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy