Java Guide- Added Examples to Built in funcions (#18797)
Added examples of Java.IO package and Java.lang packages
This commit is contained in:
@ -11,3 +11,31 @@ import java.io.*;
|
|||||||
```
|
```
|
||||||
|
|
||||||
These comprise functions which make an otherwise long and hard task easier to do.
|
These comprise functions which make an otherwise long and hard task easier to do.
|
||||||
|
|
||||||
|
Some examples of ```java.lang.* ``` are:
|
||||||
|
|
||||||
|
Generating a random number using Math. In this example a random integer from 1 - 10
|
||||||
|
```java
|
||||||
|
int randomNumber = (int) (Math.random() * 10) + 1;
|
||||||
|
```
|
||||||
|
Usage of primitive types such as double, float, int boolean
|
||||||
|
|
||||||
|
```java
|
||||||
|
int integerNumber = 1;
|
||||||
|
double doubleNumber = 0.1d;
|
||||||
|
float floatNumber = 0.2f;
|
||||||
|
boolean trueOrFalse = false;
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Classes in ```java.io.* ``` are mainly used for reading and writing of files with diffrent methods such as Input / output streams and buffered readers/ writers.
|
||||||
|
And example using a buffered reader is below:
|
||||||
|
|
||||||
|
```java
|
||||||
|
File file = new File("C:\\Users\\Desktop\\test.txt");
|
||||||
|
BufferedReader br = new BufferedReader(new FileReader(file));
|
||||||
|
String st;
|
||||||
|
while ((st = br.readLine()) != null)
|
||||||
|
System.out.println(st);
|
||||||
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user