Question

In: Computer Science

Why do you think Java provides both primitive data types and wrapper classes for them? Why...

Why do you think Java provides both primitive data types and wrapper classes for them? Why not just one or the other?

Solutions

Expert Solution

Java provides both primitive data types and wrapper classes.java being object-oriented in nature and expects everything in the form of an objects . so Primitive datatypes are converted into objects by using Wrapper classes.

A Wrapper class is a class whose object wraps or contains a primitive data types.

When we create a wrapper class object , it contains a field and in this field and can store a primitive data types. In other words, we can wrap a primitive value into a wrapper class object.

The following picture shows both primitive data types and wrapper classes.

Need of Wrapper Classes:

  1. They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).
  2. The classes in java.util package handles only objects and hence wrapper classes help in this case also.
  3. Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.
  4. An object is needed to support synchronization in multithreading.

Related Solutions

Background: introduction to "Wrapper" classes, this is a tool or concept provided in the Java Programming...
Background: introduction to "Wrapper" classes, this is a tool or concept provided in the Java Programming Language that gives us a way to utilize native datatypes as Objects with attributes and methods. A Wrapper class in Java is the type of class which contains or the primitive data types. When a wrapper class is created a new field is created and in that field, we store the primitive data types. It also provides the mechanism to covert primitive into object...
What are wrapper classes and why are they useful for ArrayLists? In your answer, include examples...
What are wrapper classes and why are they useful for ArrayLists? In your answer, include examples of autoboxing and unboxing. Your Discussion should be at least 250 words in length, but not more than 750 words. Once you’ve completed your initial post, be sure to respond to the posts of at least 3 of your classmates.
Compare and contrast the representation and use of primitive data types and built-in data structures such...
Compare and contrast the representation and use of primitive data types and built-in data structures such as C-strings and string objects?
Probiotics: Do You Need Them? What do you think? Do you think you should add probiotics...
Probiotics: Do You Need Them? What do you think? Do you think you should add probiotics to your diet? Why or why not? Do you think we know enough about probiotics to recommend them to the public? Site your source
Answer the following question: Why do you think eating disorders are on the rise for both...
Answer the following question: Why do you think eating disorders are on the rise for both men and women? Search the internet and find a photo that reflects how social media has shaped society's thinking and maybe our own. Please make good choices when choosing your picture! When you find that perfect photo please post the URL and briefly give one or two sentences discuss the photo. Keep in mind the following: What is the ‘reality’ behind these photos? How...
do you think we should be testing on animal and using them for research? explain why?
do you think we should be testing on animal and using them for research? explain why?
Connective tissue is the most diverse of all tissue types. Why do you think that is?...
Connective tissue is the most diverse of all tissue types. Why do you think that is? Why do you think blood and lymph are classified as a type of connective tissue?
Comparing Objects Vs Comparing Primitive data Types Topic: Discuss how do we compare Strings for equality?...
Comparing Objects Vs Comparing Primitive data Types Topic: Discuss how do we compare Strings for equality? Provide coding examples. How String comparison is different than the primitive data type comparison? Please cite any resources you used.
Do you think every program needs all three (algorithms, flowcharts, and pseudocodes) of them? Why or...
Do you think every program needs all three (algorithms, flowcharts, and pseudocodes) of them? Why or why not? Justify your answer with examples. If you plan to create any two of them for a program, which one would you eliminate? Why? You have read about fundamental data types in your book. Are these data types enough to represent all data or do you think additional data types are needed? Why or why not? Explain your answer with examples. Looking at...
Some genes don’t appear to have any mutations associated with them. Why do you think this...
Some genes don’t appear to have any mutations associated with them. Why do you think this is?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT