Date 04-08-2016
Data types :-
java is strictly a typed programming language, where in java
applications, before representing data first, we have to confirm which type of
data we are representing, to specify the type and data we have to use “data
type”.
I java applications, data type are able to provide the
following advantages.
1.
We are able to identify memory size to store
data
2.
We are able identify range value for the
variables to assign
To design java application, java has provide the following
data types
1.
Primitive data types/primary data types:
1.
Number data types:-
1.
Integer data type /integral data types
byte
|
1 byte
|
0
|
short
|
2 bytes
|
0
|
int
|
4 bytes
|
0
|
long
|
8 bytes
|
0
|
2.
Non-integral data type :-
float
|
4 bytes
|
0.0f
|
double
|
8 bytes
|
0.0
|
1.
User defined data types/secondary data types:
All classes, interface, arrays……………………………
Note:
-
The defined value for all user defined data
type is “null” value.
To calculate range values for the data types,
we have to use the following formule.
n-1 n-1
-2 to
2 -1
Where ‘n’ is number of bits.
Ex:-
Byte:-
Size
: 1byte =8 bits
8-1 8-1
-2 to
2 -1
7 7
-2 to
2 -1
-128 to
127
To get minimum value and maximum value for each and
every data types, we have to use the following content from all the wrapper
classes.
1. MIN_VALUE
2. MAX_VALUE
Note :-
Class
representation of primitive data types are called as “wrapper classes”.
Primitive
|
Wrapper classes
|
byte
|
java.lang.Byte
|
short
|
java.lang.Short
|
int
|
java.lang.Integer
|
long
|
java.lang.Long
|
float
|
java.lang.Float
|
double
|
java.lang.Double
|
char
|
java.lang.Character
|
boolean
|
java.lang.boolean
|
Ex:-
class Test
{
public static void main(String args[]) {
System.out.println(Byte.MIN_VALUE+"--->"+Byte.MAX_VALUE);
System.out.println(Short.MIN_VALUE+"--->"+Short.MAX_VALUE);
System.out.println(Integer.MIN_VALUE+"--->"+Integer.MAX_VALUE);
System.out.println(Long.MIN_VALUE+"--->"+Long.MAX_VALUE);
System.out.println(Float.MIN_VALUE+"--->"+Float.MAX_VALUE);
System.out.println(Double.MIN_VALUE+"--->"+Double.MAX_VALUE);
System.out.println(Character.MIN_VALUE+"--->"+Character.MAX_VALUE);
// System.out.println(Boolean.MIN_VALUE+"--->"+Boolean.MAX_VALUE);
//Error
}
}
Output: -
-128 -à127
-32768à32767
-2147483648à147483647
-9223372036854775808à9223372036854775807
1.4E-45à3.4028235E38
4.9E-324à1.7976931348623157E308
----?
Type casting:-
The process of
converting data from one data type to another data type is called “ type
casting”.
There are two
types of type casting in java
1.
Primitive data
types type casting
2.
User data types
type casting
The process of
converting data from one user defined data type to another user defined data
type is called as user defined data types type casting.
To prepare user
defined data type casting, we have to use either “extends” relation
“implements” relation between user defined data types.
1.
Primitive data
types type casting :-
The process of converting data from
one primitive data type to another primitive data type is called as primitive
types type casting.
There are two types of primitive data
types type casting
1.
Implicit type
casting
The process of converting data from
lower data types to higher data type is called as “implicit type casting”.
To cover all the possibilities of
implicit type casting, we have to use the following chart.
byte-->short-->int-->long-->float-->double
|
|
char
Ex:-
class Test
{
public static void main(String args[]) {
byte b=10;
int i=b;
System.out.println(b+" "+i);
}
}
Status: no
compilation error
Output: 10 10
Date 05-08-2016
When we compile
the above program, when compiler encounter the above assignment statement,
compiler will check whether right side variable data type is compile with left
side variable data type or not, if not, compiler will rise an error like
“possible lass of precision”. If right side variable data type is compatible
with left side variable data types, then compiler will not rise an error and compiler
will not perform type casting.
When we execute
above instructions, when JVM encounter the above assignment statement, JVM will
perform the following two actions.
1.
JVM will convert
right side variable data type to left side variable data type internally [
implicit type casting].
2.
JVM will copy
right side variable value to left side variable.
Ex:-
class Test
{
public static void main(String args[])
{
int i=10;
byte b=i;
System.out.println(i+""+b);
}
}
Status Compiler Error, possible loss of precision
Ex:-
class Test
{
public static void main(String args[])
{
byte b=65;
char c=b;
System.out.println(b+""+c);
}
}
Status Compiler Error, possible loss of precision
Ex:-
class Test
{
public static void main(String args[])
{
char c='A';
short s=c;
System.out.println(c+""+s);
}
}
Status Compiler Error, possible loss of precision
Reason:-
NO implicate conversion existed between byte and char, short and char
Ex:-
class Test
{
public static void main(String args[])
{
char c='A';
int i=c;
System.out.println(c+" "+i);
}
}
Status NoCompiler
Error
Output
A 65
Ex:-
class Test
{
public static void main(String args[])
{
byte b=128;
System.out.println(b);
}
}
Status Compiler Error, possible loss of precision
Reason:- if we Assign any value to a variable which is
grater then the MAX Value of the respective data type then compiler will
treated that value of the next higher data type and compiler will rise an error
“possible loss of precision”.
Ex:-
class Test
{
public static void main(String args[])
{
float f=22.22f;
long l=f;
System.out.println(f+" "+l);
}
}
Status Compiler Error, possible loss of precision
Ex:-
class Test
{
public static void main(String args[])
{
long l=20;
float f=l;
System.out.println(l+" "+f);
}
}
Status No Compiler Error
Output :
20
20.00
Reason :
in java even float is taking 4 bytes at memory and long is taken 8 bytes of
memory. Still, float data type is able to store more data when compiled with
long data type as per the their internal data representations, so that float
data type in higher data type when compiled with long data type, so that, we
are able to assign long data type variable to float data type variable, we are
unable to assign float data type variable to long data type variable.
Ex:-
class Test
{
public static
void main(String args[])
{
byte b1=60;
byte b2=70;
byte b=b1+b2;
System.out.println(b);
}
}
Status Compiler Error, possible loss of precision
Reason :-
>>x,y and z
are three primitive data types
>>x+y=z
1.
If x and y are
belong to { byte, short, int} the z should be int.
2.
If either x and y
or both belong to {long, float, double} then z should be higher (x,y) as per
implicit type casting chart.
Ex:-
>>byte+byte=int
>>byte+short=int
>>short+int=int
>>short+long=long
>>float+long=float
>>int+float=
float
>>float+double=double
------
------
Explicit type casting :-
The process of
converting data from higher data type to lover data type is called as explicit
type casting.
To perform
explicit type casting in java application we have to use the following
P a=(Q) b;
Where Q must be
either same as P or lower then P.
Ex:-
class Test
{
public static void main(String args[])
{
int i=10;
byte b=(byte)i;
System.out.println(i+" "+b);
}
}
Status NO
Compiler Error
Output :
10 10
When we compile
the above program, when compiler encounter the assigned statement, compiler
will check whether the right side data type is compiler with left side data
type or not, if not, compiler will rise an error like “possible loss of precession”.
if right side data type is compiler with
left side data type then compiler will not rise any error and compiler will not
perform any casting.
When we execute
the above code when JVM encounter the above assignment statement JVM will
perform the following two actions.
1.
JVM will
converter right side variable data type to the cast operator provided data
type.
2.
JVM will copy the
value from right side variable to left side variable.
Ex:-
class Test
{
public static void main(String args[])
{
int i=10;
short s=(short)i;
System.out.println(i+" "+s);
}
}
Status NO
Compiler Error
Output :
10 10
Ex:-
class Test
{
public static void main(String args[])
{
byte b=65;
char c=(char)b;
System.out.println(b+" "+c);
}
}
Status NO
Compiler Error
Output :
65 A
Ex:-
class Test
{
public static void main(String args[])
{
char c='A';
short s=(byte)c;
System.out.println(c+" "+s);
}
}
Status NO
Compiler Error
Output :
A 65
Date
06-augest-2016
Ex:-
class Test
{
public static
void main(String args[])
{
short s=65;
char c=(byte)s;
System.out.println(s+" "+c);
}
}
Status Compiler Error, possible loss of precision
Note :-
In implicit type casting,
no conversion are existed between byte and char but, in explicit type casting
conversion are existed between byte and char, short and char
Ex:-
class Test
{
public static void main(String args[])
{
int i=128;
byte b=(byte)i;
System.out.println(i+" "+b);
}
}
Status NO
Compiler Error
Output :
128 -128
Reason :
Ex:-
class Test
{
public static void main(String args[])
{
float f=22.22f;
long l=(long)f;
System.out.println(f+" "+l);
}
}
Status NO
Compiler Error
Output :
22.22 22
Ex:-
class Test
{
public static void main(String args[])
{
byte b1=30;
byte b2=40;
byte b=(byte)b1+b2;
System.out.println(b);
}
}
Status Compiler
Error, possible loss precision.
Ex:-
class Test
{
public static
void main(String args[])
{
byte b1=30;
byte b2=40;
byte b=(byte)(b1+b2);
System.out.println(b);
}
}
Status NO
Compiler Error
Output :
70
Ex:-
class Test
{
public static void main(String args[])
{
double d=22.22;
byte
b=(byte)(short)(int)(long)(float)d;
System.out.println(b);
}
}
Status NO
Compiler Error
Output :
22

No comments:
Post a Comment