Java Topic

Java Math

The java.lang.Math class provides a set of static methods and constants for performing mathematical operations. It includes methods for basic arithmetic, trigonometric functions, logarithms, and more.

1. Basic Arithmetic

The Math class provides methods for basic arithmetic operations:

  • Addition:
     
    double sum = Math.addExact(a, b);
    Adds two integers with overflow checking.Lecturre learning image
  • Subtraction:
     
    double difference = Math.subtractExact(a, b);
    Subtracts one integer from another with overflow checking.
  • Multiplication:
     
    double product = Math.multiplyExact(a, b);
    Multiplies two integers with overflow checking.
  • Division:
     
    double quotient = Math.floorDiv(a, b);
    Returns the largest (closest to positive infinity) integer that is less than or equal to the algebraic quotient.

2. Trigonometric Functions

The Math class provides methods for trigonometric calculations:

  • Sine:
     
    double sine = Math.sin(angle);
    Returns the sine of the specified angle (in radians).
  • Cosine:
     
    double cosine = Math.cos(angle);
    Returns the cosine of the specified angle (in radians).
  • Tangent:
     
    double tangent = Math.tan(angle);
    Returns the tangent of the specified angle (in radians).
  • Arc Sine:
     
    double arcSine = Math.asin(value);
    Returns the arc sine of the specified value, in radians.
  • Arc Cosine:
     
    double arcCosine = Math.acos(value);
    Returns the arc cosine of the specified value, in radians.
  • Arc Tangent:
     
    double arcTangent = Math.atan(value);
    Returns the arc tangent of the specified value, in radians.

3. Exponential and Logarithmic Functions

The Math class includes methods for exponential and logarithmic calculations:

  • Exponentiation:
     
    double exp = Math.exp(value);
    Returns Euler's number raised to the specified power.
  • Natural Logarithm:
     
    double ln = Math.log(value);
    Returns the natural logarithm (base e) of the specified value.
  • Base-10 Logarithm:
     
    double log10 = Math.log10(value);
    Returns the base-10 logarithm of the specified value.
  • Square Root:
     
    double sqrt = Math.sqrt(value);
    Returns the square root of the specified value.

4. Rounding Functions

The Math class provides methods for rounding numbers:

  • Round:
     
    long rounded = Math.round(value);
    Rounds the specified value to the nearest long.
  • Ceiling:
     
    double ceil = Math.ceil(value);
    Returns the smallest double value that is greater than or equal to the specified value.
  • Floor:
     
    double floor = Math.floor(value);
    Returns the largest double value that is less than or equal to the specified value.

5. Constants

The Math class defines several constants:

  • Math.PI
    : The value of pi (π), approximately 3.14159.
  • Math.E
    : The base of natural logarithms (e), approximately 2.71828.

6. Random Numbers

The Math class provides a method for generating random numbers:

  • Random:
     
    double random = Math.random();
    Returns a random double value between 0.0 (inclusive) and 1.0 (exclusive).

 

Java Booleans

The boolean data type in Java represents a value that can be either true or false. It is a fundamental type used for logical operations, conditional statements, and controlling the flow of execution.

1. Declaring and Initializing Booleans

In Java, you can declare and initialize boolean variables as follows:

  • Declaration:
    boolean flag;
  • Initialization:
    flag = true;
    flag = false;
  • Declaration with Initialization:
    boolean isActive = true;

2. Boolean Operations

Java provides several operators to perform logical operations with boolean values:

  • Logical AND:
     
    boolean result = (a && b);
    Returns true if both a and b are true.
  • Logical OR:
     
    boolean result = (a || b);
    Returns true if at least one of a or b is true.
  • Logical NOT:
     
    boolean result = !a;
    Returns true if a is false, and vice versa.
  • XOR (Exclusive OR):
     
    boolean result = (a ^ b);
    Returns true if a and b are different.

3. Conditional Statements

Booleans are commonly used in conditional statements to control the flow of execution:

  • If Statement:
    if (condition) {
        // code to execute if condition is true
    }
  • If-Else Statement:
    if (condition) {
        // code to execute if condition is true
    } else {
        // code to execute if condition is false
    }
  • Switch Statement:
    switch (value) {
        case true:
            // code for true
            break;
        case false:
            // code for false
            break;
    }

4. Comparison Operators

Booleans are also used with comparison operators to evaluate expressions:

  • Equal to:
     
    boolean result = (a == b);
    Returns true if a is equal to b.
  • Not Equal to:
     
    boolean result = (a != b);
    Returns true if a is not equal to b.
  • Greater Than:
     
    boolean result = (a > b);
    Returns true if a is greater than b.
  • Less Than:
     
    boolean result = (a < b);
    Returns true if a is less than b.
  • Greater Than or Equal To:
     
    boolean result = (a >= b);
    Returns true if a is greater than or equal to b.
  • Less Than or Equal To:
     
    boolean result = (a <= b);
    Returns true if a is less than or equal to b.

5. Boolean Methods

The Boolean class, a wrapper class for the boolean primitive type, provides utility methods:

  • parseBoolean:
     
    boolean flag = Boolean.parseBoolean("true");
    Converts a string to a boolean value.
  • valueOf:
     
    Boolean bool = Boolean.valueOf(true);
    Returns a Boolean instance representing the specified boolean value.
  • toString:
     
    String str = Boolean.toString(true);
    Returns a string representation of the boolean value.

6. Default Value

The default value of a boolean variable when declared but not initialized is false.

Comments (0)

No comments yet. Be the first to comment!

LecturreAI Assistant

Hello! I'm LecturreAI

What would you like to learn today?