[Java] Summary of mathematical operations

Mathematical arithmetic

Basic operation

public class Main {
  public static void main(String[] args) {
    //Absolute value,maximum,minimum
    System.out.println(Math.abs(-100)); //100
    System.out.println(Math.max(6, 3)); //6
    System.out.println(Math.min(6, 3)); //3
    //Round up
    System.out.println(Math.ceil(1234.56));  //1235.0
    System.out.println(Math.floor(1234.56)); //1234.0
    System.out.println(Math.round(1234.56)); //1235
    //square root
    System.out.println(Math.sqrt(10000));    //100.0
    //Cube root
    System.out.println(Math.cbrt(10000));    //21.544346900318835
    //x to the yth power
    System.out.println(Math.pow(2, 4));      //16.0
    //Convert angle to radian
    System.out.println(Math.sin(Math.toRadians(30))); //0.49999999999999994
    System.out.println(Math.cos(Math.toRadians(60))); //0.5000000000000001
    System.out.println(Math.tan(Math.toRadians(45))); //0.9999999999999999
    //Radix of base e, logarithm of base 10
    System.out.println(Math.log(100));                //4.605170185988092
    System.out.println(Math.log10(100));              //2.0

Long type or larger integer operation

//NG example Long type Overflow from the place where the upper limit is exceeded
public class Main {
  public static void main(String[] args) {
    long result = 1;
    for (var i = 1; i < 26; i++) {
      result *= i;
//OK operation
import java.math.BigInteger;
public class Main {
  public static void main(String[] args) {
    var result = BigInteger.valueOf(1);
    for (var i = 1; i < 26; i++) {
      result = result.multiply(BigInteger.valueOf(i));

Random number generation

import java.util.Random;

public class Main {
  public static void main(String[] args) {
    var rnd = new Random();
    //Random number generation with Boolean value
    System.out.println(rnd.nextBoolean()); //true
    //Random number generation with Float(0~1)
    System.out.println(rnd.nextFloat());   //0.11219084
    //Random number generation with Double(0~1)
    System.out.println(rnd.nextDouble());  //0.983418224742081
    //Random number generation with Int(0~bound)
    System.out.println(rnd.nextInt(400) + 100); //448
    //Random number generation with Long
    System.out.println(rnd.nextLong()); //2297825179350643621
    var data = new byte[5];
    //Randomly generate bytes and place them in a byte array with specified arguments
    for (var b : data) {
      System.out.print(b + " "); //63 -54 23 113 -76 

Numerical shaping

import java.text.NumberFormat;
import java.util.Locale;

public class Main {
  public static void main(String[] args) {
    var num1 = 1234.5678;
    //Currency format
    var nf1 = NumberFormat.getCurrencyInstance(Locale.JAPAN); 
    //Integer format
    var nf2 = NumberFormat.getIntegerInstance();
    //General-purpose numeric format (getInstance)
    var nf3 = NumberFormat.getNumberInstance();
    System.out.println(nf1.format(num1)); //¥1,235
    System.out.println(nf2.format(num1)); //1,235
    System.out.println(nf3.format(num1)); //1,234.568

    var num2 = 0.567;
    var nf4 = NumberFormat.getPercentInstance();
    System.out.println(nf4.format(num2)); //57%

Array manipulation

//Shallow copy
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    var  array1 = new String[] { "dog", "cat", "mouse", "fox", "lion" };
    //Sort array
    //Stringize an array
    System.out.println(Arrays.toString(array1)); //[cat, dog, fox, lion, mouse]
    //Find values in a sorted array
    System.out.println(Arrays.binarySearch(array1, "mouse")); //4

    var array2 = new String[] { "Ah", "I", "U", "e", "O" };   
    //Array copy, length as argument, 0 shortage/Fill with null
    var array3 = Arrays.copyOf(array2, 2);
    System.out.println(Arrays.toString(array3)); //[Ah,I]
    //Copy the array by specifying the range with the argument
    var array4 = Arrays.copyOfRange(array2, 1, 7);
    System.out.println(Arrays.toString(array4)); //[I,U,e,O, null, null]

    //Set values in the array
    Arrays.fill(array4, 4, 6, "―");
    System.out.println(Arrays.toString(array4)); //[I,U,e,O, ―, ―]
//Deep copy
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    var list1 = new StringBuilder[] {
        new StringBuilder("Doremifa Donuts"),
        new StringBuilder("ARAMA"),
        new StringBuilder("Hanihoheto")

    var list2 = new StringBuilder[list1.length];
    for (var i = 0; i < list1.length; i++) {
      list2[i] = new StringBuilder(list1[i].toString());

    System.out.println(Arrays.toString(list1)); //[Doremifa Donuts, ARAMA,Hanihoheto Hello]
    System.out.println(Arrays.toString(list2)); //[Doremifa Donuts, ARAMA,Hanihoheto]

Recommended Posts

[Java] Summary of mathematical operations
Summary of Java support 2018
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
[Java] Summary of control syntax
Summary of java error processing
[For beginners] Summary of java constructor
Summary of [Java silver study] package
Summary of object-oriented programming using Java
[Java Silver] Summary of access modifier points
Summary of in-house newcomer study session [Java]
Java knowledge summary
Java Generics Summary
[java] Summary of how to handle char
Summary of changes other than JEP of Java10
[Java] Personal summary of conditional statements (basic)
Java 8 documentation summary
[Java] [Maven3] Summary of how to use Maven3
Java Summary of frequently searched type conversions
Java 11 document summary
Summary of Java Math.random and import (Calendar)
[java] Summary of how to handle character strings
Summary of Java environment settings for myself [mac]
[Java] Personal summary of classes and methods (basic)
[Java] Summary of how to abbreviate lambda expressions
Expired collection of java
Java 12 new feature summary
[Java] Significance of serialVersionUID
[Summary] Java environment preparation
effective java 3rd summary
[Java] Date / time operations
NIO.2 review of java
Review of java Shilber
Java 13 new feature summary
Summary of OpenJDK sources
Summary of strong parameters
java --Unification of comments
Summary of jar files
Java static [Personal summary]
History of Java annotation
Summary of information security
Summary of using FragmentArgs
java (merits of polymorphism)
Thread safe summary ~ Java ~
Java Primitive Specialization Summary
Java development link summary
Personal summary about Java
NIO review of java
Java 10 new feature summary
[Java] Three features of Java
Java 14 new feature summary
Java reserved word summary
Java8 Stream Rough Summary
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
This is convenient! Summary of popular Java library + α
Summary of ToString behavior with Java and Groovy annotations
[Java Silver] Summary of points related to lambda expressions