Home > database >  How to convert a char to a long without first making it an int?
How to convert a char to a long without first making it an int?

Time:12-18

Char, int and long are all integers, but with other large of bits.

I know that if you create a long from an integer for example:

int a=39; long b=a;

that in the long is not a number with 64 bits, it is also a integer with 32 bits.

When we make:

int a=39; long b=aL;

I have read that if you add for example an L to the long, that then at the long is also a number with 64 bit. But I read that Java internally sees the integer before as an integer and by the L the number then becomes a 64 bit. But before that the number is also stored in the long as 32 bit for a short time.

So I have read that in general always first an integer with 32 bits is formed in long and then this is made to 64 bit. So even if someone makes long test =93L;, in the long first an integer with 32 bits is created, which is extended afterwards with the L and thereby becomes 64 bit. So as an intermediate step you always have an integer. Is that correct?

If yes, how can I convert a char for example char test1 ='A' to a long without making them before to a 32bit integer? Because if I am right, if I make long test2=test1; there is no long/integer with 64 bits in the long, there is only one with 32 bits. Is that true?

Or is it that now in the long is an integer with 64 bits? If so was it 32 bits before for a short moment?

If what I say is true, how can I make a char directly 64 bits without it being an int for a short moment first?

CodePudding user response:

Let's try with a test program

public class Test {
    public static void main(String args[]) {
        long a = 'a';
    }
}

Let's compile it

javac Test.java

Let's disassemble it

javap -c Test.class
Compiled from "Test.java"
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: ldc2_w        #2                  // long 97l
       3: lstore_1
       4: return
}

See this:

0: ldc2_w        #2                  // long 97l

It's loading 97 (the numerical value of 'a') directly as a long. There's no intermediate step in which your value is 32 bits wide.

If you don't do this directly, of course you get a different result, i.e. the number is stored as a 32-bits int first and then loaded into a long (but you're explicitly asking for that by declaring a as a char, so that's not surprising)

public class Test {
    public static void main(String args[]) {
        char a = 'a';
        long b = a;
    }
}

Let's decompile this

javap -c Test.class
Compiled from "Test.java"
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: bipush        97
       2: istore_1
       3: iload_1
       4: i2l
       5: lstore_2
       6: return
}

CodePudding user response:

Long is 64 bit.

When you assign an int value to a long variable, the value is converted to long.

        long a = 5;  // same as long a = (long) 5;
        long b = 5L;
  • Related