Chapter 5 Unit 1: Primitives Value and Data Types
Fill in the blanks with an appropriate word/words
Data Type is used to allocate exact space for storage of a value.
The data size of byte type ranges from -128 to 127.
A character type data is enclosed within single quotes.
A double is the higher most data type.
A set of characters is assigned to String variable.
The ASCII codes of upper case letters ranges from 65 to 90.
The data type for the values true/false are called boolean type data.
Resulting data type of expression 11.4f /2.0d will be double.
Class is an example of non primitive data type.
Byte is lower data type than short.
Write Short Answers
State two categories of data types.
Primitive and Non-Primitive data types.
(a) Integer and Floating constant
|Integer Constant||Floating Constant|
|Integer Constants represent whole number values like 2, -16, 18246, 24041973, etc.||Floating Constants represent fractional numbers like 3.14159, -14.08, 42.0, 675.238, etc.|
|Integer Constants are assigned to variables of data type — byte, short, int, long, char||Floating Constants are assigned to variables of data type — float, double|
(b) Primitive and Non-Primitive data types
|Primitive Data Types||Non-Primitive Data Types|
|Primitive Data Types are Java's fundamental data types||Non-Primitive Data Types are created by using Primitive Data Types|
|Primitive Data Types are built-in data types defined by Java language specification||Non-Primitive Data Types are defined by the programmer|
|Examples of Primitive Data Types are byte, short, int, long, float, double, char, boolean||Examples of Non-Primitive Data Types are Class and Array|
Write down the data type of the following:
(a) Integer — int
(b) Character — char
(c) A fractional number — double
(d) A special character — char
What is meant by boolean type data? Explain with an example.
A boolean data type is used to store one of the two boolean values — true or false. The size of boolean data type is 8 bits or 1 byte.
boolean bTest = false;
What do you understand by type conversion?
The process of converting one predefined type into another is called type conversion.
Define the following with an example each:
(a) Implicit type conversion
In implicit type conversion, the result of a mixed mode expression is obtained in the higher most data type of the variables without any intervention by the user. Example:
int a = 10; float b = 25.5f, c; c = a + b;
(b) Explicit type conversion
In explicit type conversion, the data gets converted to a type as specified by the programmer. For example:
int a = 10; double b = 25.5; float c = (float)(a + b);
Explain the need to use suffix L in a long type data.
The default data type of integer literals is int. So if I try to assign a value to a variable of long type like this:
//Will result in Compilation Error long worldPopulation = 7780000000;
This will result in a compilation error of "integer number too large". The reason for this error is that Java compiler is treating the number 7780000000 as an int and as this value is outside the range of int so we are getting this error. To fix this, we need to explicitly tell the compiler to treat this number as a long value and we do so by specifying L as the suffix of the number. The case of L doesn’t matter, both capital and small L are equivalent for this. The correct way to write the above statement using the suffix L is this:
//Correct way to assign long value long worldPopulation = 7780000000L;
Differentiate single precision and double precision data value.
- Single precision format uses a total of 32 bits to represent the fractional number. Out of that 32 bits, 24 are used to represent the significand and 8 bits are used to represent the exponent. Double precision format uses a total of 64 bits to represent the fractional number. Out of that 53 bits are used to represent the significand and 11 bits are used to represent the exponent.
- Double precision format stores fractional numbers at higher accuracy than single precision format.
- float data type stores a number in single precision format whereas double data type stores a number in double precision format.
A non-primitive data type is also referred to as reference type. Explain Why?
Unlike primitive data type, the allocation of non-primitive data type takes place in dynamic memory. The accessing of non-primitive data types is based on their references (addresses). Hence, non-primitive data types are also referred to as reference type.
Explain the term type casting.
The process of converting one predefined type into another is called type casting.
Predict the return data type of the following:
int p; double q; r = p+q; System.out.println(r);
Return data type is double.
float m; p = m/3*(Math.pow(4,3)); System.out.println(p);
Return data type is double.
What are the resulting data type of the following explicit conversions?
int i; float f; double d; short s; char c; byte b;
(a) (float) i/b + d;
(float) i/b + d
⇒ float / byte + double
⇒ float + double
(b) (int)f*d + c/s;
(int)f*d + c/s
⇒ int * double + char / short
⇒ double + short
(c) (char)i + f - b*d;
(char)i + f - b*d
⇒ char + float - byte * double
⇒ char + float - double
(d) (double) (f/i)*c + s;
(double) (f/i)*c + s
⇒ (double) (float / int) * char + short
⇒ double * char + short
⇒ double + short
(e) (char) d + b/i - f*s;
(char) d + b / i - f * s
⇒ char + byte / int - float * short
⇒ char + int - float