Java Sol 1,4
Java Sol 1,4
Write a
program that demonstrate the use of raw types and explain the compiler warning
generated
In Java, raw types refer to using a generic class or interface without specifying a type
parameter. For example, using List instead of List<String>. Raw types were primarily used
in Java before generics were introduced (Java 5) to provide backward compatibility.
Why are Raw Types Considered Unsafe? Raw types are considered unsafe because
they bypass the type checking provided by generics. This can lead to ClassCastException at
runtime, which would otherwise be caught at compile time if generics were used
properly.
Example Program:
java
Copy code
import java.util.ArrayList;
import java.util.List;
Compiler Warning:
When compiling the above code, you might see a warning similar to:
This warning indicates that you're using raw types and the compiler can't ensure type safety,
potentially leading to runtime exceptions.
1 b) Differentiate between string and stringBuffer. explain reverse() method
of stringBuffer with an example.
• String:
• StringBuffer:
reverse() Method of StringBuffer: The reverse() method reverses the characters in the
StringBuffer object.
Example:
java
Copy code
public class StringBufferExample {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Hello World");
sb.reverse();
System.out.println(sb); // Output: dlroW olleH
}
}
In this example, the reverse() method changes the order of characters in the StringBuffer
object from "Hello World" to "dlroW olleH".
4 a) Develop a java program to count the occurance of characters in the given
string. Explain IndexOf and LastIndexOf()method of string class with example.
java
Copy code
import java.util.HashMap;
import java.util.Map;
• indexOf Method:
o Returns the index within the string of the first occurrence of the specified
character or substring. If the character or substring is not found, it returns -1.
o Example:
System.out.println(index1); // Output: 0
System.out.println(index2); // Output: 8
System.out.println(index3); // Output: 1
}
}
• lastIndexOf Method:
o Returns the index within the string of the last occurrence of the specified
character or substring. If the character or substring is not found, it returns -1.
o Example:
System.out.println(lastIndex1); // Output: 7
System.out.println(lastIndex2); // Output: 8
System.out.println(lastIndex3); // Output: 0
}
}
4b) What are the legacy classes and interfaces in java? Provide examples
write a program that uses a legacy collection class and then refactor it to use
the generic collection.
Legacy Classes and Interfaces: In Java, legacy classes and interfaces refer to
those that existed before Java 2 (Java 1.2), when the Collections Framework
was introduced. These include classes and interfaces in the java.util
package, such as:
• Vector
• Hashtable
• Stack
• Enumeration
These classes were later adapted to work with the Collections Framework and
generics, but they are considered legacy because they were designed before the
collections framework was introduced.
Explanation