Java program to swap two numbers.

Question:
How can you swap two numbers in java?

Answer:
Swapping using temporary or third variable.

Code:
import java.util.Scanner;
 
public class Swap {
   public static void main(String[] args) {
 
      Scanner sc = new Scanner(System.in);
 
      System.out.print("Enter first int: ");
      int i1 = sc.nextInt();
 
      System.out.print("Enter first int: ");
      int i2 = sc.nextInt();
 
      System.out.println("Value #1 " + i1);
      System.out.println("Value #2 " + i2);
 
      System.out.println("Swapping...");
 
      int temp = i1;
      i1 = i2;
      i2 = temp;
 
      System.out.println("Value #1 " + i1);
      System.out.println("Value #2 " + i2);
 
   }
}

Output:
$ java Swap
Enter first int: 1
Enter first int: 4
Value #1 1
Value #2 4
Swapping...
Value #1 4
Value #2 1

How to write a program in Java that calculates the difference between 2 user input dates?

Question:
How to write a program in Java that calculates the difference between 2 user input dates?

Code:
import java.util.*;
import java.text.*;
 
public class CompareDates {
   public static void main( String[] args ) {
 
      Scanner sc = new Scanner(System.in);
 
      System.out.print("Enter date1 (yyyy-MM-dd): ");
      String d1 = sc.nextLine();
 
      System.out.print("Enter date2 (yyyy-MM-dd): ");
      String d2 = sc.nextLine();
 
      try {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         Date date1 = sdf.parse(d1);
         Date date2 = sdf.parse(d2);
 
         System.out.println("Date1: " + sdf.format(date1));
         System.out.println("Date2: " + sdf.format(date2));
 
         long diff = date1.getTime() - date2.getTime();
         diff = diff / (1000L*60L*60L*24L);
         System.out.println("Delta: " + diff + " days");
 
      } catch(ParseException ex) {
      }
   }
}

Output:
$ java CompareDates
Enter date1 (yyyy-MM-dd): 2000-11-11
Enter date2 (yyyy-MM-dd): 2000-10-11
Date1: 2000-11-11
Date2: 2000-10-11
Delta: 31 days

Round numbers to whole number without using the Math.round

Question:
How to round decimals to whole number, without using the Math.round or ceiling program?

Answer:
You can do it with casting.

Code:
import java.util.Scanner; 
 
public class Rounding {
   public static void main(String[] args) {
 
      Scanner scan = new Scanner(System.in);
 
      System.out.print("Enter double: " );
      Double d = scan.nextDouble();
      System.out.println("You entered: " + d);
      Integer i = d.intValue();
 
      if ( d - i >= .5 ) {
        i=i+1;
      }
 
      System.out.println("Rounded: " + i);
 
   }
}

Output:
$ java Rounding
Enter double: 12.4999
You entered: 12.4999
Rounded: 12


$ java Rounding
Enter double: 8.5
You entered: 8.5
Rounded: 9

Validating an input is a number and is greater then 99.

Question:
Design an algorithm that prompts the user to enter a number that is greater than 99 and validates the input.

Code:
import java.util.*;
 
public class ReadInt {
 
   public static void main(String[] args) {
 
      Scanner sc = new Scanner(System.in);
      int i=0;
 
      try {
 
         System.out.print("Enter Integer: ");
         i = sc.nextInt();
         System.out.println("You entered: " + i);
 
         if(i > 99){
            System.out.println(i + " is greater than 99");
         } else {
            System.out.println(i + " is NOT greater than 99");
         }
 
      } catch (InputMismatchException e) {
         System.out.println("You did not enter a number.");
      }
 
   }
}

Output:
$ java ReadInt
Enter Integer: 55
You entered: 55
55 is NOT greater than 99

$ java ReadInt
Enter Integer: 100
You entered: 100
100 is greater than 99

$ java ReadInt
Enter Integer: foo
You did not enter a number.

A java program to find the sum of squares.

Question:
A java program to find the sum of squares.

Code:
import java.util.Scanner;
 
public class SumOfSquares {
 
   public static void main(String[] args) {
 
      Scanner sc = new Scanner(System.in);
 
      System.out.print("Enter Integer: ");
      int n = sc.nextInt();
      System.out.println("You entered: " + n);
 
      int sum = 1;
 
      for (int x=2;x<=n;x++) {
        sum = sum + (x*x);
      }
      System.out.println(sum);
 
   }
}

Output:
$ java SumOfSquares
Enter Integer: 5
You entered: 5
55

How to find default encoding in java?

Question:
How to find default encoding in java?

Code:
import java.io.*;
 
public class DefaultCharacterEncoding {
   public static void main(String[] args) {
 
      byte [] array = {'w'};
      InputStream is = new ByteArrayInputStream(array);
      InputStreamReader reader = new InputStreamReader(is);
      String dce = reader.getEncoding();
 
      System.out.println("Default character encoding: "  + dce);
   }
}

Output:
$ java DefaultCharacterEncoding
Default character encoding: UTF8

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