Friday, 4 July 2014

appendCodePoint (int codePoint)

public synchronized StringBuffer appendCodePoint(int codePoint)
Appends the string representation of the codePoint argument to this sequence. The length of this sequence increases by Character.charCount(codePoint).

public class StringBufferAppendCodePoint {
    public static void main(String args[]){
        StringBuffer s1 = new StringBuffer("Value of s1 is ");
        s1.appendCodePoint(63);
        
        System.out.println("s1 = " + s1);
    }
}


Prevoius                                                 Next                                                 Home

Thursday, 3 July 2014

Default Method and Multiple Inheritance Ambiguity Problems

Since Java8 support default implementations in interfaces, there is an ambiguity problem exist.

For Example
lets take Interface1, Interface2 both have default implementation for print method, lets say 'MyClass' implements both the interfaces, then there is an ambiguity related to print method usage.
public interface Interface1 {
    default void print(){
        System.out.println("I am in Interface1");
    }
}

public interface Interface2 {
     default void print(){
        System.out.println("I am in Interface2");
    }
}

public class MyClass implements Interface1, Interface2{

}

When you tries to compile the above program compiler throws below error.
MyClass.java:1: error: class MyClass inherits unrelated defaults for print() from types Interface1 and Interface2
public class MyClass implements Interface1, Interface2{
       ^
1 error

In order to solve this user has to provide implementation for the method print in the class.

public class MyClass implements Interface1, Interface2{
 public void print(){
  System.out.println("I am in MyClass");
 }
}

Inside the print method of class, you can call the default methods of the interaces like below.
public class MyClass implements Interface1, Interface2{
 public void print(){
  System.out.println("I am in MyClass");
  Interface1.super.print();
  Interface2.super.print();
 }
}








Prevoius                                                 Next                                                 Home

append (StringBuffer sb)

public synchronized StringBuffer append(StringBuffer sb)
Appends the specified StringBuffer to this sequence. If sb is null, then the four characters "null" are appended to this StringBuffer.

public class StringBufferAppendStringBuffer {
    public static void main(String args[]){
        StringBuffer s1 = new StringBuffer("Value of s2 is ");
        StringBuffer s2 = new StringBuffer("str2");
        
        s1.append(s2);
        
        System.out.println("s1 = " + s1);
    } 
}

Output
s1 = Value of s2 is str2




Prevoius                                                 Next                                                 Home

append (String str)

public synchronized StringBuffer append(String str)
Appends the specified string to this character sequence.

public class StringBufferAppendStr {
    public static void main(String args[]){
        StringBuffer s1 = new StringBuffer("Value of s2 is ");
        String s2 = "str2";
        
        s1.append(s2);
        
        System.out.println("s1 = " + s1);
    } 
}

Output
s1 = Value of s2 is str2





Prevoius                                                 Next                                                 Home

append (Object obj)

public synchronized StringBuffer append(Object obj)
Appends the string representation of the Object argument.

class Student{
    int id;
    String name;
    
    Student(int id, String name){
        this.id = id;
        this.name = name;
    }
    
    @Override
    public String toString(){
        return id + "." + name;
    }
}

public class StringBufferAppendObject {
    public static void main(String args[]){
        StringBuffer s1 = new StringBuffer("Value of s1 is ");
        Student stud = new Student(1, "Hari");

        s1.append(stud);
  
        System.out.println(s1);
    }
}

Output
Value of s1 is 1.Hari



Prevoius                                                 Next                                                 Home

append (long lng)

public synchronized StringBuffer append(long lng)
Appends the string representation of the long argument to this sequence.

public class StringBufferAppendLong {
    public static void main(String args[]){
        StringBuffer s1 = new StringBuffer("Value of s1 is ");
        long lng = 10;
        s1.append(lng);
  
        System.out.println(s1);
    }
}

Output

Value of s1 is 10




Prevoius                                                 Next                                                 Home

append (int i)

public synchronized StringBuffer append(int i)
Appends the string representation of the int argument to this sequence.

public class StringBufferAppendInt {
    public static void main(String args[]){
        StringBuffer s1 = new StringBuffer("Value of s1 is ");
        int i = 10;
        s1.append(i);
  
        System.out.println(s1);
    }
}

Output
Value of s1 is 10



Prevoius                                                 Next                                                 Home