0% found this document useful (0 votes)
9 views

Corejava Interview Booster 11

Uploaded by

tellapuri.naresh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Corejava Interview Booster 11

Uploaded by

tellapuri.naresh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Date : 04/11/2024 (Interview Booster)

======================================
`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).

### 2. **Time Information**


- **`java.util.Date`**: Contains both date and time information.
- **`java.sql.Date`**: Contains only the date part. The time part is set to
00:00:00.

### 3. **Usage in Databases**


- **`java.util.Date`**: Not suitable for JDBC operations where you need to handle
SQL dates.
- **`java.sql.Date`**: Specifically used for SQL operations, especially when you
are working with `DATE` columns in databases.

### Examples

#### Example 1: Using `java.util.Date`

```java
import java.util.Date;

public class UtilDateExample {


public static void main(String[] args) {
Date utilDate = new Date();
System.out.println("Util Date: " + utilDate);

// Output includes time


}
}
```

#### Example 2: Using `java.sql.Date`

```java
import java.sql.Date;

public class SqlDateExample {


public static void main(String[] args) {
// Get the current date
long currentTimeMillis = System.currentTimeMillis();
Date sqlDate = new Date(currentTimeMillis);

System.out.println("SQL Date: " + sqlDate);

// Output shows only date part


}
}
```
### Key Points
- When you print `java.util.Date`, it includes both date and time.
- When you print `java.sql.Date`, the time is set to 00:00:00.

### Conversion Between the Two


You often need to convert between `java.util.Date` and `java.sql.Date` when working
with databases:

```java
import java.util.Date;
import java.sql.Date as SqlDate;

public class DateConversionExample {


public static void main(String[] args) {
// Convert java.util.Date to java.sql.Date
Date utilDate = new Date();
SqlDate sqlDate = new SqlDate(utilDate.getTime());
System.out.println("Converted SQL Date: " + sqlDate);

// Convert java.sql.Date to java.util.Date


Date convertedBack = new Date(sqlDate.getTime());
System.out.println("Converted back to Util Date: " + convertedBack);
}
}
```

### 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;

public class SwappingArrayElements {


public static void main(String[] args) {
String [][] x = {{"A","B"},{"C","D"}};
for (int i=x.length-1;i>=0;i--){
for(int j=0;j<x[i].length;j++){
System.out.print(x[i][j]);
}
}
}
}
============
Sure! Here’s a concise explanation of the code and its output:

### Code Overview

```java
String [][] x = {{"A","B"},{"C","D"}};
```
- A 2D array `x` is defined with two rows:
- Row 0: `{"A", "B"}`
- Row 1: `{"C", "D"}`

### Loop Explanation

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.

### Output Breakdown

- **First Iteration (`i = 1`)**:


- Prints "C" and "D" → Output: `CD`

- **Second Iteration (`i = 0`)**:


- Prints "A" and "B" → Final Output: `CDBA`

### Final Output


The program prints:
```
CDBA
```

### Summary
The code effectively prints the elements of a 2D array in reverse order, resulting
in the output "CDBA".
==================================
package com.InterviewPrep;

public class SwappingArrayindexvalues {


public static void main(String[] args) {
String [][] s = {{"A","B"},{"C","D"}};
for(int i=0; i<s.length;i++){
for(int j=s[i].length-1;j>=0;j--){
System.out.print(s[i][j]);
}
}
}
}
================
Sure! Let’s break down the code and its output step by step.

### Code Overview

```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.

### Output Breakdown

- **First Iteration (`i = 0`)** (Row: `{"A", "B"}`):


- Starts with `j = 1`: Prints "B"
- Next, `j = 0`: Prints "A"
- Output so far: `BA`

- **Second Iteration (`i = 1`)** (Row: `{"C", "D"}`):


- Starts with `j = 1`: Prints "D"
- Next, `j = 0`: Prints "C"
- Final Output: `BACD`

### Final Output


The program prints:
```
BACD
```

### 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

Here's a simple example to illustrate this:

```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.

So, yes, `toString()` is called internally when printing an object or using it in


string contexts.
===============================

You might also like