Let's take a look at the Math class from java.lang which has the static max and min methods that can be used to find the max and the min of two numbers passed in as a input argument.
Example:package org.example;
public class MaxMinAvgExample {
public static void main(String[] args) {
int no1 = 10;
int no2 = 20;
//max
System.out.println("Max number is: " + Math.max(no1, no2));
//min
System.out.println("Min number is: " + Math.min(no1, no2));
}
}
Output:
Max number is: 20
Min number is: 10
More Examples:
Math.max(14, 12) -> Output: 12
Math.min(-1, 2) -> Output: -1
Math.max(14.2, 14.3) -> Output: 14.3
Math.min(2.2, 4.5) -> Output: 2.2
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts: - How to Split a String in Python? - Python
- Bash getopts Command Example - Bash
- How to Customize Ribbon in Excel for Mac - MacOS
- Convert Java String to JSON Object using Jackson - Java
- Format Python Code in Visual Studio Code (VS Code) - Python
- Python: Merge two dictionaries into one dictionary - Python
- MySQL: How to Select Database via Terminal/Command Line - MySQL
- How to Copy full Absolute Path of a File on Mac - MacOS