1
What is JVM?
JVM (Java Virtual Machine) is an abstract machine that enables your computer
to run a Java program.
When you run the Java program, Java compiler first compiles your Java code to
bytecode. Then, the JVM translates bytecode into native machine code (set of
instructions that a computer's CPU executes directly).
Java is a platform-independent language. It's because when you write Java
code, it's ultimately written for JVM but not your physical machine (computer).
Since JVM executes the Java bytecode which is platform-independent, Java is
platform-independent.
What is JRE?
JRE (Java Runtime Environment) is a software package that provides Java class
libraries, Java Virtual Machine (JVM), and other components that are required
to run Java applications.
JRE is the superset of JVM.
What is JDK?
JDK (Java Development Kit) is a software development kit required to develop
applications in Java. When you download JDK, JRE is also downloaded with it.
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
2
In addition to JRE, JDK also contains a number of development tools
(compilers, JavaDoc, Java Debugger, etc).
Relationship between JVM, JRE, and JDK.
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
3
Java Input
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
System.out.println("You entered: " + number);
// closing the scanner object
input.close();
}
}
Output:
Enter an integer: 23
You entered 23
In the above example, we have created an object named input of the Scanner
class. We then call the nextInt() method of the Scanner class to get an integer
input from the user.
Similarly, we can use nextLong(), nextFloat(), nextDouble(), and next()
methods to get long, float, double, and string input respectively from the user.
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
4
Get float, double and String Input
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Getting float input
System.out.print("Enter float: ");
float myFloat = input.nextFloat();
System.out.println("Float entered = " + myFloat);
// Getting double input
System.out.print("Enter double: ");
double myDouble = input.nextDouble();
System.out.println("Double entered = " + myDouble);
// Getting String input
System.out.print("Enter text: ");
String myString = input.next();
System.out.println("Text entered = " + myString);
Output:
Enter float: 2.343
Float entered = 2.343
Enter double: -23.4
Double entered = -23.4
Enter text: Hey!
Text entered = Hey!
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
5
Single-line Comment
A single-line comment starts and ends in the same line. To write a single-line
comment, we can use the // symbol. For example,
// "Hello, World!" program example
Multi-line Comment
When we want to write comments in multiple lines, we can use the multi-line
comment. To write multi-line comments, we can use the /*....*/ symbol. For
example,
/* This is an example of multi-line comment.
* The program prints "Hello, World!" to the standard output.
*/
Write a Java Program to calculate sum and average of two numbers
import java.util.Scanner;
public class Average {
public static void main(String[] args) {
// create Scanner class object
Scanner scan = new Scanner(System.in);
// declare two numbers
double num1 = 0;
double num2 = 0;
// declare sum variable
// and initialize with 0
double sum = 0.0;
// declare average variable
double avg = 0.0;
// take two numbers
System.out.print("Enter two numbers: ");
n1 = scan.nextDouble();
num2 um= scan.nextDouble();
// calculate the sum value
sum = num1 + num2;
// calculate the average value
avg = sum/2;
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
6
// display result
System.out.println("Average: " + avg );
}
}
Write a Java program to calculate a Factorial of a number.
import java.util.Scanner;
class FactorialExample{
public static void main(String args[]){
int i,fact=1;
Scanner scan = new Scanner(System.in);
//int number=5;//It is the number to calculate factorial //
int number;
System.out.println("enter the number:");
number = scan.nextInt();
for(i=1;i<=number;i++){
fact=fact*i;
System.out.println("Factorial of "+number+" is: "+fact);
Factorial Program using recursion in java
import java.util.Scanner;
class FactorialExample{
static int Factorial (int n)
if (n == 0)
return 1;
else
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
7
return(n * Factorial(n-1));
public static void main(String args[]){
int i,fact=1,number;
Scanner scan = new Scanner(System.in);
System.out.println("enter the number:");
number = scan.nextInt();
fact=Factorial(number);
System.out.println("Factorial of "+number+" is: "+fact);
Write a Java program to calculate Fibonacci Series up to n term
import java.util.Scanner;
public class Fibonacci{
public static void main(String args[])
int n1=0,n2=1,n3,i,limit;
System.out.println("Enter the limit:");
Scanner scan = new Scanner(System.in);
limit = scan.nextInt();
System.out.print(n1+" "+n2+" ");
for(i=2;i<limit;++i)
n3=n1+n2;
System.out.print(" "+n3+" ");
n1=n2;
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
8
n2=n3;
}}
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
9
Java if Statement
class IfStatement {
public static void main(String[] args) {
int number = 10;
// checks if number is less than 0
if (number < 0) {
System.out.println("The number is negative.");
System.out.println("Statement outside if block");
Output
Statement outside if block
Java if with String
class Main {
public static void main(String[] args) {
// create a string variable
String language = "Java";
// if statement
if (language == "Java") {
System.out.println("Best Programming Language");
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
10
Output
Best Programming Language
Java if...else Statement
class Main {
public static void main(String[] args) {
int number = 10;
// checks if number is greater than 0
if (number > 0) {
System.out.println("The number is positive.");
// execute this block
// if number is not greater than 0
else {
System.out.println("The number is not positive.");
System.out.println("Statement outside if...else block");
Output
The number is positive.
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
11
Statement outside if...else block
Java if...else...if Statement
class Main {
public static void main(String[] args) {
int number = 0;
// checks if number is greater than 0
if (number > 0) {
System.out.println("The number is positive.");
// checks if number is less than 0
else if (number < 0) {
System.out.println("The number is negative.");
// if both condition is false
else {
System.out.println("The number is 0.");
Output
The number is 0.
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
12
Nested if...else Statement
class Main {
public static void main(String[] args) {
// declaring double type variables
Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largest;
// checks if n1 is greater than or equal to n2
if (n1 >= n2) {
// if...else statement inside the if block
// checks if n1 is greater than or equal to n3
if (n1 >= n3) {
largest = n1;
else {
largest = n3;
} else {
// if..else statement inside else block
// checks if n2 is greater than or equal to n3
if (n2 >= n3) {
largest = n2;
else {
largest = n3;
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
13
System.out.println("Largest Number: " + largest);
Output:
Largest Number: 4.5
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
14
Java for Loop
Display a Text Five Times
// Program to print a text 5 times
class Main {
public static void main(String[] args) {
int n = 5;
// for loop
for (int i = 1; i <= n; ++i) {
System.out.println("Java is fun");
Output
Java is fun
Java is fun
Java is fun
Java is fun
Java is fun
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
15
Display numbers from 1 to 5
// Program to print numbers from 1 to 5
class Main {
public static void main(String[] args) {
int n = 5;
// for loop
for (int i = 1; i <= n; ++i) {
System.out.println(i);
Output
Display Sum of n Natural Numbers
// Program to find the sum of natural numbers from 1 to 1000.
class Main {
public static void main(String[] args) {
int sum = 0;
int n = 1000;
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
16
// for loop
for (int i = 1; i <= n; ++i) {
// body inside for loop
sum += i; // sum = sum + i
System.out.println("Sum = " + sum);
Output:
Sum = 500500
// Program to find the sum of natural numbers from 1 to 1000.
class Main {
public static void main(String[] args) {
int sum = 0;
int n = 1000;
// for loop
for (int i = n; i >= 1; --i) {
// body inside for loop
sum += i; // sum = sum + i
System.out.println("Sum = " + sum);
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT
17
Java for-each Loop
The Java for loop has an alternative syntax that makes it easy to iterate through
arrays and collections. For example,
// print array elements
class Main {
public static void main(String[] args) {
// create an array
int[] numbers = {3, 7, 5, -5};
// iterating through the array
for (int number: numbers) {
System.out.println(number);
Output
-5
Ms. Srirupa Das, Assistant Professor, Dept. of CSE, RCCIIT