Java Basic Examples

Calculate logarithm value using log method of Java Math class

This example shows how to calculate logarithm values using log() method of Java Math class.

How to calculate natural logarithm value in Java?

Use log() static method of Java Math class to calculate logarithm value in Java.

This method returns the natural logarithm value (base e) of argument double number.

Output

How to calculate log value base 10 in Java?

log() method of Java Math class uses base e. If you want to calculate log base 10 use following code.

a) Java version 1.4 and below

Output

b) Java version 1.5 and above

Java version 1.5 introduced a new method log10() which calculates log value using base 10.

Output

How to calculate log value base 2 in Java?

Use following code to calculate log value base 2 in Java.

Output

Please let us know your views in the comments section below.

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

Tags