Added a more detailed example and corrected some content. (#22716)
The original page showed java.io as a built in package but it really isn't in the sense that you have to import the package in order to use it where as java.lang does not need to be imported. I removed mentions of java.io and cleaned up the description. I also added a simple example and explanation of a couple of java.lang packages.
This commit is contained in:
committed by
Christopher McCormack
parent
3d59624fba
commit
8b4c2f3d03
@ -3,11 +3,26 @@ title: Built-In Functions
|
|||||||
---
|
---
|
||||||
# Built-In Functions
|
# Built-In Functions
|
||||||
|
|
||||||
Java also has many built-in or pre-defined functions which are usually stored in the java.lang and java.io packages,
|
Java also has many built-in or pre-defined methods which are stored in the java.lang package.
|
||||||
which are automatically imported in editors like BlueJ or can be imported using the following Syntax-
|
These are automatically imported by Java or can be imported manually using the following Syntax:
|
||||||
```java
|
```java
|
||||||
import java.lang.*;
|
import java.lang.*;
|
||||||
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 useful examples of these are the java.lang.System and java.lang.Math packages. Most people know System because it is used to print to the console. Math has slew of useful methods as well.
|
||||||
|
|
||||||
|
```java
|
||||||
|
package javaexample;
|
||||||
|
|
||||||
|
public class JavaExample {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
double myDouble = 22.4;
|
||||||
|
System.out.println(Math.round(myDouble));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The code above initializes a variable of type double and sets it equal to 22.4. On the next line the java.lang.System package is called simply by typing System. System.out.println is used to print to the console with a new line at the end of the text. In the parenticies you will see the java.lang.Math package called. For this example the round method was chosen which takes either a double or a float and returns the nearest whole number. The myDouble variable was passed in and the whole line prints 22 to the console.
|
||||||
|
Reference in New Issue
Block a user