0% found this document useful (0 votes)
10 views

Unit 2 Answers

Uploaded by

tuy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Unit 2 Answers

Uploaded by

tuy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Q: Explain the use of Stringbuilder and Stringbuffer with example.

- In Java, both StringBuilder and StringBuffer are used to manipulate


strings, when there is a need to modify strings multiple times. These
classes are preferred over the String class when you need to perform
frequent modifications because they provide better performance and
efficiency.
StringBuilder

Purpose: Used to create and modify strings in a fast and efficient


manner.
Thread Safety: Not thread-safe, meaning it cannot be used safely in
multi-threaded programs without external synchronization.
Performance: Faster than StringBuffer because it does not have
synchronization overhead.
Use Cases: Use StringBuilder when working in a single-threaded
environment and performance is critical.

public class StringBuilderExample{


public static void main(String[] args){
StringBuilder builder=new StringBuilder("Hi");
builder.append("Java 8");
System.out.println("StringBuilderExample" +builder);
}
}

StringBuffer
Purpose:String Buffer class creates strings that can be modified in terms
of length and content.
Thread Safety:it ensures that multiple threads can work on the same object
without conflict.
Performance: Slower than StringBuilder due to synchronization.
User Case: Use StringBuffer when working in a multi-threaded environment
where thread safety is necessary.
public class StringBufferExample{
public static void main(String[] args){
StringBuffer buffer=new StringBuffer("Hi");
buffer.append("Java 8");
System.out.println("StringBufferExample" +buffer);
}
}

Q.Compare Stringbuffer and Stringbuilder in java.


-
Sr. No StringBuffer StringBuilder
1 StringBuffer has been part of StringBuilder was
Java since its first version. introduced in Java 5.
2 StringBuffer operations are StringBuilder operations are
synchronised. not synchronised.
3 Multiple threads cannot StringBuilder is not
access StringBuffer at the thread-safe.
same time due to its
thread-safety.
4 StringBuffer operations are StringBuilder operations are
slower compared to faster than StringBuffer.
StringBuilder.
5 StringBuffer is mutable, so we StringBuilder is also
can modify a string without mutable.
creating a new object
6 StringBuffer uses heap StringBuilder also uses
memory. heap memory.

Q. What are wrapper class and explain data conversion using valueoff()
method with example.

● Java provides type wrappers classes.


● Wrapper classes are those classes which are used to encapsulate a
primitive data type inside an object
● The use of wrapper class in Java is to convert primitive types into
object and object into primitive type.
● They are needed because Java is designed on the concept of object.
● It helps in synchronization of multithreading.

In Java, valueOf() method converts data from its internal form into a
human-readable form. It is used to convert data types into their
corresponding wrapper objects or String representation.

public class ValueOfExample {


public static void main(String[] args) {
// Example with Integer
String intString = "123";
Integer intValue = Integer.valueOf(intString); // Converts String to
Integer
System.out.println("Integer value: " + intValue); // Output: 123
// Example with Double
String doubleString = "45.67";
Double doubleValue = Double.valueOf(doubleString); // Converts
String to Double
System.out.println("Double value: " + doubleValue); // Output: 45.67
// Example with Boolean
String boolString = "true";
Boolean boolValue = Boolean.valueOf(boolString); // Converts String
to Boolean
System.out.println("Boolean value: " + boolValue); // Output: true
// Using valueOf() to convert primitives to Wrapper objects
int primitiveInt = 500;
Integer wrappedInt = Integer.valueOf(primitiveInt); // Converts int to
Integer
System.out.println("Wrapped Integer: " + wrappedInt); // Output: 500
}
}
Q. Uses of this keyword
the 6 usage of java this keyword.

1. This can be used to refer current class instance variable.


Class Student{
Int rollno;
String name;
Float fee;
Student(int rollno,String name,float fee){
This.rollno=rollno;
This.name=name;
This.fee=fee; 9.
}
Void display(){System.out.println(rollno+" "+name+" "+fee);}

}
Class TestThis2{
Public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
18. }}

Output
111 ankit 5000
112 sumit 6000

2. This can be used to invoke current class method (implicitly)


Class A{
Void m(){System.out.println("hello m");}
Void n(){
System.out.println("hello n");
//m();//same as this.m()
This.m();
}
8. }
Class TestThis4{
Public static void main(String args[]){
A a=new A();
a.n();
13. }}

Output:
Hello n
Hello m

3. This() can be used to invoke current class constructor.


4. This can be passed as an argument in the method call.
5. This can be passed as argument in the constructor call.
Class S2{
Void m(S2 obj){
System.out.println("method is
invoked"); 4. }
Void p(){
m(this);
}
Public static void main(String args[]){
S2 s1 = new S2();
s1.p();
}
}

Output:
Method is invoked

6. This can be used to return the current class instance from the
method.
Class B{
A4 obj;
B(A4 obj){
This.obj=o
bj; 5. }
Void display(){
System.out.println(obj.data);//using data member of A4 class 8.
}
9. }
Class A4{
Int
data=10; 12. A4(){
B b=new B(this);
b.display();
15. }
Public static void main(String args[]){
A4 a=new A4();
18. }
19. }

Output:10

Q. Methods of String comparison


Java, there are three techniques to compare strings:
1. By Using equals() Method
The equals() function of the String class compares the string's original
content. It compares string values for equality. Two methods are
available in the String class:

public boolean equals(Object another) compares this string to the


specified object.
public boolean equalsIgnoreCase(String another) compares this
string to another string, ignoring case.

Class Teststringcomparison2{
Public static void main(String args[]){
String s1="Sachin";
String s2="SACHIN"; System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true
}
}

Output
False
True

2. By Using == Operator
By using == operators

The == operator compares references to each other rather than


values.

Class Teststringcomparison3{
Public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to same
instance)
System.out.println(s1==s3);//false(because s3 refers to instance
created in nonpool)
}
}

Output
True
False

3. By compareTo()
Assume that s1 and s2 are two different String objects. If:
s1 == s2: The method returns 0.
s1 > s2: The method returns a positive value.
s1 < s2: The method returns a negative value.

Class Teststringcomparison4{
Public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
System.out.println(s1.compareTo(s2));//0
System.out.println(s1.compareTo(s3));//1(because s1>s3)
System.out.println(s3.compareTo(s1));//-1(because s3 < s1 )
}
}

Output
0
1
-1
Q. How to declare and initialize One Dimensional and two -dimensional
array with example?
-

You might also like