Data Types in java tutorial with examples
Table of Contents
Attend job interviews easily with these Data Types in java MCQ
Data Types in java
A Data type refers to the type of data we use in our Java program. There are different types of data namely integer numbers, real numbers or fractions and character data. These are non object data types.
Pri-mitive Data Types in JAVA
Java does not offer unsigned data types like C language. It provides only a signed version of integer data types. Sizes of data types are machine independent in Java. So irrespective of hardware and operating system implementations, sizes of Java data types remain the same to provide consistency and portability.
Note: These pri-mitive data types in Java are implemented in non object oriented way. This retains the similar performance offered by languages like C and C++. This is the only reason why Java is not a pure Object Oriented Language.
Java language has 8 different pri-mitive data types. There are 8 pri-mitive data types in Java: byte, char, short, int, long, float, double and boolean. These data types act as the basic building blocks of data manipulation in Java. Pri-mitive data types have a constraint that they can hold data of the same type and have a fixed size
- byte
- short
- int
- long
- float
- double
- char
- boolean
1. Byte
A Byte is the smallest data type in Java language. and Byte occupies 8 bits in memory. Byte data type is used in applications involving Input Output buffers, streams and raw data. Binary data is also processed using byte data type in many software. Byte for numeric applications is not recommended. You can use short, int and long for number type calculations.
Number Range of Byte Data Type
Byte data type can hold numbers between -128 and + 127.
Size of byte data type
Byte occupies only 8 bits or 1 byte in memory.
Example:
byte buf = 124;
2. Short
Short is a numeric data type. A Short data type is half of int data type.
Number Range of short Data Type
Short data type can hold numbers between -32768 and +32767.
Size of short data type
Short data type occupies 2 bytes or 16 bits of memory.
Example:
short num = 1245;
3. Int
Int is a numeric data type. Byte and Short numbers are converted into Int type in expressions
Number Range of int Data Type
Int data type can hold numbers between -2147483648 and 2147483647.
Size of int data type
Int data type occupies 4 bytes or 32 bits of memory.
Example:
int num = 123456;
4. Long
Long is a numeric data type. Byte, Short and Int numbers are converted into Long type . if at least one number is of long type.
Number Range of long Data Type
Long data type can hold numbers between -9223372036854775808 and 9223372036854775807.
Size of long data type
Long data type occupies 8 bytes or 64 bits of memory.
Example:
long num = 123456L or 123456l;
5. Float
Float data type is used to represent real numbers with a fractional part. Java implements IEEE-754 standard to implement floating point numbers. Float is called Single Precision data type. Floating point data is for storing real numbers. On 32 bit machines, float data type is efficient for manipulating real numbers.
Number Range of float Data Type
Float data type can hold numbers between 1.4e-045 to 3.4e+038.
Size of float data type
Float data type occupies 4 bytes or 32 bits of memory.
Example:
float sal = 123456.235f or 123456.235F;
6. Double
A Double data type is used to represent real numbers with a fractional part. and Double is called Double Precision data type. Double stores the same data as that of a float. The only difference is that double data type stores more digits of precision after the decimal point. Double can store very big real numbers. On 64 bit machines, double data type is efficient for manipulating real numbers.
Number Range of double Data Type
Double data type can hold numbers between 1.4e-045 to 3.4e+038.
Size of double data type
Double data type occupies 8 bytes or 64 bits of memory.
Data Types in java Example:
double sal = 123456.235;
7. Char
Character type data is represented using char data type. While C language uses ASCII to represent just 128 characters, Java language uses UNICODE to represent all international 65536 letters of all languages all over the world. So a char type in Java is bigger with 2 Bytes of storage.
Number Range of char Data Type
Char data type can hold numbers between 0 to 65535.
Size of char data type
Char data type occupies 2 bytes or 16 bits of memory.
Example:
char code = 'A'; char type = 90;//number is converted to character
8. Boolean
Boolean data type represents only two values namely true and false. All relational operations or expressions result in one of these two values. C considers number Zero as false and Non-Zero number a true. But Java does not work in that way.
Values for Boolean Data Type
true, false.
Size of boolean data type
Boolean data type is a pri-mitive data type . Size may be 1 bit.
Data Types in java Example:
boolean exp = true; //works boolean kk = 1; //Error. Can not assign number if(exp) { //works } if(kk) //kk is not boolean type. Error { //.... }
[WpProQuiz 52]
Data Types in java MCQ
1) What does a Data Type in Java refers to?
A) The place where data is stored
B) The technique how data is retreived
C) The type or variety of data being handled for reading and writing
D) None of the above
Answer [=] C
Explanation:
Integers, Real numbers, Boolean, Characters etc
2) Choose the wrong statement about Java programming?
A) Java supports unsigned integers
B) Java supports signed integers
C) Java supports signed char
D) None of the above
Answer [=] A
Explanation:
Only C language supports unsigned integers. Java does not support.
3) Which data type among the following is an implementation of Objects or OOPs?
A) byte
B) int
C) char
D) None of the above
Answer [=] D
Explanation:
All pri-mitive data types are implemented in a Non-Object Oriented way.
4) What is a Pri-mitive Data Type in Java?
A) Data type, which is implemented in an Object-oriented way.
B) Data Type which is implemented in a machine-dependent way
C) Data Type which is implemented in a non-object oriented way.
D) None of the above
Answer [=] C
5) which among the following is not a Data Type in Java?
A) short
B) int
C) long double
D) double
Answer [=] C
Explanation:
“long double” is present only in C language.
6) Which among the following is not a valid Data Type in Java?
A) long
B) bool
C) double
D) float
Answer [=] B
Explanation:
It is “boolean” not “bool”.
7) Which is the data type used mostly to handle streams and buffers in Java language?
A) short
B) int
C) byte
D) float
Answer [=] C
8) Which is the data type that is not recommended for numeric applications in Java?
A) byte
B) float
C) int
D) long
Answer [=] A
Explanation:
Size of a byte is only 8 bits. Also, any arithmetic operation produces output in int, float or double. It cannot handle more data.
9) Choose the number range for byte data type in Java?
A) -127 to +128
B) -128 to +127
C) 0 to 256
D) 0 to 255
Answer [=] B
10) What is the size of a SHORT integer in Java?
A) 1 byte
B) 2 bytes
C) 4 bytes
D) 8 bytes
Answer [=] B
Explanation:
Number range is -32768 and +32767.
11) What is the size of an INT integer in Java?
A) 2 bytes
B) 4 bytes
C) 6 bytes
D) 8 bytes
Answer [=] B
Explanation:
Number range is -2147483648 and 2147483647.
12) What is the size of a LONG integer in Java?
A) 2 bytes
B) 4 bytes
C) 8 bytes
D) 16 bytes
Answer [=] C
Explanation:
Range: -9223372036854775808 and 9223372036854775807.
13) What is the size of a FLOAT floating point number in Java?
A) 2 bytes
B) 4 bytes
C) 6 bytes
D) 8 bytes
Answer [=] B
Explanation:
Number range is ±3.40282347E+38F
14) What is the size of a DOUBLE floating point number in Java?
A) 4 bytes
B) 6 bytes
C) 8 bytes
D) 16 bytes
Answer [=] C
Explanation:
Number range is ±1.79769313486231570E+308.
15) What is the size of a CHAR data type constant in Java?
A) 1 byte
B) 2 bytes
C) 4 bytes
D) 6 bytes
Answer [=] B
16) What is the size of a boolean data type constant in Java?
A) 1 bit
B) 4 bits
C) 8 bits
D) Not documented well
Answer [=] D
Explanation:
A boolean value can hold only one bit of information. But the size of a boolean constant in memory is not defined clearly anywhere. It is machine dependent. It may be 4 bytes or so.
17) What is the IEEE standard adopted to represent Floating point numbers in Java?
A) IEEE 9000
B) IEEE 800
C) IEEE 754
D) IEEE 512
Answer [=] C
Explanation:
IEEE stands for Institute of Electrical and Electronics Engineers. Original specifications were defined in the year 1985. The current version includes improvements or corrections done in the year 2008.
18) What is the character encoding standard used in Java language?
A) ASCII
B) Unicode
C) Hexacode
D) Bytecode
Answer [=] C
Explanation:
Unicode takes 2 Bytes of memory to represent all characters of all languages.
19) What is the abbreviation of ASCII?
A) American Standard Characters for Information Interchange
B) Australian Standard Code for Information Interchange
C) American Standard Code for Information Interchange
D) None of the above
Answer [=] C
Explanation:
ASCII can represent only the English Alphabets and a few special symbols.
20) Choose the right statement about Java Data types.
A) Integer data types are short, int and long
B) Real number data types are float and double
C) The character data type is char.
D) All the above
Answer [=] D