Learn Java Tutorial for Beginners, Part 32: Casting Numerical Values

preview_player
Показать описание
In this video by Quordnet Academy for the series Learn Java Tutorial for Beginners how to use Casting Numerical Values have been discussed.
If you would like to discover even more about java programming tutorial or the java complete course I advise you to check out the playlist :

Type conversion in Java with Examples
When you assign value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion and if not then they need to be casted or converted explicitly. For example, assigning an int value to a long variable.

Widening or Automatic Type Conversion

Widening conversion takes place when two data types are automatically converted. This happens when:

The two data types are compatible.
When we assign value of a smaller data type to a bigger data type.
For Example, in java the numeric data types are compatible with each other but no automatic conversion is supported from numeric type to char or boolean. Also, char and boolean are not compatible with each other.
Widening or Automatic Type Conversion
Example:
class Test
{
public static void main(String[] args)
{
int i = 100;

// automatic type conversion
long l = i;

// automatic type conversion
float f = l;
}
}
Output:

Int value 100
Long value 100
Float value 100.0
Narrowing or Explicit Conversion

If we want to assign a value of larger data type to a smaller data type we perform explicit type casting or narrowing.

This is useful for incompatible data types where automatic conversion cannot be done.
Here, target-type specifies the desired type to convert the specified value to.
Narrowing or Explicit Conversion

char and number are not compatible with each other. Let’s see when we try to convert one into other.
//Java program to illustrate incompatible data
// type for explicit type conversion
public class Test
{
public static void main(String[] argv)
{
char ch = 'c';
int num = 88;
ch = num;
}
}
Error:

7: error: incompatible types: possible lossy conversion from int to char
ch = num;
^
1 error
How to do Explicit Conversion?
Example:

//Java program to illustrate explicit type conversion
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;

//fractional part lost

//fractional part lost
}
}
Output:

Double value 100.04
Long value 100
Int value 100

Possibly if you have doubt comment below and let me understand what else I can help you with information in java.This playlist is a full fledged java programming for beginners and java tutorial for beginners which is given above.
Please share with your friends the video to assist other people looking for java programming tutorial or object oriented programming java .
To never miss an update from or channel hit the subscribe button first and if already subscribe hit the bell icon.
1.Follow us on INSTAGRAM for Interesting posts
2.Follow us on LINKEDIN for interesting content on different aspects
3.Don't forget to like our FACEBOOK to get the most out of it
4.Follow us on twitter to get a mix of all
5.If you want to get us on TUMBLR please then click on the link given below
6.Do join our OFFICIAL Telegram for notes of different things
7.For get job update regularly both private and government do join this telegram channel
#quordnetacademy, #java_tutorial_series, #javatutorialseries
Рекомендации по теме