java type casting_Type Conversion in Java | Types of Type Casting in Java

1. Objective

In this Java with the help of various examples. We are also going to study the different types of type casting in Java with example that are available to us.

So, let us start Type Conversion in Java.

ab2c7a2cd5f85f2b27639d3efa703b16.png

What is Type conversion in Java | Types of Type casting in Java

2. Type Conversion in Java

When we need to assign the values of one data type to another and there might be a possibility that the two types are not compatible to each other, then Java does the type casting for us if it can be done automatically, and if not we need to do it explicitly.

3. Types of Type Casting in Java

Here, we discuss 4 types of Type casting in Java, let’s discuss them one by one:

a. Widening or Automatic Type Conversion in Java

Widening conversion happens when two information composes are consequently changed over. This happens when:

The two information composes are perfect.

When we allow estimation of a littler information writes to a greater information compose.

For Example, in java the numeric information composes are good with each other however no programmed change is bolstered by numeric kind to roast or boolean. Additionally, roast and boolean are not good for each other.

78cef4138ef7e7cbefdc315e043dc2e1.png

Widening or Automatic Type Conversion in Java

Example –

class Test

{

public static void main(String[] args)

{

int i = 100;

long l = i;

float f = l;

System.out.println("Int value "+i);

System.out.println("Long value "+l);

System.out.println("Float value "+f);

}

}

b. Narrowing or Explicit Conversion in Java

f3b5614d1a8549ad59cfa20dc5306c23.png

Narrowing or Explicit Conversion

Occasionally there is a chance that we need to allot an estimation of bigger information write to a little information compose we perform express compose throwing or narrowing.

This is helpful for contradictory information writes where programmed change is impossible.

Here, target-type determines the coveted kind to change over the predetermined an incentive to char to long.

Example –

public class Test

{

public static void main(String[] argv)

{

char ch = 'c';

int num = 88;

ch = num;

}

}

Output-

7: error: incompatible types: possible lossy conversion from int to char

ch = num;

^

1 error

Read about Java Exception Handling in detail

Explicit Conversion Example –

class Test

{

public static void main(String[] args)

{

double d = 100.04;

//explicit type casting

long l = (long)d;

//explicit type casting

int i = (int)l;

System.out.prinln("Double value "+d);

System.out.println("Long value "+l);

System.out.println("Int value "+i);

}

}

c. Type Promotion in Expressions

While assessing statements, the middle of the road esteem may surpass the scope of operands and subsequently, the articulation esteem will be advanced. A few conditions for type advancement are:

Java consequently advances every byte, short, or single operand to the int while assessing an articulation.

On the off chance that one operand is a long, float or double the entire articulation is elevated too long, buoy or twofold separately.

Example –

class Test

{

public static void main(String args[])

{

byte b = 42;

char c = 'a';

short s = 1024;

int i = 50000;

float f = 5.67f;

double d = .1234;

double result = (f * b) + (i / c) - (d * s);

System.out.println("result = " + result);

}

}

d. Explicit Type Casting in Expressions

While assessing statements, the outcome is naturally refreshed to bigger information sort of the operand. In any case, in the event that we store that outcome in any littler information compose it creates aggregate time blunder, because of which we have to type cast the outcome.

Example –

class Test

{

public static void main(String args[])

{

byte b = 50;

b = (byte)(b * 2);

System.out.println(b);

}

}

This was all about Type Conversion in Java Tutorial. Hope you like our explanation on Types of Type casting with example.

4. Conclusion

Hence, in this tutorial, we studied about What is Type conversion in Java and types of type casting in Java: Widening or Automatic Type Conversion in Java, Narrowing or Explicit Conversion in Java, Type Promotion in Expressions, and Explicit Type Casting in Expressions with examples. Furthermore, if you have any query about Type conversion in Java or Type casting in Java feel free to ask in a comment section.

For reference

注意:本文来自DataFlair。本站无法对本文内容的真实性、完整性、及时性、原创性提供任何保证,请您自行验证核实并承担相关的风险与后果!

CoLaBug.com遵循[CC BY-SA 4.0]分享并保持客观立场,本站不承担此类作品侵权行为的直接责任及连带责任。您有版权、意见、投诉等问题,请通过[eMail]联系我们处理,如需商业授权请联系原作者/原网站。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值