Unit - I
Unit - I
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.
Execution Engine
● It contains a virtual processor, A Interpreter and Just-In-Time(JIT) compiler.
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.
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.
{
}
{
}
Program
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
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.
Parts of System.out.println()
● There are different ways to read input from the user in the command line
environment(console).
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.