Difference between JDK, JRE and JVM

Difference between JDK, JRE and JVM

 

Lets see whats more like the basic differences between them.

JDK basically contains everything you need to compile a java program. It indirectly also contains everything to run a java program also. The reason is because JDK contains the JRE also.

However, if you just have JRE, you would be able to run a java program (the byte code), but you would not be able to compile or debug a java program.

It would be important to note that, its JVM and not JRE , which actually runs the Java Byte Code. JRE  provides JVM with the class libraries and other supporting files to run the program.

Program to find the smallest number in an array

Input

NA. The array of numbers is already provided in the program

Program Code

package programs.java4newbie.com;

/*
* This Program finds the smallest number and prints it.
*/
public class SmallestNumber {

public static void main(String[] args) {

//array of 10 numbers
int numbers[] = new int[]{123,321,423,12,4,4444,567,7897,43,9};

//assign first element of an array to min
int min = numbers[0];

for(int i=1; i< numbers.length; i++)
{ // if current number is smaller than min, then this is the new min
if(numbers[i] < min)
min = numbers[i];

}

System.out.println("Smallest Number is : " + min);
}
}

Output

Smallest Number is : 4

Program to find the largest number in an array

Input

NA. The array of number is already provided in program

Program Code

package programs.java4newbie.com;

/*
* This Program finds the largest number and prints it.
*/
public class LargestNumber {

public static void main(String[] args) {

//array of 10 numbers
int numbers[] = new int[]{123,321,423,12,4,4444,567,7897,43,9};

//assign first element of an array to max
int max = numbers[0];

for(int i=1; i< numbers.length; i++)
{ // if current number is greater than max, then this is the new max
if(numbers[i] > max)
max = numbers[i];

}

System.out.println("Largest Number is : " + max);
}
}

Output

Largest Number is : 7897

Program to reverse a number

Input

NA. Since, the number to be reversed is already provided in Program

Program Code

package programs.java4newbie.com;

/*
* This Program reverses the original number by extracting digits
* one by one from the last to first.
*
*/
public class ReverseNumber {

public static void main(String[] args) {

//original number
int originalNumber = 1234;
int reversedNumber = 0;
int temp = 0;

while(originalNumber > 0){

//extract the last digit using mod operator
temp = originalNumber%10;

//create the reversed number
reversedNumber = reversedNumber * 10 + temp;
originalNumber = originalNumber/10;

}

//print the reversed number
System.out.println("Reversed Number is: " + reversedNumber);
}
}

Output

Before Swapping
Value of num1 is :10
Value of num2 is :20
After Swapping
Value of num1 is :20
Value of num2 is :10

Program to swap two numbers using a temporary variable

Input

NA. Since the value for two numbers is already provided in code.

Program Code

package programs.java4newbie.com;

/**
*
* This program swaps two numbers using a temporary variable.
*
*/
public class SwapTwoNumbersUsingTemp {

public static void main(String[] args) {

int num1 = 10;
int num2 = 20;

System.out.println("Before Swapping");
System.out.println("Value of num1 is :" + num1);
System.out.println("Value of num2 is :" +num2);

//swap the value
int temp = num1;
num1 = num2;
num2 = temp;

System.out.println("After Swapping");
System.out.println("Value of num1 is :" + num1);
System.out.println("Value of num2 is :" +num2);

}

}

 

Output

Before Swapping
Value of num1 is :10
Value of num2 is :20
After Swapping
Value of num1 is :20
Value of num2 is :10

Program to swap two numbers without a temporary or third number

Input

NA. Input already provided in Program.

Program Code

package programs.java4newbie.com;

/**
 * 
 * This program swaps two numbers without using a temporary or third number
 *
 */
public class SwapTwoNumbers {
	
	public static void main(String args[]){

    int num1 = 10;
    int num2 = 20;
   
    System.out.println("Before Swapping");
    System.out.println("Value of num1 is : " + num1);
    System.out.println("Value of num2 is : " +num2);
  
    
    
    // Code to swap 'num1' and 'num2'
    num1 = num1 + num2;  // num1 now becomes 30
    num2 = num1 - num2;  // num2 becomes 10
    num1 = num1 - num2;  // num1 becomes 20
    
    System.out.println("After Swapping");
    System.out.println("Value of num1 is : "+ num1);
    System.out.println("Value of num2 is : "+ num2);
}
}

Output

Before Swapping
Value of num1 is : 10
Value of num2 is : 20
After Swapping
Value of num1 is : 20
Value of num2 is : 10

Java Program to calculate factorial of a number by recursion

Input

Number whose factorial is to be calculated by recursion.

Example: 3

Program Code

package programs.java4newbie.com;

/*
This Program calculates the factorial of the number by recursion
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FactorialByRecursion {

public static void main(String args[]) throws NumberFormatException, IOException{

System.out.println("Enter the number whose factorial is to be calculated: ");

//get input from the user
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());

//call the recursive function to generate factorial
int result= factorial(a);

System.out.println("Factorial of the number is: " + result);
}

static int factorial(int a)
{
if(a <= 1)
//if the number is 1 then return 1, since factorial of 1 is 1
return 1;
else
//else call the same function by recursion
return a * factorial(a-1);
}
}

Output

Enter the number:
3
Factorial of the number is: 6

 

Program to print HelloWorld

Input

NA

Program Code

package programs.java4newbie.com;

/*
This program prints HelloWorld!
*/

public class HelloWorld{

public static void main(String args[]){

/*
System.out.println() to print on console.
*/
System.out.println("Hello World!");

}

}

Output

Hello World!

Java Program to calculate factorial of a number

Input

NA, Since the value for number is set to 5 in the program itself

Program Code

package programs.java4newbie.com;

/*
This Program calculates the factorial of a number using the formula n!= n*(n-1)*(n-2)...1
*/

public class Factorial {

public static void main(String[] args) {
int factorial = 1 ; //Initialise factorial variable to 1

int number = 5;

for(int counter = number; counter &gt;=1 ; counter--)
{
factorial = factorial * counter;
}

/*
* 1st Loop: counter = 5
* 2nd Loop: counter = 4
* 3nd Loop: counter = 3
* 4nd Loop: counter = 2
* 5nd Loop: counter = 1
*/

// factorial of 5 is 5*4*3*2*1 = 120
System.out.println("Factorial of the number is " + factorial);
}
}

Output

Factorial of the number is 120

Program to Calculate the Area of a circle

Input

1. Radius of the circle in integer value (example radius=4)

Program Code

package programs.java4newbie.com;

/*
 This program accepts an integer value for radius of circle
 and then return the area of the circle.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CircleArea {

 public static void main(String[] args) {

 float radius = 0;
 System.out.println("Please enter radius of a circle");

 try {
 // get the radius from console
 BufferedReader br = new BufferedReader(new InputStreamReader(
 System.in));
 radius = Float.parseFloat(br.readLine());
 }
 // if invalid value was entered
 catch (NumberFormatException nfe) {
 System.out
 .println("Invalid value for radius entered, please try again with a valid value ");
 System.exit(0);
 } catch (IOException ioe) {
 System.out.println("IO Error ");
 System.exit(0);
 }

 /*
 * Area of a circle is pi * r * r where r is a radius of a circle.
 */

 // NOTE : use Math.PI constant to get value of pi
 double area = Math.PI * radius * radius;

 System.out.println("Area of a circle is " + area);
 }
}

Output

Please enter radius of a circle
5
Area of a circle is 78.53981633974483