Learning Programming #3.0 Using Java: The standard library
Now that you know all the specialties of java it is time to do some actual programming.
Java provides a pretty advanced standard library compared to other languages. That allows you to easily access useful algorithms and classes without needing to install them yourself or downloading any other library. This post is about some classes that are useful in any kind of application.
java.lang.Math
Math is automatically imported → so you don't need to import it when you use it.
Math contains all sorts of mathematical functions. Some of them are listed here:
mathematical expression | java function |
---|---|
sin(x) | Math.sin(x) |
cos(x) | Math.cos(x) |
tan(x) | Math.tan(x) |
sin⁻¹(x) | Math.atan(x) |
cos⁻¹(x) | Math.acos(x) |
tan⁻¹(x) | Math.atan(x) |
tan⁻¹(Δy/Δx) | Math.atan2(y, x) |
e^x | Math.exp(x) |
a^b | Math.pow(a, b) |
√x | Math.sqrt(x) |
∛x | Math.cbrt(x) |
∣x∣ | Math.abs(x) |
java.lang.System
System is a class that handles a lot of general stuff. It is like Math also automatically imported.
Here are some useful functions of system:
function | use |
---|---|
System.exit(1) | Kills your program |
System.currentTimeMillis() | Returns the time in milliseconds(10⁻³ s). Can be used to calculate the current date. |
System.nanoTime() | Returns the time in nanoseconds(10⁻⁹ s) with unknown accuracy(usually inaccurate to a few µs(10⁻⁶ s). nanoTime can only be used to determine relative time between two measurements. |
System.out.print(string); | Prints the string into the terminal. |
System.out.println(string); | Prints the string + "\n" into the terminal. |
java.lang.Thread
Apart from allowing multithreadding through Thread.run(), Thread
also contains another generally very useful function:
Thread.sleep(int milliseconds)
This will have the current Thread sleep(being inactive) for the given amount of milliseconds.
But be careful about that. Java tends to oversleep a little → the actual sleep time might be up to 1 ms longer than specified, but that doesn't matter in most applications.
Here an image from a 4d-projection(I'll explain that somewhere in a future post.) class I wrote some time ago. And it contains some Math
s:
Congratulations @quantumdeveloper! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP