Getting Started with Java [a] data type conversions - Integer

I, on the java language among the integer

type of data       take up space  Defaults   Ranges
byte   1 0  [-128~127]
short        2   0  [-23768~23767]
int      4   0  
long      8   0L  [-2147483648~2147483647]


1, java language among integer literals by default as an int type to handle. To make this integer literals are treated as type long to deal with the case, you need to add 1 / L after the integer literals, it is recommended to use a capital L


2, java language among the three plastic literal representation:


    The first way: decimal [a way of] default defaults
    second way: [octal need to zero in the preparation of octal integer literals]
    third way: Hex [in writing hexadecimal integer literals is when you need to write 0x start]
 

public class DataTypeTest04
{
    public static void main(String[] args){
        int a=10;
        int b =010;
        int c=0x10;

        System.out.println(a);//10
        System.out.println(b);//8
        System.out.println(c);//16

        System.out.println(a+b+c);//34   
    }
}

FIG Output:

  Second, the basic data type conversion  

1. integer conversion

    // 123 This integer literals are of type int
    // when i type int variable declarations also
    // int type 123 is assigned to variable of type int i, there is no type conversion  

public class DataTypeTest04
{
    public static void main(String[] args){
          int i=123;
        System.out.println(i);
    }
}

  456 // integer literals are treated as type int, 4 bytes.
  // x variable in a statement when the type is long, 8 bytes.
  // int type literal 456 assigned to the long type variable x, the presence of type conversion,
  conversion types as a long int //
  // int type small capacity
  @ long type large-capacity
  @ small capacity can be automatically converted into mass, called an automatic type conversion mechanism.

public class DataTypeTest04
{
    public static void main(String[] args){  

        long x=456;
        System.out.println(x);
    }
}

 // 2147483647 literal of type int, 4 bytes
 // Y type long, 8 bytes, automatic type conversions.

public class DataTypeTest04
{
    public static void main(String[] args){

        long y=2147483647;
        System.out.println(y);

    }
}

2. Error

// compile error: integer too large:
  // 2147483648 is treated as int four bytes processed, but beyond the literal scope of type int
  // long z = 2147483648;

public class DataTypeTest04
{
    public static void main(String[] args){
        long z=2147483648;
        System.out.println(z);

    }
}

Error Figure:

  3. resolve errors
  // 2147483648 literal up to as long a process type, added after literal L
  // 2147483648L is 8 bytes long in this
  // Z is a long type variable, the program conversion type does not exist
 

public class DataTypeTest04
{
    public static void main(String[] args){
        long z=2147483648L;
        System.out.println(z);

    }
}

 

Published 142 original articles · won praise 67 · views 190 000 +

Guess you like

Origin blog.csdn.net/yyp0304Devin/article/details/105328471