Java 2marks
Java 2marks
o JVM (Java Virtual Machine): It is a runtime environment that converts Java bytecode
into machine code.
o JRE (Java Runtime Environment): It contains the JVM and libraries to run Java
programs.
o JDK (Java Development Kit): It includes the JRE, compiler, and development tools
required for writing Java programs.
o Syntax:
o class ClassName {
o // Variables
o // Methods
o }
o Example:
o class Car {
o String brand;
o void display() {
o }
o }
o Syntax:
o class ClassName {
o ClassName() {
o // Constructor body
o }
o }
o Example:
o class Car {
o String brand;
o Car() { // Constructor
o brand = "Toyota";
o }
o }
o Definition: Access specifiers control the visibility of classes, methods, and variables.
o Types:
o Definition: A static method belongs to the class and can be called without creating
an object.
o Syntax:
o class ClassName {
o }
o }
o Example:
o class MathUtils {
o return x * x;
o }
o }
o Syntax:
o /**
o * @return Description
o */
o Example:
o /**
o */
o class Car {
o /**
o */
o void display() {
o System.out.println("Car Brand");
o }
o }
7. What is a JavaBean? Mention its properties.
o Properties of JavaBeans:
Must provide getter and setter methods for accessing private variables.
o import java.io.Serializable;
o return brand;
o }
o this.brand = brand;
o }
o }
o Definition: Wrapper classes provide object representation for primitive data types.
o Syntax:
o Example:
o Example:
Definition: The switch statement allows selecting a block of code from multiple options.
Syntax:
switch(expression) {
case value1:
// Code block
break;
case value2:
// Code block
break;
default:
// Default block
}
Example:
int day = 2;
switch(day) {
}
11. What is the difference between break and continue statements?
Example:
System.out.println(i);
}
12. Explain the difference between primitive and reference data types.
Primitive data types: Directly store values (e.g., int, char, double).
Example:
Example:
System.out.println(i);
}
Definition: Instance control flow determines the execution order of instance variables and
blocks before the constructor.
Example:
class Test {
int x = 10;
{ // Instance block
System.out.println("Instance Block");
}
Test() {
System.out.println("Constructor");
}
}
}
Output:
Instance Block
Constructor
Definition: Regular expressions are patterns used for text matching and searching.
Example:
import java.util.regex.*;
Matcher m = p.matcher("123ABC");
System.out.println(m.find()); // true
}
}
Example:
class Example {
}