Java Integer min() method
The min() method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min. Syntax public static int min(int a, int b) Parameters The parameters ‘a’ and ‘b’ represent the first and second operand. Return Value This method returns the smaller of ‘a’ and ‘b’. Example 1
public class JavaIntegerMinExample1 { public static void main(String[] args) { Integer val1=134; Integer val2=567; // returns the smaller of two values int min=Integer.min(val1,val2); System.out.println("The smaller number between "+val1+" and "+val2+" is "+min); } }Output
The smaller number between 134 and 567 is 134Example 2
import java.util.Scanner; public class JavaIntegerMinExample2 { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); System.out.print("Enter the total number : "); int n=scanner.nextInt(); System.out.println("Enter "+n+" elements : "); int min=0; int[] a= new int[n]; for (int i=0;i<n;i++ ){ a[i]=scanner.nextInt(); } for (int i=0;i<n;i++){ for (int j=i+1;j<n;j++){ min=Integer.min(a[i],a[j]); if (a[j]==min){ a[j]=a[i]; a[i] =min ; } } } System.out.println("The smallest number is "+a[0]+" "); } }Output
Enter the total number : 5 Enter 5 elements : 198 66 0 98 78 The smallest number is 0Example 3
import java.util.Scanner; public class JavaIntegerMinExample3 { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); System.out.print("Enter the total number : "); int n=scanner.nextInt(); System.out.println("Enter "+n+" elements : "); int[] a= new int[n]; for (int i=0;i<n;i++ ){ a[i]=scanner.nextInt(); } for (int i=0;i<n;i++){ for (int j=i+1;j<n;j++){ int min=Integer.min(a[i],a[j]); if (a[j]==min){ a[j]=a[i]; a[i] =min ; } } } System.out.print("Ascending Order : "); for (int i=0;i<n;i++) { System.out.print(a[i]+" "); } } }Output
Enter the total number : 5 Enter 5 elements : 12 456 98 7 34 Ascending Order : 7 12 34 98 456