Posts

Showing posts from August, 2017

Write a program to check string is palindrome or not

Write a simple program to check given string is palindrome or not. This is very simple but it frequent asked in interview for freshers. public class StringPalindrome { public static void main(String[] args) { String input = "abcdcba"; boolean isPalindrome = isPalindrome(input); System.out.println(isPalindrome); } private static boolean isPalindrome(String input){ for (int i = 0; i < input.length()/2; i++) { if(input.charAt(i) != input.charAt(input.length()-i-1)){ return false; } } return true; } }

Implement a custom linked list with add and iteration functionality.

Write a program to implement custom linked list with minimum functionalities like add and iterations. You can not use in-build functionality of collection framework. package ztest; import java.util.Iterator; public class CustomLinkedList implements Iterable{ private int size = 0; private Node first; private Node last; private static class Node{ Object element; Node next; public Node(Object element) { this.element = element; } } public void add(Object element){ Node newNode = new Node(element); Node preLast = this.last; this.last = newNode; if(this.first == null){ this.first = newNode; }else{ preLast.next = newNode; } size++; } private class CustomListIterator implements Iterator{ int cursor; Node currentNode = first; @Override public boolean hasNext() { return cursor < size; } @Override public Object next() { Node tempNode = currentNode; currentNode = currentNode.next; cursor++; return tempNode.

Write a program to sort object using Comparator and Comparable

We will sort object of Student class. We will sort students by marks in ascending order. Comparable: import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Student implements Comparable { private Integer id; private String name; private int marks; public Student() { } public void setName(String name) { this.name = name; } public Student(Integer id, String name, int marks) { this.id = id; this.name = name; this.marks = marks; } @Override public int compareTo(Student stu) { if(this.marks < stu.marks){ return -1; }else if(this.marks > stu.marks){ return 1; }else{ return 0; } } public static void main(String[] args) { List students = new ArrayList<>(); students.add(new Student(1, "A", 67)); students.add(new Student(2, "B", 35)); students.add(new Student(3, "C", 80)); students.add(new Student(4, "D", 17)); System.out.println("E

Clone method of Object class in Java

Image
Let me ask you a question. How will you make copy of one object and assign to another reference variable? If you think, you can use assignment operator(=) then you are wrong. Assignment operator does not copy object to another object instead new reference variable points to same object in memory. Person obj1 = new Person(); Person obj2 = obj1; Here both variable obj1 and obj2 points to same object to memory. Two varibles referring to same object in memory. If you change something in obj1 that will be reflected in obj2 and vice versa Below image explains this - You can use clone method to make copy of object. Here is code snippet of clone method from object class. protected native Object clone() throws CloneNotSupportedException; clone is native:  The clone method is native in Object class. This method works at memory level and create copy of object. It means we don't need to create new object in code manually to make copy. Below way of writing clone method is wr

toString method of Object class in Java

This is very important method. I have seen in multiple code review where developers call toString method to print content of object. Actually you don't have to call this method explicitly. This method is automatically called when you print object of any class. What I meant for "content of object" is values of instance variable or any meaningful human readable information of an object.  But toString method of object class does not return human readable information about object. It returns name of the class and hexadecimal representation of hashcode.  This information is useless for human. This information does not say anything about an object.  Below is code snippet of toString method from Object class- public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } Let's see how toString method print information of an object by below example - public class Person { private Integer id; private String nam

Constructor In Java

Constructor is block of code which executed when object of class is created. Constructor is responsible for creating new object. It assigns initial values of object. Syntax of constructor is almost same as method. Constructor has below rule - Name of constructors must be same as class name. Constructors must not have return type. Constructor can not return a value. Types Of Constructor 1. Default Constructor: A constructor without parameters is called as default constructor. When we don't define any constructor in our class then compiler will add default constructor. It means a class will always has constructor either we explicitly define or compiler will add one. Example of default constructor- public class Person { public Person() { System.out.println("In Default Constructor of Person"); } public static void main(String[] args) { Person person = new Person(); } } Output: In Default Constructor of Person If we remove default cons