AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Java Type Casting


Java Tutorial » Java Type Casting

Converting the value of one data type as int, long, float, double, etc. to another data type named typecasting.
There are 13 types of type conversion in Java

In this tutorial, we target on the major 2 types.
1. Widening Type Casting - (automatically) - converting a smaller type to a larger type size
2. Narrowing Type Casting - (manually) - converting a larger type to a smaller size type

Widening Type Casting


Example: Converting int to double
public class Main {
public static void main(String[] args) {
// create int type variable
int my_number = 24;
System.out.println("The integer value of my number is: " + my_number);

// convert into double type
double data = my_number;
System.out.println("The double value of my number: " + data);
}
}
Output:
The integer value of my number is: 24
The double value of my number: 24.0


Narrowing Type Casting


Example: Converting double into an int
public class Main {
public static void main(String[] args) {
// create double type variable
double my_number = 24.98;
System.out.println("The double value of my number: " + my_number);

// convert into int type
int data = (int)my_number;
System.out.println("The integer value of my number: " + data);
}
}
Output:
The double value of my number: 24.98
The integer value of my number: 24


Converting the value of one data type (short, char, int , float , double , etc.) to another data type is known as typecasting.

Java Type Casting objects, string to int, example, rules, double to int, parent to child, syntax, object, error
Java Type Casting - java

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...
jQuery slideUp()...
jQuery fadeToggle...
jQuery fadeOut()...
jQuery fadeIn()...
jQuery Effects - hide () and show ()...
jQuery Events...
jQuery Attributes...
jQuery Selectors...