Question: What is datatype?
Answer – Datatype is a set of values that has an associated set of operations and a memory representation for its values.
S.
NO.
|
DATA
TYPE
|
SIGN
IN BYTES
|
DEFAULT
VALUES
|
1.
|
char
|
2
|
\0 (Null Character)
|
2.
|
byte
|
1
|
0
|
3.
|
short
|
2
|
0
|
4.
|
int
|
4
|
0
|
5.
|
long
|
8
|
0
|
6.
|
float
|
4
|
0
|
7.
|
double
|
8
|
0
|
8.
|
boolean
|
Not defined
|
false
|
- class Test1
- {
- public static void main(String args[])
- {
- int a;
- System.out.println("a = “ + a); // Compilation error
- }
- }
Output -
Explanation-
v Concept of default value is not applied on local
variables of method i.e. local variables of a method need to be explicitly
assigned.
v Default value will be applied in class variables.
Like for example the above program can be rewritten as-
- PROGRAM Test1.java
- class Test1
- {
- static int a, b;
- public static void main(String args[])
- {
- System.out.println("a & b is : " + a + " " + b); // No compilation error
- }
- }
OUTPUT –
- PROGRAM Test2.java
- class Test2
- {
- public static void main(String args[])
- {
- float f = 12.45;
- System.out.println("Value of f = " + f);
- }
- }
What
will be the output –
A.
Value of f = 12.45
B.
Value of f = 12
C.
Value of f = 12.00000
D.
Compilation Error (output)
Now let’s see the output of the above program.
Explanation –
In Java, floating point constants are of double type by default. The
value of higher type cannot be directly assigned to a lower type. In such a
case type conversion is required.
No comments:
Post a Comment