Skip to content

Commit a00a824

Browse files
committed
Add Java Basic Input Output docs
1 parent 74180ee commit a00a824

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed

src/navs/documentation.js

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

0 commit comments

Comments
 (0)