java type casting
Java
// You can typecast to convert a variable of one data type to another.
// Wide Casting converts small data types to larger ones.
// Narrow Casting converts large data types to smaller ones.
// Java can automatically Wide Cast.
// Java will throw an error when trying to automatically Narrow Cast.
// This is because data is often lost when Narrow Casting.
// Narrow Casting must be done manually.
//Wide Cast:
int SomeNumber = 5;
double WideCastedNumber = (double)SomeNumber;
//Narrow Cast:
double SomeNumber = 5.39;
int NarrowCastedNumber = (int)SomeNumber;
//Note: The data that holds the decimal places will be lost!public class MyClass {
public static void main(String[] args) {
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println(myDouble); // Outputs 9.78
System.out.println(myInt); // Outputs 9
}
}
Also in Java:
- Title
- how to install java 8 in ubuntu 16.04
- Category
- Java
- Title
- android get distance between two locations kotlin
- Category
- Java
- Title
- java convert char to int
- Category
- Java
- Title
- random processing
- Category
- Java
- Title
- android create notification
- Category
- Java
- Title
- how to print ascii value in java
- Category
- Java
- Title
- java generic calling clone method
- Category
- Java
- Title
- enum with numbers java
- Category
- Java
- Title
- add element to stack java
- Category
- Java
- Title
- java calendar add minutes
- Category
- Java
- Title
- how to pass parameters to xsl file
- Category
- Java
- Title
- primitive and non primitive data types in java
- Category
- Java
- Title
- converting char array to string
- Category
- Java
- Title
- onbackpressed android
- Category
- Java
- Title
- java getter
- Category
- Java
- Title
- sort elements with sortedset
- Category
- Java
- Title
- how to reverse a list in java
- Category
- Java
- Title
- enum in java
- Category
- Java
- Title
- Java array hashcode
- Category
- Java
- Title
- java sort method
- Category
- Java
- Title
- integer to string java
- Category
- Java
- Title
- android switch on change
- Category
- Java
- Title
- how to add a keylistener to a jframe
- Category
- Java
- Title
- initialize scanner java
- Category
- Java
- Title
- java command line arguments
- Category
- Java
- Title
- java simple jframe example
- Category
- Java
- Title
- initiralizing an arraylist java
- Category
- Java
- Title
- how to add to a file in java
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- int to string java
- Category
- Java
- Title
- immagini java
- Category
- Java
- Title
- fibonacci series i logn java
- Category
- Java
- Title
- how to do for each in java
- Category
- Java
- Title
- java quit application
- Category
- Java
- Title
- java optional to collection
- Category
- Java
- Title
- java pass arraylist by value
- Category
- Java
- Title
- java read integer from text file into array scanner
- Category
- Java
- Title
- binary string to int java
- Category
- Java
- Title
- java shortest if else statement
- Category
- Java
- Title
- snackbar not showing with bottom nav
- Category
- Java
- Title
- raise number to power java
- Category
- Java
- Title
- separateur JMenu swing java
- Category
- Java
- Title
- how to use lambda in java
- Category
- Java
- Title
- spigot actionbar
- Category
- Java
- Title
- java random number generator in range
- Category
- Java
- Title
- java animated gif example
- Category
- Java
- Title
- java read from file
- Category
- Java
- Title
- java read file from command line argument
- Category
- Java
- Title
- goodbye java
- Category
- Java
- Title
- java regex replace all characters before
- Category
- Java
- Title
- selection sort in java
- Category
- Java
- Title
- java length of matrix
- Category
- Java
- Title
- Unrecognized option: --version Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.
- Category
- Java
- Title
- exponents java
- Category
- Java
- Title
- how to exit a for loop in java
- Category
- Java
- Title
- java random number in range
- Category
- Java
- Title
- java arraylist in enum
- Category
- Java
- Title
- how to check if rs next is null
- Category
- Java
- Title
- numberformatexception
- Category
- Java
- Title
- java map declaration
- Category
- Java
- Title
- how to create an action listener in java
- Category
- Java
- Title
- how to make a fixed size array in java
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- Removing DOM nodes when traversing a NodeList
- Category
- Java
- Title
- java get keys from hashmap
- Category
- Java
- Title
- java execute jar from main
- Category
- Java
- Title
- java foreach
- Category
- Java
- Title
- scanner check if int
- Category
- Java
- Title
- print in one line in java
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- java queue
- Category
- Java
- Title
- center textview programmatically android
- Category
- Java
- Title
- send variable intent
- Category
- Java
- Title
- get first character of string java
- Category
- Java
- Title
- refrence xml textfield in javafx
- Category
- Java
- Title
- download spring
- Category
- Java
- Title
- java get current milliseconds
- Category
- Java
- Title
- java generate list of random element
- Category
- Java
- Title
- java localdate subtract two dates
- Category
- Java
- Title
- how to create a 2d arraylist java
- Category
- Java
- Title
- java newinstance alternative
- Category
- Java
- Title
- java create circle
- Category
- Java
- Title
- how to create a thread local variable in java
- Category
- Java
- Title
- how to convert char to uppercase java
- Category
- Java
- Title
- java go troght loop object
- Category
- Java
- Title
- saving String character in arraylist
- Category
- Java
- Title
- java string to lower case
- Category
- Java
- Title
- java lambda list of objects cast
- Category
- Java
- Title
- java for
- Category
- Java
- Title
- basic java coding
- Category
- Java
- Title
- arraylist of double
- Category
- Java
- Title
- java switch case
- Category
- Java
- Title
- java method to capitalize first letter
- Category
- Java
- Title
- iterate over map keys java
- Category
- Java
- Title
- java get command line output
- Category
- Java
- Title
- java how to show an array
- Category
- Java
- Title
- java set value of arraylist
- Category
- Java
- Title
- import scanner
- Category
- Java
- Title
- in java how to compare two strings
- Category
- Java
- Title
- java while loop example
- Category
- Java