Professional Documents
Culture Documents
The bytecode is executed using a Java runtime environment which is called a Java
Virtual Machine (JVM). Sometimes JVM is also called an interpreter for bytecode.
The programs that are running on JVM must be compiled into a binary format which is
denoted by .class files.
Sometime for ease of distribution multiple class files are packaged into one jar file.
The JVM executes .class or .jar files, by either interpreting it or using a just-in-time
compiler (JIT) .
The JIT is used for compiling and not for interpreting the file. It is used in most JVMs
today to achieve greater speed.
The bytecode verifier verifies all the bytecode before it is executed. This verification
helps to prevent the crashing of the host machine.
The Java Development Kit (JDK) is a collection of tools that are used to develop,
compile, and run Java programs. Java Development Kit consists of –
javac – The javac is the Java compiler that translates the Java source code into
bytecode form and stores it in a separate class file.
java – The java in the Java interpreter, which interprets the bytecode stored in the
class file and executes the program to generate output.
javadoc – The javadocis used to convert the Java source code into HTML document
for documentation from source code file.
javah – The javah is used to produce the header files for the use of native methods.
jdb – The jdb is the Java debugger which is used to locate the errors in the program.
First, user creates the Java source code using any text editor like Notepad, Notepad++
etc.
Next, the source code is compiled using the javac compiler. The javac compiler
converts the source program into a class file which consists of the byte code.
The developer can use javadoc tool to create the HTML files that document the source
program.
The developer may use javah tool for creating the header files.
Finally, the java tool is used to interpret the class file generated by javac tool in order to
produce an executable.
2. Write a Java Program to Find the Sum of array elements, Product of array
elements, and Average of elements of a single dimensional array.
1. Declare the local variables like a, sum, prod, avg to store array elements, the sum of
the array, a product of array, and an average of array elements respectively.
5. Find the sum, product of elements of an array, then find the average using the sum
and number of elements in an array.
Source code of Java Program to find Sum, Product, and Avg. of an Array
import java.util.Scanner;
/*
* Java Program to find the Sum, Product and Average of
elements single
* dimensional array
Output:
Logic is: f1 = 0, f2 = 1, f3 = f1 + f2
2. Create an object of Scanner class to read the input from standard keyboard.
5. If the value of n is greater than 2 then display f1, f2 and calculate the next number in
the series using formula f3=f1+f2.
6. if the value of n is less than or equal 0 then display that the error message like
invalid input.
Source code of Java Program to print first “n” Numbers in Fibonacci Series
import java.util.Scanner;
/*
Write a Java Program to display first "n" numbers in
Fibonacci series
Subscribe to Mahesh Huddar YouTube Channel for more Videos
Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13 and so on
Logic: f1 = 0, f2 = 1, f3 = f1 + f2
*/
public class Fibonacci
{
public static void main(String args[])
{
int n, f1 = 0, f2 = 1, f3;
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of n:");
n = input.nextInt();
System.out.print("Fibonacci series is: ");
if (n == 1)
{
System.out.print(f1);
}
else if (n == 2)
{
System.out.print(f1 +" "+ f2);
}
else if (n > 2)
{
System.out.print(f1 +" "+ f2+" ");
for (int i = 3; i<=n; i++)
{
f3 = f1 + f2;
System.out.print(f3+" ");
f1 = f2;
f2 = f3;
}
}
else
{
System.out.print("Invalid Input - Enter the
value of n greater than 0");
}
}
}
Output:
Case 1:
Case 2:
Case 3:
Case 4:
Fibonacci series is: Invalid Input – Enter the value of n greater than 0
5. Write a Java program to find the number of and the sum of all numbers
greater than 100 and less than 200 which are divisible by 7.
1. Declare two local variables sum and count both are set to 0 to store the sum of all
numbers between 100 and 200 which are divisible by 7 and count of those numbers
respectively.
2. Use a for loop a to loop over from 101 to 199.
3. Check all the numbers between 100 to 200, whether they are divisible by 7 using
mod operator.
4. If number is divisible by 7, then add number to previous sum and increment the
count.
5. after the loop, display the result, that is Sum of number between 100 to 200 which
are divisible by 7 and count of numbers between 100 to 200 which are divisible by 7.
Source code of Java Program to find the sum of numbers divisible by 7 between
100 and 200
/*
* Write a Java program to find the number of and the sum
of all
* numbers greater than 100 and less than 200 which are
divisible by 7
Output:
The Sum of the number between 100 to 200 which are divisible by 7 is: 2107