javarevisited

javarevisited


How to check if a number is a palindrome or not in Java - Example

Posted: 26 Dec 2012 09:05 PM PST

How to check if a number is a palindrome or not is a variant of  popular String interview question how to check if a String is a palindrome or not. A number is said to be a palindrome if number itself is equal to reverse of number e.g. 313 is a palindrome because reverse of this number is also 313. On the other hand 123 is not a palindrome because reverse of 123 is 321 which is not equal to 123, i.e. original number. In order to check if a number is a palindrome or not we can reuse the logic of How to reverse number in Java. Since in most of interview, you are supposed to solve this question without taking help from API i.e. only using basic programming construct e.g. loop, conditional statement, variables, operators and logic. I have also seen programmer solving this question by first converting integer to String and than reversing String using reverse() method of StringBuffer and than converting String back to Integer, which is not a correct way because you are using Java API. Some programmer may think that this is just a trivial programming exercise but it's not. Questions like this or Fibonacci series using recursion can easily separate programmers who can code and who can't. So it's always in best interest to keep doing programing exercise and developing logic.
Read more »

Inner class and nested Static Class in Java with Example

Posted: 26 Dec 2012 07:37 PM PST

Inner class and nested static class in Java both are classes declared inside another class, known as top level class in Java. In Java terminology, If you declare a nested class static, it will called nested static class in Java while non static nested class are simply referred as Inner Class. Inner classes are also a popular topic in Java interviews. One of the popular question is difference between inner class and nested static class , some time also refereed as difference between static and non static nested class in Java. One of the most important question related to nested classes are Where should you use nested class in Java? This is one of the tricky Java question, If you have read Effective Java from Joshua Bloch than in one chapter he has suggested that if a class can only be useful to one particular class, it make sense to keep that inside the class itself  otherwise declare it as top level class. Nested class improves Encapsulation and help in maintenance. I actually look JDK itself for examples and if you look HashMap class, you will find that Map.Entry is a good example of nested class in Java. Another popular use of nested classes are implementing Comparator in Java e.g. AgeCompartor for Person class. Since some time Comparator is also tied with a particular class, it make sense to declare them as nested static class in Java.  In this Java tutorial we will see what is Inner class in Java, different types of Inner classes, what is static nested class and finally difference between static nested class and inner class in Java.
Read more »

SQL query to add, modify and drop column in table with default value NOT NULL constraint – MySQL database

Posted: 26 Dec 2012 06:24 AM PST

How to add column in existing table with default value is another popular SQL interview question asked for Junior level programming job interviews. Though syntax of SQL query to add column with default value varies little bit from database to database, it always been performed using ALTER keyword of ANSI SQL. Adding column in existing table in MySQL database is rather easy and straight forward and we will see example of SQL query for MySQL database which adds a column with default value. You can also provide constraints like NULL or NOT NULL while adding new column in table. In this SQL tutorial  we are adding third column in a table called Contacts which contains name and phone of contacts. Now we want to add another column email with default value "abc@yahoo.com". We will use ALTER command in SQL to do that. By the way this is next in our SQL tutorials e.g. How to join three tables in SQL and SQL query to find duplicate records in table. If you haven't read them yet, then you may find them useful.
Read more »

What is Type Casting in Java - Casting one Class to other class or interface Example

Posted: 26 Dec 2012 12:34 AM PST

Type casting in Java is to cast one type, a class or interface, into another type i.e. another class or interface. Since Java is an Object oriented programming language and supports both Inheritance and Polymorphism, It's easy that Super class reference variable is pointing to Sub Class object but catch here is that there is no way for Java compiler to know that a Super class variable is pointing to Sub Class object. Which means you can not call method which is declared on sub class. In order to do that, you first need to cast the Object back into its original type. This is called type-casting in Java. This will be more clear when we see an example of type casting in next section. Type casting comes with risk of ClassCastException in Java, which is quite common with method which accept Object type and later type cast into more specific type. we will see when ClassCastException comes during type casting and How to avoid it in coming section of  this article. Another worth noting point here is that from Java 5 onwards you can use Generics to write type-safe code to reduce amount of type casting in Java which also reduces risk of java.lang.ClassCastException at runtime.
Read more »


1 comment:

  1. Awesome blog with best explanation on the concept. Thanks for sharing.

    Cheers,
    http://www.flowerbrackets.com/palindrome-number-in-java/

    ReplyDelete