Sunday, June 12, 2011

JAVA : conversion and casting's data type


download  materi conversion and casting's data type

1. type conversion

we are ordinary do point application one type with other type. if two that type kompatibel therefore Java will do self acting conversion.

example:
if we assign value int goes to variable get long's type.

are not all type mutually kompatibel.

example:
no conversion which is defined from double goes to byte.

We make a abode to be still get to get type conversion that don't kompatibel manually, we get ready to take on accordingly. We do casting. We shall know consequence who may happen so we handle it in order not to begets rioting.

2. convert auto type

while one data type at gives to other data type variable therefore Java will do self acting type conversion if pock two following requisites:
1. Two type is kompatibel
2. Fairish intent type greater at source type appeal

While requisite second tesebut is accomplished, convert enlargement will do self acting Java.

example:
int's type always big have which suffice to keep all all point byte that legitimate. For int's variable case is filled with byte, we not necessarily do casting explicit ala.

Convert enlargement will at do on integer and dot number floats that kompatibel one by other. Type numerik not kompatibel with char or boolean. char's type and boolean is not mutually kompatibel.

Java also self acting does to convert type while keep literal integer goes to variable get byte's type, short, or long.

3. Casting

Available our possible wants to thrust conversion one type goes to other type. Process this enforcing at casting's mention. Casting often at requires while function transfer type in contrast to type which required by operation. casting's action at do with place type that at expects in couple sign boxs in at appreciative left that wants at conversion.

Sintaks casting as follows:

 (target-type) value

type's target define type that at wants deep conversion go to value's type.

example:

casting from int goes to byte
int anInt;
byte aByte;

// ...
...
aByte = (byte) anInt;

If integer point is even greater to be appealed range who can keep all byte, therefore Java mereduksi will go to rest for with range from byte.

purpose example:
read's function() at stream's input standard (System.in) transferring int. We get to do casting to int's type becomes char's type before storage:

char c = (char) ystem. in. read();

System.in.read's function() transferring int's point, then done by casting goes to char with (char)  char's result, that result at keeping at variable c gets char's type.

Stored size science important deep determine casting's end product. Are not all casting can save data in advance safe ala. On casting long's type becomes int long's matter 64 bit becomes int's types 32 bits. While is casting as it is done, kompilator cuts 32 long's type bit becomes int 32 bits. If point 32 above bits contain essential information therefore that information is lost. Information can also get lost while we do casting among given different types even its depositor measures with.

example:
Casting is double's number becomes long will remove fraction information so just gets a part double's round even both fairish 64 bits.

Conversion that result truncation's so-called cut.

Following table is casting that at secures its result no that lost.
Tipe Asal
Tipe Tujuan
Byte
Short, char, int, long, float, double
Short
Int, long, float,double
Char
Int, long, float.,double
Int
Long, float, double
Long
Float, double
Float
Double

download materi conversion and casting's data type

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More