Lesson 3.4: Object-Oriented Programming Flashcards
Why are primitive types used over objects?
Primitive types، rather than objects، are used for these quantities for the sake of performance. Using objects for these basic types would add an unacceptable overhead to even the simplest of calculations. Thus، the primitive types are not part of the object hierarchy، and they do not inherit Object.
In layman’s terms، what are type wrappers?
type wrappers، which are classes that encapsulate a primitive type within an object.
What are the 8 type wrappers and what are they packaged in?
The type wrappers are Double، Float، Long، Integer، Short، Byte، Character، and Boolean، which are packaged in java.lang. These classes offer a wide array of methods that allow you to fully integrate the primitive types into Java’s object hierarchy.
What are the most used type wrappers and what abstract class do they inherit?
437Probably the most commonly used type wrappers are those that represent numeric values. These are Byte، Short، Integer، Long، Float، and Double. All of the numeric type wrappers inherit the abstract class Number.
What does the abstract class “Number” declare? What is the general form for its methods?
Number declares methods that return the value of an object in each of the different numeric types. These methods are shown here: byte byteValue( ) double doubleValue( ) float floatValue( ) int intValue( ) long longValue( ) short shortValue( )
Though not used anymore، what are the general forms for the integer and double wrapper constructors? Are there any other wrapper types that get a constructor?
All of the numeric type wrappers define constructors that allow an object to be constructed from a given value، or a string representation of that value. For example، here are the constructors defined for Integer and Double: Integer(int num) Integer(String str) throws NumberFormatException Double(double num) Double(String str) throws NumberFormatException If str does not contain a valid numeric value، then a NumberFormatException is thrown. However، beginning with JDK 9، the type-wrapper constructors have been deprecated.
Why is using a type constructor outdated for obtaining a wrapper object? What should you do to obtain a wrapper object? What is it’s general form?
Today، it is recommended that you use one of the valueOf( ) methods to obtain a wrapper object. The valueOf( ) method is a static member of all of the wrapper classes and all numeric classes support forms that convert a numeric value or a string into an object. For example، here are two forms supported by Integer: static Integer valueOf(int val) static Integer valueOf(String valStr) throws NumberFormatException Here، val specifies an integer value and valStr specifies a string that represents a properly formatted numeric value in string form.
What is boxing and in what version did doing it manually become outdated?
The process of encapsulating a value within an object is called boxing. Prior to JDK 5، all boxing took place manually، with the programmer explicitly constructing an instance of a wrapper with the desired value
What is unboxing?
The process of extracting a value from a type wrapper is called unboxing. Again، prior to JDK 5، all unboxing also took place manually، with the programmer explicitly calling a method on the wrapper to obtain its value.
What is autoboxing and auto-unboxing?
Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrapper whenever an object of that type is needed. There is no need to explicitly obtain an object. Auto-unboxing is the process by which the value of a boxed object is automatically extracted (unboxed) from a type wrapper when its value is needed.
How do you autobox a value?
You need only assign that value to a type-wrapper reference. Java automatically constructs the object for you. For example، here is the modern way to declare an Integer object that has the value 100: Integer iOb = 100; // autobox an int Notice that the object is not explicitly boxed. Java handles this for you، automatically.
How do you auto-unbox a value?
To unbox an object، simply assign that object reference to a primitive-type variable. For example، to unbox iOb، you can use this line: int i = iOb; // auto-unbox Java handles the details for you.
Give me an example of autoboxing/unboxing in a piece of code.
For example، consider the following program: Code to the right >
Why are primitive types used instead of autoboxing & unboxing?
. It is far less efficient than the equivalent code written using the primitive type double. The reason is that each autobox and auto-unbox adds overhead that is not present if the primitive type is used.
Why are parameterized types important? What is its relationship with generics?
At its core، the term generics means parameterized types. Parameterized types are important because they enable you to create classes، interfaces، and methods in which the type of data Page 453upon which they operate is specified as a parameter. A class، interface، or method that operates on a type parameter is called generic، as in generic class or generic method.
What is similar in Java to C++’s “templates”?
Java generics are similar to templates in C++. What Java calls a parameterized type، C++ calls a template. However، Java generics and C++ templates are not the same، and there are some fundamental differences between the two approaches to generic types. For the most part، Java’s approach is simpler to use. A word of warning: If you have a background in C++، it is important not to jump to conclusions about how generics work in Java. The two approaches to generic code differ in subtle but fundamental ways.
What is similar in Java to C++’s “templates”?
Java generics are similar to templates in C++. What Java calls a parameterized type، C++ calls a template. However، Java generics and C++ templates are not the same، and there are some fundamental differences between the two approaches to generic types. For the most part، Java’s approach is simpler to use. A word of warning: If you have a background in C++، it is important not to jump to conclusions about how generics work in Java. The two approaches to generic code differ in subtle but fundamental ways.