The Java Math class provides a collection of methods for performing basic numeric operations such as exponentiation, logarithms, square roots, and trigonometric functions. These methods are static, allowing them to be called without creating an instance of the Math class.
The Math.abs() method returns the absolute value of a given number. It can handle different types of numeric data, including int, long, float, and double.
public class MathExample {
public static void main(String[] args) {
System.out.println(Math.abs(-10)); // Output: 10
System.out.println(Math.abs(5.5)); // Output: 5.5
}
}
Math.max() returns the greater of two values, while Math.min() returns the smaller. These methods are useful in scenarios where comparisons between two values are necessary.
public class MathExample {
public static void main(String[] args) {
System.out.println(Math.max(10, 20)); // Output: 20
System.out.println(Math.min(10, 20)); // Output: 10
}
}
The Math.sqrt() method calculates the square root of a number. It is particularly useful in mathematical computations where determining the root of a number is required.
public class MathExample {
public static void main(String[] args) {
System.out.println(Math.sqrt(16)); // Output: 4.0
System.out.println(Math.sqrt(2)); // Output: 1.4142135623730951
}
}
The Math.pow() method raises a base to the power of an exponent, providing a way to perform exponential calculations.
public class MathExample {
public static void main(String[] args) {
System.out.println(Math.pow(2, 3)); // Output: 8.0
System.out.println(Math.pow(5, 2)); // Output: 25.0
}
}
The Math.random() method generates a random double value greater than or equal to 0.0 and less than 1.0. It's often used in applications requiring random number generation.
public class MathExample {
public static void main(String[] args) {
System.out.println(Math.random()); // Output: A random number between 0.0 and 1.0
}
}
Math.ceil() rounds a number up to the nearest integer, while Math.floor() rounds down to the nearest integer. These methods are useful in situations where precise control over rounding behavior is needed.
public class MathExample {
public static void main(String[] args) {
System.out.println(Math.ceil(4.3)); // Output: 5.0
System.out.println(Math.floor(4.7)); // Output: 4.0
}
}
The Math.round() method rounds a floating-point number to the nearest integer. It is particularly useful when exact whole numbers are required from floating-point calculations.
public class MathExample {
public static void main(String[] args) {
System.out.println(Math.round(4.5)); // Output: 5
System.out.println(Math.round(4.4)); // Output: 4
}
}
Math.log() returns the natural logarithm (base e) of a double value, while Math.log10() returns the base 10 logarithm. These methods are essential in scientific calculations involving exponential growth or decay.
public class MathExample {
public static void main(String[] args) {
System.out.println(Math.log(1)); // Output: 0.0
System.out.println(Math.log10(100)); // Output: 2.0
}
}
These trigonometric methods calculate the sine, cosine, and tangent of an angle (in radians), respectively. They are widely used in fields such as physics and engineering.
public class MathExample {
public static void main(String[] args) {
System.out.println(Math.sin(Math.PI / 2)); // Output: 1.0
System.out.println(Math.cos(0)); // Output: 1.0
System.out.println(Math.tan(Math.PI / 4)); // Output: 1.0
}
}
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies