1. What is a string? explain the various string functions in java
A string in Java is a sequence of characters, commonly used to store text. The String class in Java is a predefined class that comes with a variety of methods for handling and manipulating string data. Strings in Java are immutable, meaning once a string is created, its value cannot be changed.
Here’s an overview of some common string functions in Java:
length()
- Description: Returns the number of characters in the string.
- Example:
String str = "Hello"; int len = str.length(); // len = 5
charAt(int index)
- Description: Returns the character at the specified index.
- Example:
String str = "Hello"; char ch = str.charAt(1); // ch = 'e'
substring(int beginIndex, int endIndex)
- Description: Returns a new string that is a substring of the original string, from
beginIndextoendIndex - 1. - Example:
String str = "Hello"; String sub = str.substring(1, 4); // sub = "ell"
contains(CharSequence s)
- Description: Checks if the string contains the specified sequence of characters.
- Example:
String str = "Hello"; boolean result = str.contains("ell"); // result = true
equals(Object another)
- Description: Compares the string to the specified object. Returns
trueif both are equal. - Example:
String str1 = "Hello"; String str2 = "Hello"; boolean result = str1.equals(str2); // result = true
equalsIgnoreCase(String another)
- Description: Compares the string to another string, ignoring case considerations.
- Example:
String str1 = "hello"; String str2 = "HELLO"; boolean result = str1.equalsIgnoreCase(str2); // result = true
toUpperCase()
- Description: Converts all characters in the string to uppercase.
- Example:
String str = "Hello"; String upper = str.toUpperCase(); // upper = "HELLO"
toLowerCase()
- Description: Converts all characters in the string to lowercase.
- Example:
String str = "Hello"; String lower = str.toLowerCase(); // lower = "hello"
trim()
- Description: Removes leading and trailing whitespace from the string.
- Example:
String str = " Hello "; String trimmed = str.trim(); // trimmed = "Hello"
replace(CharSequence target, CharSequence replacement)
- Description: Replaces each substring of the string that matches the specified target sequence with the replacement sequence.
- Example:
String str = "Hello"; String replaced = str.replace("l", "p"); // replaced = "Heppo"
split(String regex)
- Description: Splits the string around matches of the given regular expression.
- Example:
String str = "Hello,World"; String[] parts = str.split(","); // parts = ["Hello", "World"]
indexOf(String str)
- Description: Returns the index within the string of the first occurrence of the specified substring.
- Example:
String str = "Hello"; int index = str.indexOf("l"); // index = 2
lastIndexOf(String str)
- Description: Returns the index within the string of the last occurrence of the specified substring.
- Example:
String str = "Hello"; int index = str.lastIndexOf("l"); // index = 3
startsWith(String prefix)
- Description: Checks if the string starts with the specified prefix.
- Example:
String str = "Hello"; boolean result = str.startsWith("He"); // result = true
endsWith(String suffix)
- Description: Checks if the string ends with the specified suffix.
- Example:
String str = "Hello"; boolean result = str.endsWith("lo"); // result = true
concat(String str)
- Description: Concatenates the specified string to the end of this string.
- Example:
String str1 = "Hello"; String str2 = "World"; String result = str1.concat(str2); // result = "HelloWorld"
valueOf(Object obj)
- Description: Returns the string representation of the given argument (often used for converting different data types to a string).
- Example:
int num = 10; String str = String.valueOf(num); // str = "10"
compareTo(String anotherString)
- Description: Compares two strings lexicographically.
- Example:
String str1 = "Hello"; String str2 = "World"; int result = str1.compareTo(str2); // result < 0 (because "H" comes before "W")
These methods provide a robust set of tools for manipulating and analyzing string data in Java, making it a powerful language for text processing.
2. explain the concept of pass by value and pass by reference with examples
The concepts of “pass by value” and “pass by reference” are important in programming, particularly when discussing how arguments are passed to functions or methods. These terms describe the way data is passed to a function or method:
- Pass by Value: A copy of the actual value is passed to the function. Modifications made to the parameter inside the function do not affect the original variable.
- Pass by Reference: A reference (or address) of the actual variable is passed. Modifications made to the parameter inside the function do affect the original variable.
Pass by Value
In “pass by value,” the function receives a copy of the variable’s value. If the function modifies this value, it only changes the local copy inside the function, leaving the original variable unchanged.
Example in C (Pass by Value):
#include <stdio.h>
void modifyValue(int x) {
x = 20; // Changes only the local copy of x
}
int main() {
int a = 10;
modifyValue(a);
printf("Value of a: %d\n", a); // Output: 10
return 0;
}Explanation:
ais passed to themodifyValuefunction as a copy. Changingxinside the function does not changeain themainfunction.
Pass by Reference
In “pass by reference,” the function receives the memory address (reference) of the variable. Changes made to the parameter inside the function will affect the original variable because the function operates on the actual data in memory.
Example in C++ (Pass by Reference):
#include <iostream>
using namespace std;
void modifyValue(int &x) {
x = 20; // Modifies the original variable
}
int main() {
int a = 10;
modifyValue(a);
cout << "Value of a: " << a << endl; // Output: 20
return 0;
}Explanation:
ais passed to themodifyValuefunction by reference. Changingxinside the function directly changesain themainfunction.
Pass by Value in Java
Java uses only pass by value. However, how this works depends on whether you’re passing a primitive type or an object.
Example with Primitives (Pass by Value):
public class Main {
public static void main(String[] args) {
int num = 10;
modifyValue(num);
System.out.println("Value of num: " + num); // Output: 10
}
public static void modifyValue(int x) {
x = 20; // Changes only the local copy of x
}
}Explanation:
- The integer
numis passed to themodifyValuemethod as a copy. Modifyingxdoes not affectnumin themainmethod.
Example with Objects (Pass by Value of Reference):
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass(10);
modifyValue(obj);
System.out.println("Value of obj: " + obj.value); // Output: 20
}
public static void modifyValue(MyClass o) {
o.value = 20; // Modifies the original object's value
}
}
class MyClass {
int value;
MyClass(int value) {
this.value = value;
}
}Explanation:
- The reference to the
MyClassobject is passed by value, meaning a copy of the reference (not the object itself) is passed. Since both references point to the same object in memory, modifying the object’s value inside the method changes the original object’s value.
Important Notes on Java:
- Java is always “pass by value”: Even for objects, Java passes the reference by value. This means the method gets a copy of the reference, but both the original and copied references point to the same object.
- Modifying the object: Changes made to the object using the reference inside the method affect the original object.
- Reassigning the reference: Reassigning the reference inside the method does not affect the original reference outside the method.
Conclusion
- Pass by Value: The function/method receives a copy of the value (primitives in Java).
- Pass by Reference: The function/method receives a reference (memory address) to the variable (not applicable in Java).
- Java’s behavior: Java always passes by value, but with objects, this means passing a copy of the reference to the object, not the actual object.
3. what is inheritance? explain the types of inheritance.
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class to inherit properties and behaviors (methods) from an existing class. The existing class is called the parent class, superclass, or base class, and the new class is called the child class, subclass, or derived class.
Purpose of Inheritance
- Code Reusability: Inheritance allows for code reuse. Instead of writing the same code multiple times, you can write it once in a superclass and reuse it in subclasses.
- Polymorphism: Inheritance facilitates polymorphism, where a subclass can override or extend the behavior of the superclass.
- Extensibility: Inheritance makes it easy to extend or enhance existing code without modifying it directly.
Types of Inheritance
-
Single Inheritance
-
Multiple Inheritance
-
Multilevel Inheritance
-
Hierarchical Inheritance
-
Hybrid Inheritance
-
Single Inheritance
In single inheritance, a class inherits from only one superclass.
Example:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal
dog.bark(); // Method in Dog
}
}Explanation:
- The
Dogclass inherits from theAnimalclass, meaningDogcan use theeatmethod defined inAnimal.
- Multiple Inheritance
Multiple inheritance refers to a scenario where a class can inherit from more than one superclass. However, Java does not support multiple inheritance with classes to avoid complexity and ambiguity, such as the diamond problem.
Instead, Java allows multiple inheritance through interfaces.
Example Using Interfaces:
interface CanRun {
void run();
}
interface CanBark {
void bark();
}
class Dog implements CanRun, CanBark {
public void run() {
System.out.println("Dog is running");
}
public void bark() {
System.out.println("Dog is barking");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.run();
dog.bark();
}
}Explanation:
- The
Dogclass implements two interfaces,CanRunandCanBark, thereby inheriting their behaviors.
- Multilevel Inheritance
In multilevel inheritance, a class is derived from a class that is already derived from another class. This creates a chain of inheritance.
Example:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy is weeping");
}
}
public class Main {
public static void main(String[] args) {
Puppy puppy = new Puppy();
puppy.eat(); // Inherited from Animal
puppy.bark(); // Inherited from Dog
puppy.weep(); // Method in Puppy
}
}Explanation:
- The
Puppyclass inherits fromDog, which in turn inherits fromAnimal. Therefore,Puppyinherits methods from bothDogandAnimal.
- Hierarchical Inheritance
In hierarchical inheritance, multiple classes inherit from a single superclass.
Example:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
dog.eat(); // Inherited from Animal
dog.bark(); // Method in Dog
cat.eat(); // Inherited from Animal
cat.meow(); // Method in Cat
}
}Explanation:
- Both
DogandCatclasses inherit from theAnimalclass, allowing them to use theeatmethod defined inAnimal.
- Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance. It can be a mix of multilevel, hierarchical, and multiple inheritance. However, since Java does not support multiple inheritance through classes, hybrid inheritance is achieved using interfaces.
Example Using Interfaces:
interface CanRun {
void run();
}
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal implements CanRun {
public void run() {
System.out.println("Dog is running");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
dog.eat(); // Inherited from Animal
dog.run(); // Implemented from CanRun
dog.bark(); // Method in Dog
cat.eat(); // Inherited from Animal
cat.meow(); // Method in Cat
}
}Explanation:
- The
Dogclass inherits fromAnimaland implements theCanRuninterface, making it an example of hybrid inheritance.
Summary
- Single Inheritance: One class inherits from one superclass.
- Multiple Inheritance: One class inherits from multiple superclasses (supported in Java through interfaces only).
- Multilevel Inheritance: A chain of inheritance where a class is derived from another derived class.
- Hierarchical Inheritance: Multiple classes inherit from a single superclass.
- Hybrid Inheritance: A combination of two or more types of inheritance, often implemented using interfaces in Java.
4. write short notes on i) wrapper classes ii)access modifiers iii) Array of objects
i) Wrapper Classes
Wrapper classes in Java are used to convert primitive data types into objects. Java is an object-oriented language, and sometimes it’s necessary to work with objects instead of primitives. Wrapper classes provide a way to use primitive types (like int, char, boolean, etc.) as objects.
Key Points:
-
Primitive Types and Wrapper Classes:
int→Integerchar→Characterboolean→Booleandouble→Double- And so on for other primitive types.
-
Autoboxing and Unboxing:
- Autoboxing: The automatic conversion of a primitive type to its corresponding wrapper class. For example, converting
inttoInteger. - Unboxing: The automatic conversion of a wrapper class to its corresponding primitive type. For example, converting
Integertoint.
- Autoboxing: The automatic conversion of a primitive type to its corresponding wrapper class. For example, converting
Example:
public class Main {
public static void main(String[] args) {
int num = 5;
Integer wrappedNum = num; // Autoboxing
int unwrappedNum = wrappedNum; // Unboxing
System.out.println(wrappedNum); // Outputs: 5
}
}Wrapper classes are useful when working with collections (like ArrayList, HashMap) that can only store objects, not primitive types.
ii) Access Modifiers
Access modifiers in Java are keywords that set the accessibility (visibility) of classes, methods, and other members. They define the scope of access and help in encapsulation by restricting access to the internal details of classes.
Types of Access Modifiers:
-
private: The member is accessible only within the same class. It is the most restrictive access level.- Example:
private int age;
- Example:
-
default(no modifier): The member is accessible only within the same package. If no access modifier is specified, it is considered “package-private.”- Example:
int age;(no modifier)
- Example:
-
protected: The member is accessible within the same package and by subclasses (even if they are in different packages).- Example:
protected int age;
- Example:
-
public: The member is accessible from any other class, regardless of the package.- Example:
public int age;
- Example:
Example:
class Person {
private String name; // Private: only accessible within the Person class
protected int age; // Protected: accessible within the same package and subclasses
public String gender; // Public: accessible from anywhere
// Getter for private variable
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person();
p.gender = "Male"; // Allowed because gender is public
// p.name = "John"; // Not allowed because name is private
}
}iii) Array of Objects
An array of objects in Java is an array that stores references to objects. It is similar to arrays of primitive data types but can hold multiple objects of a particular class.
Key Points:
- Declaration: Just like an array of primitive types, you can declare an array of objects. The elements of the array are initialized to
nullby default. - Initialization: You can initialize the array by creating instances of the class and assigning them to the array elements.
Example:
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
// Declare and create an array of objects
Student[] students = new Student[3];
// Initialize the array with Student objects
students[0] = new Student("Alice", 20);
students[1] = new Student("Bob", 22);
students[2] = new Student("Charlie", 23);
// Accessing the array elements
for (Student student : students) {
student.display(); // Output each student's details
}
}
}Explanation:
- An array of
Studentobjects is created with 3 elements. - Each element is initialized with a
Studentobject. - The
displaymethod is called on each object to print its details.
Usage: Arrays of objects are commonly used in scenarios where you need to manage multiple objects, like storing a list of students, employees, or any other entities.