Data types, variables and constants Flashcards

1
Q

Data types

A

• Data that can be handled by a computer can
be of many types such as Character, Numeric,
String etc.
• Java classifies data types as follows:
– Primitive or in-built data type
– Reference data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Primitive Data Types

A
Primitive Data Types
• Primitive data types (also known as the
fundamental data types) are the inbuilt data
types offered by Java.
• Java provides 8 primitive data types which are
categorized as follows:
– Integral
• byte, short, int, long
– Fractional
• float, double
– Character
• char
– Boolean
• boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Sizes of integral data types

A

byte: 1 byte
short: 2 bytes
int: 4 bytes
long: 8 bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Ranges of integral data types

A

byte: -128 to +127
short: -32768 to +32767
int: -2 billion to +2 billion i.e -2^31 to 2^31 -1
long: -2^63 to +2^63 - 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Sizes of fractional data types

A

float: 4 bytes
double: 8 bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Ranges of fractional data types

A

float: -3.4E+38 to +3.4E+38
double: -1.7E + 308 to 1.7E + 308

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Precision of fractional data types

A

float: 6 digit single precision
double: 15 digit double precision

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Size and range of char data type

A

size: 2 bytes
range: 0 to 65535

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Size or range of boolean data type

A

size: 8 bits, uses 1 bit
range: true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Reference Types

A

Inbuilt reference types
– Arrays
– Strings
• User-defined reference types (also known as
user-defined objects or user-defined data
types)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Variables

A

• A variable is a named storage location in the
computer’s memory.
• It can hold only one value of a particular data
type.
• The value in a variable can be manipulated
during the execution of the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Constants

A

• A constant is a variable whose value cannot
be changed once it has been assigned a
value. If the value is changed subsequently in
the program, the compiler gives an error.
• The keyword final makes a variable as
constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Advantages of Constants

A

• Indicates to the programmer that the value in
that constant (variable) cannot be changed and
hence makes it easier to read and check for
correctness. Even if the programmer accidentally
changes the value of that constant in the code,
the compiler gives an error.

• If the value in the constant needs to be changed,
only the declaration needs to be modified. On
the other hand if a literal is used, one has to
search through the entire program for every
occurrence of that specific value and then
change all the occurrences.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Suffixes

A

• Suffixes are used to explicitly specify the data type of a literal data value.
• In some situations it is required, because the compiler
gives an error, when the value that is assigned to a
variable is beyond the capacity of the variable on the left hand side.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly