Question

QUESTION 2: Elementary Java Programming (a) Explain what a Wrapper class is in Java and provide an example of one and what it

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(a)

A Wrapper class is a class whose object wraps or contains a primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data types. In other words, we can wrap a primitive value into a wrapper class object.

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.

// Java program to demonstrate Wrapping and UnWrapping

// in Java Classes

class WrappingUnwrapping

{

    public static void main(String args[])

    {

        // byte data type

        byte a = 1;

        // wrapping around Byte object

        Byte byteobj = new Byte(a);

        // int data type

        int b = 10;

        //wrapping around Integer object

        Integer intobj = new Integer(b);

        // float data type

        float c = 18.6f;

        // wrapping around Float object

        Float floatobj = new Float(c);

        // double data type

        double d = 250.5;

        // Wrapping around Double object

        Double doubleobj = new Double(d);

        // char data type

        char e='a';

        // wrapping around Character object

        Character charobj=e;

        // printing the values from objects

        System.out.println("Values of Wrapper objects (printing as objects)");

        System.out.println("Byte object byteobj: " + byteobj);

        System.out.println("Integer object intobj: " + intobj);

        System.out.println("Float object floatobj: " + floatobj);

        System.out.println("Double object doubleobj: " + doubleobj);

        System.out.println("Character object charobj: " + charobj);

        // objects to data types (retrieving data types from objects)

        // unwrapping objects to primitive data types

        byte bv = byteobj;

        int iv = intobj;

        float fv = floatobj;

        double dv = doubleobj;

        char cv = charobj;

        // printing the values from data types

        System.out.println("Unwrapped values (printing as data types)");

        System.out.println("byte value, bv: " + bv);

        System.out.println("int value, iv: " + iv);

        System.out.println("float value, fv: " + fv);

        System.out.println("double value, dv: " + dv);

        System.out.println("char value, cv: " + cv);

    }

}

(b)For loop will be used as there are many limitations of for each loop due to which it doesnt fit everywhere.

Limitations of for-each loop

  1. For-each loops are not appropriate when you want to modify the array
  2. For-each loops do not keep track of index. So we can not obtain array index using For-Each loop
  3. For-each only iterates forward over the array in single steps
  4. For-each cannot process two decision making statements at once

(c)

Difference between StringBuffer and StringBuilder

Java provides three classes to represent a sequence of characters: String, StringBuffer, and StringBuilder. The String class is an immutable class whereas StringBuffer and StringBuilder classes are mutable. There are many differences between StringBuffer and StringBuilder. The StringBuilder class is introduced since JDK 1.5.

A list of differences between StringBuffer and StringBuilder are given below:

No. StringBuffer StringBuilder
1) StringBuffer is synchronized i.e. thread safe. It means two threads can't call the methods of StringBuffer simultaneously. StringBuilder is non-synchronized i.e. not thread safe. It means two threads can call the methods of StringBuilder simultaneously.
2) StringBuffer is less efficient than StringBuilder. StringBuilder is more efficient than StringBuffer.

The StringBuffer and StringBuilder classes are used when there is a necessity to make a lot of modifications to Strings of characters. Unlike Strings, objects of type StringBuffer and String builder can be modified over and over again without leaving behind a lot of new unused objects.

##That is all about your answer..........please upvote my answer.........please...........

Add a comment
Know the answer?
Add Answer to:
QUESTION 2: Elementary Java Programming (a) Explain what a Wrapper class is in Java and provide...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Those questions are about Java. Question 2 Which of the following is NOT a wrapper class?...

    Those questions are about Java. Question 2 Which of the following is NOT a wrapper class? Integer Double String Character Flag this Question Question 3 Which of the following kinds of things may be stored directly in an ArrayList? None of the above primitive values Either of these two kinds of data object references Flag this Question Question 4 In this declaration: ArrayList<Point> polygon; what do we call 'Point'? a type argument a wrapper class a collection a variable Flag...

  • The files must be called Proper coding conventions required the first letter of the class start...

    The files must be called Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter. Only submit the .java file needed to make the program run. Do not submit the .class file or any other file. 5% Style Components Include properly formatted prologue, comments, indenting, and other style elements as shown in Chapter 2 starting page 64 and Appendix 5 page 881-892....

  • the programming language is in java Problem 2 You are given an array A with n...

    the programming language is in java Problem 2 You are given an array A with n distinct elements. Implement an (n log n)-time algorithm that creates an array B where all elements are in range from 0 to n - 1 and where the order of elements is the same as in A. That is, 0 has the same index in B as the smallest element in A, 1 has the same index in B as the second smallest element...

  • 1.Explain how arrays allow you to easily work with multiple values. 2 Explain how for loops...

    1.Explain how arrays allow you to easily work with multiple values. 2 Explain how for loops can be used to work effectively with elements in an array. 3 Provide an example of using a for loop without an array. 4 What is the main difference between standard array and a dynamic array? 5 How are elements added to a vector in C++? 6 How can you remove an element from a vector in C++? 7 Why pass a vector by...

  • Programming Question

    Exercise #1:Write a C program that contains the following steps. Read carefully each step as they are not only programming steps but also learning topics that explain how numeric arrays in C work.Write a while loop to input a series of numbers (either from a file or from the keyboard, but using a file will make for easier debugging) into a one dimensional array of type double named x_arr, declared to be 25 elements in length. Count the elements as you...

  • In Java describe the differences between an abstract class and an interface. Provide examples for both...

    In Java describe the differences between an abstract class and an interface. Provide examples for both citing the differences. Which would you choose to use and why?

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • Programming in java In this part of this Lab exercise, you will need to create a...

    Programming in java In this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple manipulation of an array. Then you should add a main method to this class to check that your code does what it is supposed to do. 1. Create a BlueJ project named LAB06 as follows: a. Open the P drive and create a folder named Blue), if it does not exist already. Under this folder,...

  • Interfaces 1. What is inside an interface definition? What does a class do to an interface...

    Interfaces 1. What is inside an interface definition? What does a class do to an interface and what keyword is involved? How does a class do this to an interface (what should we find in the class)? Can an interface have a generic parameter? How do you instantiate an interface as an object? What methods can’t you use on an interface type? Abstract Data Types 2. What does an ADT define? Does an ADT specify programming language and/or data structures...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT