Corejava Interview Booster 11
Corejava Interview Booster 11
======================================
`java.util.Date` and `java.sql.Date` are both classes in Java that represent dates,
but they serve different purposes and have different characteristics. Here’s a
brief comparison along with examples.
### 1. **Purpose**
- **`java.util.Date`**: Represents a specific instant in time, with millisecond
precision. It can be used in general-purpose applications.
- **`java.sql.Date`**: A subclass of `java.util.Date`, specifically designed for
working with SQL `DATE` values. It represents only the date (year, month, day)
without time information (hours, minutes, seconds).
### Examples
```java
import java.util.Date;
```java
import java.sql.Date;
```java
import java.util.Date;
import java.sql.Date as SqlDate;
### Summary
- Use `java.util.Date` for general date-time operations.
- Use `java.sql.Date` when working specifically with SQL databases to handle dates
without time.
================================
package com.InterviewPrep;
```java
String [][] x = {{"A","B"},{"C","D"}};
```
- A 2D array `x` is defined with two rows:
- Row 0: `{"A", "B"}`
- Row 1: `{"C", "D"}`
1. **Outer Loop**:
```java
for (int i = x.length - 1; i >= 0; i--)
```
- Starts from the last row (`i = 1` for `{"C", "D"}`) and goes to the first row
(`i = 0` for `{"A", "B"}`).
2. **Inner Loop**:
```java
for(int j = 0; j < x[i].length; j++)
```
- Iterates through each column of the current row.
3. **Printing**:
```java
System.out.print(x[i][j]);
```
- Prints each element without a newline.
### Summary
The code effectively prints the elements of a 2D array in reverse order, resulting
in the output "CDBA".
==================================
package com.InterviewPrep;
```java
String [][] s = {{"A","B"},{"C","D"}};
```
- A 2D array `s` is defined with two rows:
- Row 0: `{"A", "B"}`
- Row 1: `{"C", "D"}`
### Loop Explanation
1. **Outer Loop**:
```java
for(int i = 0; i < s.length; i++)
```
- Iterates through each row of the array, starting from the first row (`i = 0`
for `{"A", "B"}`) to the last row (`i = 1` for `{"C", "D"}`).
2. **Inner Loop**:
```java
for(int j = s[i].length - 1; j >= 0; j--)
```
- For each row, it starts from the last column (`j = 1` for the first row) and
goes to the first column (`j = 0`).
3. **Printing**:
```java
System.out.print(s[i][j]);
```
- Prints each element without a newline.
### Summary
This code prints the elements of a 2D array in reverse order for each row,
resulting in the output "BACD".
===================
Yes, when you create an object of a class in Java, the `toString()` method is
implicitly called when you try to print that object or when the object is
concatenated with a string.
### Example
```java
class MyClass {
@Override
public String toString() {
return "This is MyClass object";
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
System.out.println(obj); // This will call obj.toString() internally
}
}
```
### Output
```
This is MyClass object
```
### Explanation
- When you use `System.out.println(obj)`, the `println` method calls
`obj.toString()` internally to get the string representation of the object.
- If you don't override the `toString()` method in your class, the default
implementation from the `Object` class is called, which returns a string containing
the class name and the object's hash code.