Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java type casting

// 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!
Comment

Java Type Casting

Narrowing Casting (manually) - larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte 
Comment

Java Type Casting

public class Main {
  public static void main(String[] args) {
    int myInt = 9;
    double myDouble = myInt; // Automatic casting: int to double

    System.out.println(myInt);      // Outputs 9
    System.out.println(myDouble);   // Outputs 9.0
  }
}
Comment

java casting method

Animal animal = new Dog ();
animal.fetch(); // Compiler error
(Dog) animal.fetch();
Comment

java type casting

Lowest to highest primitive data types: Byte-->Short-->Int-->Long-->Float-->Double
Comment

casting in java

Byte-->short-->char-->Int-->long-->float-->double
Comment

PREVIOUS NEXT
Code Example
Java :: docker java image 
Java :: how to find some of digits in java 
Java :: java iterator to stream 
Java :: java set foreach 
Java :: test a javafx project 
Java :: find length of array java 
Java :: binary to decimal in java program 
Java :: java.lang.string cannot be cast to java.lang.double react native 
Java :: java generate uuid 
Java :: Count Occurrences in Seven Integers Using Java Single Dimension Arrays 
Java :: simple java code 
Java :: android studio edittext enter pressed 
Java :: write files with FileOutPutStream java 
Java :: read timed out android studio 
Java :: java get month 
Java :: multiplication program java 
Java :: is a and has a relationship in java 
Java :: sort comparator in java 
Java :: java class jar determine 
Java :: android textview center align text programmatically 
Java :: java vector push_back 
Java :: how to use while loop in java 
Java :: regex pattern for bank cards validation 
Java :: javafx arabic letters 
Java :: find highest value in keyset java 
Java :: java extends 
Java :: java arraylist remove 
Java :: Java Create an OutputStream 
Java :: java ignore catch 
Java :: how to initialize a string in java 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =