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
//Truncate
System.out.println(Math.floor(1234.56)); //1234.0
//Rounding
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
}
}
//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;
System.out.println(result);
//1,2,,,(Abbreviation),2432902008176640000,-4249290049419214848,-1250660718674968576,8128291617894825984,-7835185981329244160,7034535277573963776
}
}
}
//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));
System.out.println(result);
//1,2,6,,,(Abbreviation),,2432902008176640000,51090942171709440000,1124000727777607680000,25852016738884976640000,620448401733239439360000,15511210043330985984000000
}
}
}
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
rnd.nextBytes(data);
for (var b : data) {
System.out.print(b + " "); //63 -54 23 113 -76
}
}
}
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;
//%format
var nf4 = NumberFormat.getPercentInstance();
System.out.println(nf4.format(num2)); //57%
}
}
//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
Arrays.sort(array1);
//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());
}
list1[2].append("Hello");
System.out.println(Arrays.toString(list1)); //[Doremifa Donuts, ARAMA,Hanihoheto Hello]
System.out.println(Arrays.toString(list2)); //[Doremifa Donuts, ARAMA,Hanihoheto]
}
}
Recommended Posts