Java Spring Boot Interview Questions
Question: How do you create a class in Java?
Answer:
In Java, a class is a blueprint for creating objects (instances). A class contains fields (variables) and methods (functions) that define the properties and behavior of the objects created from it.
Steps to Create a Class in Java:
- Define the Class: A class is defined using the
class
keyword. - Define Instance Variables (Fields): These variables represent the properties of the class.
- Define Methods: Methods define the actions or behavior that the class objects can perform.
- Constructor: A constructor is a special method used to initialize objects when they are created.
Syntax:
public class ClassName {
// Instance variables (fields)
private int field1;
private String field2;
// Constructor
public ClassName(int field1, String field2) {
this.field1 = field1;
this.field2 = field2;
}
// Methods
public void display() {
System.out.println("Field1: " + field1);
System.out.println("Field2: " + field2);
}
// Getter and Setter Methods
public int getField1() {
return field1;
}
public void setField1(int field1) {
this.field1 = field1;
}
}
Explanation:
-
Class Declaration:
- The class is defined using the
class
keyword followed by the class name (e.g.,ClassName
). - By convention, class names in Java start with an uppercase letter.
- The class is defined using the
-
Instance Variables (Fields):
- Inside the class, we declare variables to represent the properties of the objects.
- In the example above,
field1
is an integer andfield2
is a string.
-
Constructor:
- The constructor is a special method that is called when an object of the class is created.
- It has the same name as the class and does not have a return type.
- In the example, the constructor initializes the
field1
andfield2
variables.
-
Methods:
- Methods define the behavior of the class.
- In the example, the
display()
method prints the values offield1
andfield2
. - Getter and setter methods (
getField1()
andsetField1()
) are also provided to access and modify the private variables.
Example of Creating and Using a Class:
public class Car {
// Instance variables
private String model;
private int year;
// Constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}
// Method to display details of the car
public void displayDetails() {
System.out.println("Car Model: " + model);
System.out.println("Car Year: " + year);
}
// Getter and Setter methods
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car("Toyota Camry", 2020);
// Calling the displayDetails method
myCar.displayDetails();
}
}
Explanation of Example:
- Class Definition: The
Car
class has two fields (model
andyear
), a constructor to initialize these fields, and a method (displayDetails
) to print them. - Creating an Object: In the
main()
method, an object of theCar
class is created using thenew
keyword.Car myCar = new Car("Toyota Camry", 2020);
- Calling Methods: After creating the object, we call the
displayDetails()
method to print the details of the car.
Example Output:
Car Model: Toyota Camry
Car Year: 2020
Key Points:
- A class in Java is a template for creating objects.
- Use the
class
keyword followed by the class name. - Instance variables store the properties of the object.
- Constructor initializes the object’s properties when it is created.
- Methods define the behavior of the object.
Question: How to swap two numbers without using a third variable in Java?
Answer:
In Java, you can swap two numbers without using a third (temporary) variable by using mathematical operations or bitwise XOR. Below are two common methods to swap two numbers without an extra variable:
Method 1: Using Arithmetic Operations (Addition and Subtraction)
You can swap two numbers by leveraging addition and subtraction. Here’s how:
- Add the two numbers and store the result in one of the variables.
- Subtract the second number from the result to get the first number.
- Subtract the first number (which now contains the sum of both numbers) from the result to get the second number.
Example:
public class SwapNumbers {
public static void main(String[] args) {
int a = 5;
int b = 10;
// Swapping without using a third variable
a = a + b; // a becomes 15
b = a - b; // b becomes 5
a = a - b; // a becomes 10
// Output swapped values
System.out.println("a: " + a); // 10
System.out.println("b: " + b); // 5
}
}
Output:
a: 10
b: 5
Explanation:
- Initially,
a = 5
andb = 10
. - After
a = a + b
,a
becomes15
(5 + 10
). - Then,
b = a - b
results inb = 5
(15 - 10
). - Finally,
a = a - b
results ina = 10
(15 - 5
).
Method 2: Using Bitwise XOR
Bitwise XOR can also be used to swap two numbers without a third variable. This method is faster than arithmetic operations because it works directly at the binary level.
The steps are as follows:
- XOR the two numbers and store the result in one of the variables.
- XOR the result with the second number to get the original first number.
- XOR the result with the first number to get the original second number.
Example:
public class SwapNumbers {
public static void main(String[] args) {
int a = 5;
int b = 10;
// Swapping using XOR
a = a ^ b; // a becomes 15 (binary: 0101 ^ 1010 = 1111)
b = a ^ b; // b becomes 5 (binary: 1111 ^ 1010 = 0101)
a = a ^ b; // a becomes 10 (binary: 1111 ^ 0101 = 1010)
// Output swapped values
System.out.println("a: " + a); // 10
System.out.println("b: " + b); // 5
}
}
Output:
a: 10
b: 5
Explanation:
- Initially,
a = 5
(binary0101
) andb = 10
(binary1010
). - After
a = a ^ b
,a
becomes1111
(binary XOR of0101
and1010
). - Then,
b = a ^ b
results inb = 5
(binary XOR of1111
and1010
). - Finally,
a = a ^ b
results ina = 10
(binary XOR of1111
and0101
).
Key Points:
- Arithmetic method: It uses addition and subtraction to swap values but may cause overflow if the values are too large (though this is rare with small numbers).
- Bitwise XOR method: It works efficiently at the binary level and doesn’t have the risk of overflow. It is considered a more elegant solution.
Both methods allow you to swap two numbers without using a third variable, and both are commonly used in coding challenges and interview questions.
Read More
If you can’t get enough from this article, Aihirely has plenty more related information, such as Java interview questions, Java interview experiences, and details about various Java job positions. Click here to check it out.
Tags
- Java
- Java interview questions
- ArrayList vs LinkedList
- Inheritance in Java
- Method overloading
- Method overriding
- String vs StringBuilder vs StringBuffer
- Multithreading in Java
- Thread creation in Java
- Final keyword in Java
- JSON parsing in Java
- Random number generation in Java
- Comparing strings in Java
- String reversal in Java
- Calculation using switch case
- Remove duplicates in ArrayList
- Exception handling in Java
- Two dimensional array in Java
- Interface vs abstract class in Java
- Java programming basics
- Java collections
- Java performance optimization
- Java data structures