3.Differentiate between String and string Buffer.
Ans:-
Ans:-
String:-
- String class is immutable.
- String is slow and consumes more memory when you concat too many strings because every time it creates new instance.
- 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
Comments
Post a Comment