Find maximum of two numbers using Math.max

  1. /*
  2.   Find maximum of two numbers using Math.max
  3.   This java example shows how to find maximum of two int, float,
  4.   double or long numbers using max method of Java Math class.
  5. */
  6.  
  7. public class FindMaxOfTwoNumbersExample {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. /*
  12.   * To find maximum of two int values, use
  13.   * static int max(int a, int b) method of Math class.
  14.   */
  15.  
  16. System.out.println(Math.max(20,40));
  17.  
  18. /*
  19.   * To find minimum of two float values, use
  20.   * static float max(float f1, float f2) method of Math class.
  21.   */
  22. System.out.println(Math.max(324.34f,432.324f));
  23.  
  24. /*
  25.   * To find maximum of two double values, use
  26.   * static double max(double d2, double d2) method of Math class.
  27.   */
  28. System.out.println(Math.max(65.34,123.45));
  29.  
  30. /*
  31.   * To find maximum of two long values, use
  32.   * static long max(long l1, long l2) method of Math class.
  33.   */
  34.  
  35. System.out.println(Math.max(435l,523l));
  36. }
  37. }
  38.  
  39. /*
  40. Output would be
  41. 40
  42. 432.324
  43. 123.45
  44. 523
  45. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!