3.Differentiate between String and string Buffer.
Ans:-

String:-

  1. String class is immutable.
  2. String is slow and consumes more memory when you concat too many strings because every time it creates new instance.
  3. 3.String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method.

Example:-

public class StringDemo{
public static void main(String args[]){
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
String helloString = new String(helloArray);
System.out.println( helloString );
}
}

OUTPUT:-
hello

String Buffer:-

  1. StringBuffer class is mutable.
  2. StringBuffer is fast and consumes less memory when you cancat strings.
  3. StringBuffer class doesn't override the equals() method of Object class.

Comments

Popular posts from this blog

java Assignment Questions 4