Starting a JVM with duplicate options

Question:
Which JVM option is used when there are duplicates

Answer:
In general, it's usually the latter option that gets used if the jvm doesn't reject it. I'm not sure if it is documented anywhere. Your best bet is to see what happens with your specific JVM, via Runtime's totalMemory and maxMemory:

Code:
public class HeapSize {
   public static final void main(String[] args) {
      Runtime rt = Runtime.getRuntime();
      System.out.println("Total currently: " + rt.totalMemory());
      System.out.println("Max:             " + rt.maxMemory());
   }
}

Output:
$ java HeapSize
Total currently: 94896128
Max:             1407713280

$ java -Xmx64m HeapSize
Total currently: 64487424
Max:             64487424

$ java -Xmx64m -Xmx512m HeapSize
Total currently: 94896128
Max:             477102080

$ java -Xmx512m -Xmx64m HeapSize
Total currently: 64487424
Max:             64487424

How to print out a factorial.

Question:
How to print out a factorial.

8! should print out:
1, 2, 6, 24, 120, 720, 5040, 40320.

Code:
import java.util.*; 
 
public class Factorial {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
 
      System.out.print("Please enter a number: "); 
      int number = sc.nextInt();
 
      System.out.print("1");
      for(int i=2;i<=number;i++){
         System.out.print( ", " + fact(i));
      }
      System.out.println(".");
   }
 
   static int fact(int b) {
      if(b <= 1) {
         return 1;
      } else {
         return b * fact(b-1);
      }
   }
}

Output:
$ java Factorial
Please enter a number: 8
1, 2, 6, 24, 120, 720, 5040, 40320.

How to print out an array in reverse order?

Question:
How to print out an array in reverse order?

Code:
public class MyArray {
 
   public static void main(String[] args) {
 
      int[] numbers = {1,2,3,4,5,6,7,8,9,10};
 
      // print forward
      for ( int i = 0; i < numbers.length; i++ ) {
         System.out.print(numbers[i] + " ");
      }
      System.out.println();
 
      // print reverse order
      for ( int i = numbers.length-1; i >= 0 ; i--) {
         System.out.print(numbers[i] + " ");
      }
      System.out.println();
   }
}

Output:
$ java MyArray
1 2 3 4 5 6 7 8 9 10 
10 9 8 7 6 5 4 3 2 1 

Print out all vowels contained in the string.

Question:
How to read string and print out all vowels contained in the string.

Code:
import java.util.Scanner;
 
public class GetVowels {
  public static void main(String[] args) {
 
    String vowels = "AEIOUaeiou";
    System.out.print("Enter a string: ");
    Scanner in = new Scanner(System.in);
    String str = in.nextLine();
    for(int i=0;i<str.length();i++){
      String onechar = str.substring(i,i+1);
      if( vowels.indexOf(onechar) != -1){
        System.out.print(onechar);
      }
    }
    System.out.println();
  }
}

Output:
$ java GetVowels
Enter a string: abcDEFghi         
aEi

How to validate an input is a positive number?

Question:
How to validate an input is a positive number?

Code:
import java.util.*;
 
public class ReadInt {
 
   public static void main(String[] args) {
 
      Scanner sc = new Scanner(System.in);
 
      while(true) {
         try {
 
            System.out.print("Enter Positive Number: ");
            int i = sc.nextInt();
            if(i > 0){
               System.out.println("You entered: " + i);
               System.out.println("Thank you");
               break;
            } else {
               System.out.println("Negative Number");
            }
 
         } catch (InputMismatchException e) {
            System.out.println("You did not enter a Number.");
            sc.nextLine(); 
         }
      }
   }
}

Output:
$ java ReadInt 
Enter Positive Number: hello
You did not enter a Number.
Enter Positive Number: -222
Negative Number
Enter Positive Number: 100
You entered: 100
Thank you

Validating that a users input is a number and within a range.

Question:
How do I validate an input ensuring a user enters a number in the range 1-10?

Answer:
See example:

Code:
import java.util.*;
 
public class ReadInt {
 
   public static void main(String[] args) {
 
      Scanner sc = new Scanner(System.in);
 
      int number = 0;
      while (true) {
         try {
            System.out.print("Enter number betwen 1 and 10: ");
            number = sc.nextInt();
            if( number >= 1 && number <= 10){
              break;
            }
            System.out.println("Out of range.");
 
         } catch (InputMismatchException e) {
            System.out.println("You did not enter a Integer.");
            sc.nextLine(); // needed to clear buffer
         }
      }
      System.out.println("You entered: " + number);
   }
}

Output:
$ java ReadInt
Enter number betwen 1 and 10: 11
Out of range.
Enter number betwen 1 and 10: a
You did not enter a Integer.
Enter number betwen 1 and 10: 12
Out of range.
Enter number betwen 1 and 10: 10
You entered: 10

Finding the lowest and highest numbers in an array of ints?

Question:
How to find the lowest and highest numbers in an array of ints?

Answer:
Use a for loop.

Code:
import java.util.*;
 
public class LowestNumber {
   public static void main(String[] args) {
 
      int[] array = new int[15];
 
      // populate array
      Random r = new Random();
      for(int i=0;i<array.length;i++){
         array[i]= r.nextInt(1000);
      }
      System.out.println("Contents: " + Arrays.toString(array));
 
      // find smallest and largest numbers
      int small = array[0];
      int large = array[0];
      for(int i=0;i<array.length;i++){
         if(array[i] <= small){
            small = array[i];
         }
         if(array[i] >= large){
            large = array[i];
         }
      }
      System.out.println("Smallest number: " + small);
      System.out.println("Largest number: " + large);
   }
}

Output:
$ java LowestNumber
Contents: [489, 283, 334, 331, 665, 643, 280, 100, 184, 642, 730, 560, 930, 517, 24]
Smallest number: 24
Largest number: 930