Question: The Nature of Things The Java platform (Java language plus Java API) includes many useful constants and utility methods in wrapper classes related to the

The Nature of Things The Java platform (Java language plus Java API) includes many useful constants and utility methods in wrapper classes related to the primitive type they represent. For example, the Integer class contains the constant Integer.MAX_VALUE and Integer.MIN_VALUE. You can use these constant to find out if the value of a number is in the range of the integer data type. The Character class, contains the method Character.isDigit() among others. The Float class contains three very useful constants NaN (Not a Number), POSITIVE_ INFINITY and NEGATIVE_ INFINITY. You will find the same constant in the Double class. They are very useful when you what to check if the CST8132-OOP, HA02, MMXIs Page 2 of 3 result from some arithmetic operation is correct. For example, 1.0 / 0.0 will produce POSITIVE_INFINITY; square root of -1 will produce NaN. Note: You cannot compare these constants to some primitive type value. You must use an appropriate method, for example isNAN().

An important fact is that wrapper classes including String are immutable, which means they cannot be changed. They do not provide methods (called mutators or setters ) that allow the programmer to change their values. If you want to have an integer object containing a different value, you must create a new Integer object. Reference: Textbook Chapter 16, Sections 16.3 Type-Wrapper Classes, and 16.4 Autoboxing and Auto-Unboxing.

Syntax, Semantic Dissection, Code Example, and Program Code Dissection Wrapping a primitive type in an object:

Before Java 5: int a = 5; // declare and initialize a primitive int Integer oa = new Integer(a); //wrap a in Integer object Integer oa = new Integer(5); //wrap 5 in Integer object After Java 5: int a = 5; Integer oa = a; //wrap a in Integer object Integer oa = 5; //wrap 5 in Integer object

This new language feature is known as autoboxing. It is convenient, saves some typing, but actually, the compiler will convert your code to the one shown under Before Java 5. The autoboxing can be used only for primitives and wrappers of the same types i.e. Integer and int, Float and float and so on. In all other cases you must use a constructor or an appropriate method. Unwrapping a primitive type from an object: Before Java 5: int a; // declare a primitive int Integer oa = new Integer(5); //wrap 5 in Integer object a = oa.intValue();//unwrap the Integer object After Java 5: int a; // declare a primitive int Integer oa = 5; //wrap 5 in Integer object a = oa; //unwrap the Integer object This new language feature is known as auto-unboxing. It is convenient, saves some typing, but actually, the compiler will convert your code to the one shown under Before Java 5. The auto-unboxing can be used only for primitives and wrappers of the same types i.e. Integer and int, Float and float and so on. In all other cases you must use a constructor or an appropriate method. CST8132-OOP, HA02, MMXIs Page 3 of 3 The new auto boxing/unboxing feature allows the programmer to write expression involving primitive and object types. For example: a = oa + 5; will compile and work. Mixing primitive and wrapper types in expressions is not considered a good programming practice. Hybrid Activity Exercise Exercise 1 Use your browser to open the Java API documentation. Find the java.lang package and explore carefully wrapper classes for primitive types you have used (skip Void). Exercise 2 Your task is to write a simple Java class (program) called Wrapper. Write a method with the following signature and return value: public Long addIntegers(Integer a, Integer b) {your code here} The method must return the result from the addition of a and b. Try Before Java 5 and After Java 5 syntax. Write another method: public Double divideFloats(Float a, Float b) {your code here} Write a main method and test your addInteger() and divideFloats() methods. See what will happen when you divide a by b and b=0. The method must return null if the result from the division is infinity or NaN. Questions Q1. Do all primitive data types have a corresponding wrapper class? Q2. Will the following line compile? Float f = 5.0; Submission Using the submission link, copy and paste your source code for your divideFloats() method. To earn the marks you must submit your source code by the end of Week 5. The exercise will be marked according to the following marking method: public int markHA2E2(Boolean submitted, Boolean correct){ int mark = 0; if(submitted & correct.equals(true)) mark = 2; return mark; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!