Type Conversions

 Data Types

Primitive Data Types:

1) byte 2) short 3) int 4) long 5) float 6) double 7) char 8) boolean

Type Conversions:-

  • Conversion one type to another type is called type conversions.
  • types of conversion are divided in to two types
1.Widening or Automatic Type Conversion  (Implicitly)

2.Narrowing Type Conversion  (Explicitly )


over all 56 conversions (8*7=56)

  • boolean type cannot be converted to other types and other types cannot be converted to boolean type.=14(not Possible)
Widening or Implicitly Type Conversions:-

The following 19 conversions are done by system implicitly:
byte to short, int, long, float, double   => 5
short to int, long, float, double            => 4
int to long, float, double                       => 3
long to float, double                              => 2
float to double                                       => 1
char to int, long, float, double             => 4
                                                    =============
                                                     Total  => 19
                                                  ==============

Narrowing or Explicitly Type Conversions:-

The following 23 conversions are must be done by programmer explicitly otherwise compile time error occurs:

byte to char                                                   => 1
short to byte, char                                        => 2
int to byte, short, char                                 => 3
long to byte, short, int, char                        => 4
float to byte, short, int, long, char              => 5
double to byte,short,int,long,float,char     => 6
char to byte, short                                        => 2
                                                              ==============
                                                      Total     => 23   
                                                             =============
Examples

If we compile and run the following code snippets, what will be happen?

1.int true=10;

System.out.println(true)

2.char a=’A’;

int b=a;

System.out.println(b)

3.byte a = 10;

Short b = a;

System.out.println(b);







Comments

Popular posts from this blog

Inheritance in Java

Life cycle of a Thread and creating thread class