LAB Notes
LAB Notes
LAB Notes
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).
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.
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.
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.
class Input {
Output:
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,
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,
*/
// display result
System.out.println("Average: " + avg );
}
}
class FactorialExample{
int i,fact=1;
int number;
number = scan.nextInt();
for(i=1;i<=number;i++){
fact=fact*i;
class FactorialExample{
if (n == 0)
return 1;
else
return(n * Factorial(n-1));
int i,fact=1,number;
number = scan.nextInt();
fact=Factorial(number);
int n1=0,n2=1,n3,i,limit;
limit = scan.nextInt();
for(i=2;i<limit;++i)
n3=n1+n2;
n1=n2;
n2=n3;
}}
Java if Statement
class IfStatement {
if (number < 0) {
Output
class Main {
// if statement
if (language == "Java") {
Output
class Main {
if (number > 0) {
else {
Output
int number = 0;
if (number > 0) {
else {
Output
The number is 0.
largest = n1;
else {
largest = n3;
} else {
largest = n2;
else {
largest = n3;
Output:
class Main {
int n = 5;
// for loop
System.out.println("Java is fun");
Output
Java is fun
Java is fun
Java is fun
Java is fun
Java is fun
class Main {
int n = 5;
// for loop
System.out.println(i);
Output
class Main {
int sum = 0;
int n = 1000;
// for loop
Output:
Sum = 500500
class Main {
int sum = 0;
int n = 1000;
// for loop
class Main {
// create an array
System.out.println(number);
Output
-5