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.
1.2 KiB
title
title |
---|
Built-In Functions |
Built-In Functions
Java also has many built-in or pre-defined methods which are stored in the java.lang package. These are automatically imported by Java or can be imported manually using the following Syntax:
import java.lang.*;
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.
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.