توفر Java كلاسًا اسمه Math يحتوي على مجموعة من الدوال الرياضية الجاهزة التي تُغني المطور عن كتابة هذه العمليات يدويًا. كل دواله ساكنة static فلا حاجة لإنشاء كائن لاستخدامها.
أبسط الدوال
System.out.println(Math.max(5, 10)); // 10
System.out.println(Math.min(5, 10)); // 5
System.out.println(Math.abs(-7)); // 7
System.out.println(Math.sqrt(16)); // 4.0
System.out.println(Math.pow(2, 8)); // 256.0
التقريب
System.out.println(Math.round(3.6)); // 4
System.out.println(Math.floor(3.9)); // 3.0
System.out.println(Math.ceil(3.1)); // 4.0
الأرقام العشوائية
double r = Math.random(); // بين 0 و 1
int n = (int)(Math.random() * 100); // بين 0 و 99
ثوابت رياضية
System.out.println(Math.PI);
System.out.println(Math.E);