Day-7 - Java Interview Questions & URL Shortener
Introduction
In Day 7, I learned Java Interview Questions Like Difference Between Final, Finally and Finalize and Method Overloading vs Method Overriding and Comparable vs Comparator and Also Implement URL Shortener. The Session Is Live in Youtube Channel By Navin Reddy Sir.
Java Interview Questions
Final, Finally And Finalize
Final : Final variable can't change value, final method cannot override and Final class cannot be extended.
Finally: Finally is Block in java, finally block is always executed it is use to close connection when we use database connectivity.
Finalize: Finalize is a method call by garbage collector. It is used to release all the resources used by the object .
Method Overloading And Method Overriding
Method Overloading: In a Same Class We have Methods With Same Name And Same Parameters. Method Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters, or a mixture of both.
Method Overriding: If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.
Recommended by LinkedIn
Comparator and Comparator
URL Shortener
Complete Code Link: Link
package com.urlshort.urlshortspringproject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
@Controller
public class URLShortController {
private Map<String, String> urlMap = new HashMap<>();
private Random random = new Random();
@PostMapping("/generate")
@ResponseBody
public String generateShortURL(@RequestParam("longURL") String longURL) {
// Check if the original URL is already present in the map
for (Map.Entry<String, String> entry : urlMap.entrySet()) {
if (entry.getValue().equals(longURL)) {
return entry.getKey();
}
}
// Generate a new short URL
String shortURL = generateShortURLFromOriginalURL(longURL);
urlMap.put(shortURL, longURL);
return shortURL;
}
private String generateShortURLFromOriginalURL(String longURL) {
String characters = extractCharacters(longURL);
int length = 6;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
int randomIndex = random.nextInt(characters.length());
char randomChar = characters.charAt(randomIndex);
sb.append(randomChar);
}
return "telus.ko/" + sb.toString();
}
private String extractCharacters(String longURL) {
// Remove "https://" from the beginning of the URL
String withoutHttps = longURL.replaceFirst("^https://", "");
// Remove "www" from the beginning of the URL if it exists
String withoutWWW = withoutHttps.replaceFirst("^www.", "");
// Remove any remaining special characters
String characters = withoutWWW.replaceAll("[^a-zA-Z0-9]", "");
if (characters.isEmpty()) {
throw new IllegalArgumentException("Invalid URL");
}
return characters;
}
}
There is a index.html in resource->template folder which is handle front end.
Thanks To Navin Reddy Sir To learn me this new concepts. In this article I tried my best to explain. Thanks!