Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Saturday, December 3, 2011

Some Of Basic FAQ about Core JAVA


Hi Friends,

In this blog i want to share some of basic FAQ about Core JAVA that may help you have some basic information.

  1. Can multiple inheritance be achieved in Java. If yes, how?
    Answer – Yes, through implementing interfaces as we can implemente multile interfaces in a single class.
  2. What is the basic difference between String and StringBuffer object?
    Answer - String is an immutable object. StringBuffer is a mutable object.
  3. What is the base class for Error and Exception?
    Answer – Throwable is base class for Exception.
  4. What is a static block?
    Answer – this is the block executed before the class initialization.
  5. Can static methods call directly non static methods of the same class?
    Answer – No. They cannot directly call. If they need, they will have to create object.
  6. From an overridden method of the sub class, how can you call the overridden base class method ?
    Answer – call the method by using 'super' keyword.
  7. Can protected methods be accessed by classes in different package.
    Answer – Yes, provided they are subclasses. Otherwise no.
  8. Can a subclass reference hold a superclass object.
    Eg: Class A, Class B.
    If B extends A. Can we say B b = new A()
    Answer – No. Viceversa is true (if A a = new B() is fine).
  9. If a super class method is throwing an IOException, what should the overriding sub class method throw?
    Answer – It should either
    Throw the same IOException
    Throw a sub class of IOException.
    Throw nothing
  10. What are the two ways of making a class non-extensible?
    Make the class Final
    Make the constructor private

Saturday, June 4, 2011

Brief uses and advantages of java.util.Arrays


Hi Folks,

In this post i want to introduce you guys about one common utility class that is very helpful in sorting and searching of values in an array of any type like int,long,String etc. Simply we can state it as java.util.Arrays which is very useful in manipulating arrays such as searching,sorting and viewing all values as a list at a time without any iteration etc.

This class contains various methods to manipulate arrays of any type. For example, you have an array of string values like below

String s[] = {"hi", "ramu", "cris"};

and you want to sort these values then you don't need to write/implement any sorting algorithm instead you can simply use statement

Arrays.sort(s[]);

to sort those values. This java.util.Arrays.sort(Object[] a) method is useful to sort array of objects. Here our s[] is String type of object so we can simply use this method to sort our array.

If you want to print all the values using at a time then also you don'e need to iterate instead you can use

System.out.println(Arrays.asList(s[]);

This method java.util.Arrays.asList(T... a) is used to back the values to a List.

Like these there are many more methods by which you can easily manipulate arrays.