0% found this document useful (0 votes)
23 views8 pages

Unit - I

The document provides an overview of the Java Virtual Machine (JVM) architecture, detailing its components such as Classloader, Heap, Stack, and Execution Engine. It also explains the structure of a Java program, including sections like Documentation, Package Declaration, Import Statements, and the main method. Additionally, it covers concepts related to static variables and methods, the String class, and methods associated with it.

Uploaded by

haiitzmex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

Unit - I

The document provides an overview of the Java Virtual Machine (JVM) architecture, detailing its components such as Classloader, Heap, Stack, and Execution Engine. It also explains the structure of a Java program, including sections like Documentation, Package Declaration, Import Statements, and the main method. Additionally, it covers concepts related to static variables and methods, the String class, and methods associated with it.

Uploaded by

haiitzmex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

JVM (Java Virtual Machine) Architecture

● JVM is a virtual machine that enables the execution of Java bytecode.

JVM Architecture

Classloader
● It is a subsystem of JVM.
● It is used to load class files.
● There are three built-in classloaders in Java.
1. Bootstrap ClassLoader: It loads the rt.jar file, it contains all class files of Java
Standard Edition like java.lang package classes, java.net package classes, java.util
package classes, java.io package classes, java.sql package classes etc.
2. Extension ClassLoader: It loads the jar files located inside the
$JAVA_HOME/jre/lib/ext directory.
3. System/Application ClassLoader: It loads the class files from classpath.

Class(Method) Area

● It stores per-class structures such as the runtime constant pool, field and method data, the
code for methods.
Heap
● It is the runtime data area, objects are allocated.

Stack
● It stores frames. It holds local variables and partial results, and plays a part in method
invocation and return.

Program Counter Register


● It contains the address of the JVM currently being executed.

Native Method Stack


● It contains all the native methods used in the application.

Execution Engine
● It contains a virtual processor, A Interpreter and Just-In-Time(JIT) compiler.

Java Native Interface


● It is a framework.
● It provides an interface to communicate with another application written in another
language like C, C++, Assembly etc.

Structure of Java Program


Documentation Section
● It is an optional for a Java program.
● It includes basic information about a Java program.
● The information includes the author's name, date of creation, version, program name,
company name, and description of the program.

Package Declaration
● The package declaration is optional.
● Declare the package name in which the class is placed.
● There can be only one package statement in a Java program.

(Eg) package javatpoint; //where javatpoint is the package name

Import Statements
● It contains many predefined classes and interfaces.
● If we want to use any class of a particular package, we need to import that class.
● The import statement represents the class stored in the other package.

(Eg)import java.util.*; //it imports all the class of the java.util package

Interface Section
● It is an optional section.
● An interface is slightly different from the class.
● It contains only constants and method declarations

interface car
{
void start();
void stop();
}

Class Definition
● It contains information about user-defined methods, variables, and constants.
● Every Java program has at least one class that contains the main() method.

class Student //class definition

{
}

Class Variables and Constants


● The variables and constants are defined just after the class definition.
● The variables and constants store values of the parameters.
class Student //class definition
{
String sname; //variable
int id;
double percentage;
}

Main Method Class


● The execution of all Java programs starts from the main() method.
● It must be inside the class.

public static void main(String args[])

{
}

Methods and behavior


● The methods are the set of instructions that we want to perform.
● These instructions execute at runtime and perform the specified task.

Program

class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}

Java main() method


A Java program is executed, the Java Virtual Machine (JVM) looks for the main() method to
begin execution.
Program

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:

Hello, World!

● Public
○ It is an access specifier.
○ A public keyword used before the main() method, so that JVM can identify the
execution point of the program.
● Static
○ It invokes without creating the objects, so do not need any object to call the
main() method.
● Void
○ keyword acknowledges the compiler that the main() method does not return any
value.
● Main ()
○ It is called by JVM to execute a program line by line and end the execution after
completion of this method.
● String args[]:
○ The main() method accepts some data from the user. It accepts a group of strings,
which is called a string array.

Java Console Output(System.out)

● Java System.out.println() is used to print an argument that is passed to it.

Parts of System.out.println()

1. System - It is a final class defined in the java.lang package.


2. Out - This is an instance of PrintStream type, it is a public and static member field of the
System class.
3. println() - The PrintStream class has a public method println(). It prints any argument
passed to it and adds a new line to the output.
Java Console Input

● There are different ways to read input from the user in the command line
environment(console).

● Using Buffered Reader Class


a. This method is used by wrapping the System.in (standard input stream) in an
InputStreamReader which is wrapped in a BufferedReader.
b. Example
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
String name = reader.readLine();
● Using Scanner Class
○ it is also used to read input from the user in the command line.
○ Example
Scanner in = new Scanner(System.in);
String s = in.nextLine();

Static Data
● Declare any variable as static, it is known as a static variable.
● It is used to refer to the common property of all objects for example, the company name
of employees,
● It gets memory only once in the class area at the time of class loading.
● It is used to initialize the default values if not explicitly initialized by the programmer.
● It is shared among all instances of the class
● eg
static String college ="ITS";//static variable

Static Method
● A static keyword with any method, it is known as a static method.
● It belongs to the class rather than the object of a class.
● It can be invoked without the need for creating an instance of a class.
● It can access static data members and can change their value of it.

Program
class Student{
int rollno;
String name;
static String college = "ITS";
static void change(){
college = "BBDIT";
}
Student(int r, String n){
rollno = r;
name = n;
}
void display(){System.out.println(rollno+" "+name+" "+college);}
}
public class TestStaticMethod{
public static void main(String args[]){
Student.change();
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
}
}

Output:
111 Karan BBDIT
222 Aryan BBDIT

String Class
● It is a sequence of characters.
● Objects of the String class are immutable it means they cannot be changed once created.

Program
import java.io.*;
class GFG {
public static void main (String[] args) {
String s="Geeks for Geeks String in Java";
System.out.println(s);
}
}
Output
Geeks for Geeks String in Java

Creating a String
● There are two ways to create string in Java:
○ 1. Using String literal
String s = “GeeksforGeeks”;
● Using new keyword
String s = new String (“GeeksforGeeks”);

String Constructors
String(char[] char_arr) - Allocates a new String from the given Character array.

String Methods
1. int length() - Returns the number of characters in the String.
2. Char charAt(int i) - Returns the character at ith index.
3. String substring (int i) - Return the substring from the ith index character to end.
4. String concat( String str) - Concatenates specified string to the end of this string.
5. String replace (char oldChar, char newChar) - Returns new string by replacing all occurrences
of oldChar with newChar.

String Buffer Class

You might also like