Java Convert char to long

Tags: char long

In this Java core tutorial we learn how to convert a char value into long value with different solutions in Java programming language.

Table of contents

  1. Assign char variable to long variable in Java
  2. Using Long.valueOf() method

Assign char variable to long variable in Java

In this first solution, we can convert char to long by simply assign a char variable directly to a long variable as the following Java program.

ConvertCharToLongExample1.java

public class ConvertCharToLongExample1 {
    public static void main(String... args) {
        char charValue = 'T';

        // Assign char variable to long variable
        long longValue = charValue;

        System.out.println("char value: " + charValue);
        System.out.println("long value: " + longValue);
    }
}
The output as below.
char value: T
long value: 84

Using Long.valueOf() method

In this second solution, we can use the Long.valueOf(long l) static method to convert a char value to long value as the example Java code below.

ConvertCharToLongExample2.java

public class ConvertCharToLongExample2 {
    public static void main(String... args) {
        char charValue = 'A';

        // Convert char to long
        long longValue = Long.valueOf(charValue);

        System.out.println("char value: " + charValue);
        System.out.println("long value: " + longValue);
    }
}
The output as below.
char value: A
long value: 65

Happy Coding 😊

Java Convert byte to long

Java Convert long to char

Java Convert long to byte