Hide Answers
1.what is a transient variable?
2.which containers use a border Layout as their default layout?
3.Why do threads block on I/O?
4. How are Observer and Observable used?
5. What is synchronization and why is it important?
6. Can a lock be acquired on a class?
7. What's new with the stop(), suspend() and resume() methods in JDK 1.2?
8. Is null a keyword?
9. What is the preferred size of a component?
10. What method is used to specify a container's layout?
11. Which containers use a FlowLayout as their default layout?
12. What state does a thread enter when it terminates its processing?
13. What is the Collections API?
14. Which characters may be used as the second character of an identifier, but not as the first character of an identifier?
15. What is the List interface?
16. How does Java handle integer overflow and underflow?
17. What is the Vector class?
18. What modifiers may be used with an inner class that is a member of an outer class?
19. What is an Iterator interface?
20. What is the difference between the >> and >>> operators?
21. Which method of the Component class is used to set the position and size of a component?
22. How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
23.What is the difference between yielding and sleeping?
24. Which java.util classes and interfaces support event handling?
25. Is sizeof a keyword?
26. What are wrapped classes?
27. Does garbage collection guarantee that a program will not run out of memory?
28. What restrictions are placed on the location of a package statement within a source code file?
29. Can an object's finalize() method be invoked while it is reachable?
30. What is the immediate superclass of the Applet class?
31. What is the difference between preemptive scheduling and time slicing?
32. Name three Component subclasses that support painting.
33. What value does readLine() return when it has reached the end of a file?
35. What is clipping?
36. What is a native method?
37. Can a for statement loop indefinitely?
38. What are order of precedence and associativity, and how are they used?
39. When a thread blocks on I/O, what state does it enter?
40. To what value is a variable of the String type automatically initialized?
41. What is the catch or declare rule for method declarations?
42. What is the difference between a MenuItem and a CheckboxMenuItem?
43. What is a task's priority and how is it used in scheduling?
44. What class is the top of the AWT event hierarchy?
45. When a thread is created and started, what is its initial state?
46. Can an anonymous class be declared as implementing an interface and extending a class?
47. What is the range of the short type?
48. What is the range of the char type?
49. In which package are most of the AWT events that support the event-delegation model defined?
50. What is the immediate superclass of Menu?
Hide Answers
My Experiments With Java
Java Experiments
Sunday, October 28, 2018
Wrapper class in java - example
f the main advantages of wrapper class is avoiding explicit type casting for primitive types while using collections.
Here is a basic example which will give compilation errors without explicit casting as shown below.
You can correct the program by adding type casting and here is a working sample
To avoid this explicit casting wrapper, we can add wrapper class as a generic argument to the collection.
eg: List<Integer> intList = new ArrayList<Integer>();
With this declaration of wrapper class, you can avoid explicit type casting. This example might not make good sense as we are declaring the list ourselves but when we are working with numerous lists in a big project, you can find lot of instances where avoiding explicit type casting is very useful and using wrapper classes save us from type casting exceptions.
One more advantage is - as you are declaring Integer wrapper class as a generic argument, while coding adding any other wrapper class value to the list will give you a compilation error.
Another useful feature used with wrapper classes is unboxing.
int value= intList.get(0); // here hava compiler will unbox Integer wrapper class value to int premitive type
Here is a basic example which will give compilation errors without explicit casting as shown below.
You can correct the program by adding type casting and here is a working sample
public class WrapperClassInJava {
public static void main(String[] args) {
List intList = new ArrayList();
intList.add(123);
int value= (int)intList.get(0);
}
}
To avoid this explicit casting wrapper, we can add wrapper class as a generic argument to the collection.
eg: List<Integer> intList = new ArrayList<Integer>();
With this declaration of wrapper class, you can avoid explicit type casting. This example might not make good sense as we are declaring the list ourselves but when we are working with numerous lists in a big project, you can find lot of instances where avoiding explicit type casting is very useful and using wrapper classes save us from type casting exceptions.
public class WrapperClassInJava {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<Integer>();
intList.add(123);
int value= intList.get(0);
System.out.println(value);
}
}
One more advantage is - as you are declaring Integer wrapper class as a generic argument, while coding adding any other wrapper class value to the list will give you a compilation error.
Another useful feature used with wrapper classes is unboxing.
int value= intList.get(0); // here hava compiler will unbox Integer wrapper class value to int premitive type
Subscribe to:
Comments (Atom)
