One of the basics of Java - basic data types

foreword

Since I started working, I have forgotten a lot of the basic knowledge of Java in learning various technologies. ⊙﹏⊙b Khan. . . And the more you learn, the more you discover the importance of Java basics, so prepare to take time alone to re-learn Java basics. When I was re-learning, I had a different perception of these, so I prepared to record them all and organize them into a blog, hoping to help those in need.

Introduction to Basic Data Types

The basic data types are mainly these:
byte, short, int, long, float, double, char, boolean
can be divided into three categories:

  1. Numeric types: byte, short, int, long, float, double
  2. character type: char
  3. boolean: boolean

byte

  1. byte is an 8-bit data type, occupying 1 byte (8bit), the default value is 0, and its value range is (-2^7) ~ (2^7-1) , which is -128 ~ 127 . time, so the maximum amount of stored data is 255;
  2. Byte is generally used in large arrays instead of integers, because the space occupied by the byte variable is only 1/4 of the int.
  3. Example of byte usage: byte a = 10,byte=-10. When using the byte data type, you need to pay attention to the value range! ! !

short

  1. short is a 16-bit data type, occupying 2 bytes, the default value is 0, its value range is (-2^15) ~ (2^15-1) , that is , between -32768 ~ 32767 , so The maximum amount of data storage is 65536;
  2. Although short is 1/2 of the space occupied by int variables, it is rarely used in practice. Also saves space in large arrays.
  3. Example of using short: short a=100,short b=-200;

int

  1. int is a 32-bit data type, occupying 4 bytes, the default value is 0, its value range is (-2^31) ~(2^31-1) , that is , between -2147483648 ~ 2147483647 , so The maximum amount of data storage is 2^32-1 ;
  2. int is an integer data type, which is one of the most used data types in our projects;
  3. Example of int usage: int a=1000,int b=-2000;

long

  1. long is a 64-bit data type, occupying 8 bytes, the default value is 0L, its value range is (-2^63) ~ (2^63-1) , that is -9223372036854775808 ~ 9223372036854775808 , so The maximum data storage size is 2^64 ;
  2. long is the data type is a long integer, which is one of the most used data types in our projects. When using data of type long, it is best to put an uppercase L at the end of the value!
  3. Example of using long: long a=1000L, long b=-2000L;

float

  1. float is a 32-bit data type, occupying 4 bytes, the default value is 0, and its value range is between 3.4e-45 ~ 1.4e38 ;
  2. float is the data type is single precision, must add f or F after the number when assigning directly.
  3. Example of using float: float a=10.25f, float b=-20.35F;

double

  1. double is a 64-bit data type, occupying 8 bytes, the default value is 0, and its value range is between 4.9e-324 ~ 1.8e308 ;
  2. double is the data type is double precision, it is best to add D or d when assigning directly.
  3. Example of double usage: double a=10.123d, double b= -10.25644D;

boolean

  1. boolean is a boolean type, occupies 1 byte, has only two values, false and true, the default value is false.
  2. boolean can only use one flag to record true or false, generally used in combination with if.
  3. Example of using boolean: boolean a=true, boolean b=false;

char

  1. char is a character type, occupying 2 bytes, the default value is empty, and the value range is 0~65535 , that is, \u0000 ~ \uffff .
  2. The char data type can store any character.
  3. Example of char usage: char a=1, char b='A';

The levels of numeric types from low to high are:
byte, char, short (the three levels) --> int --> float --> long --> double
Among them, from low level to high level, it belongs to Automatic type conversion , which is automatically converted by the system.

For example, convert data of type int to data of type float.
Example:

int i=10;
float j=i;
System.out.println("i:"+i+",j:"+j);

result:

i:10,j:10.0

If you change from a high-level to a low-level, you need to cast , that is, cast.
For example, convert data of type int to data of type byte.
Example:

int i=127;
int j=128;
byte bye=(byte)i;
byte bye2=(byte)j;
System.out.println("i:"+i+",bye:"+bye);
System.out.println("j:"+j+",bye2:"+bye2);

result:

i:127,bye:127
j:128,bye2:-128

Explanation: Because the byte type is 8 bits, the maximum value is 127, so when the int is cast to the byte type, the value of 128 will cause overflow. Therefore, when performing forced type conversion, pay attention to the range of values!

You need to pay attention to the accuracy of the data when forcing conversion, otherwise there may be data loss .
For example, data of type double is converted to data of type float.
Example:

double d=10.1111115;
float f=(float) d;
System.out.println("f:"+f+",d:"+d);

result:

f:10.111112,d:10.1111115

After understanding the level of data types, if different data types are used for calculation, which is the final data type?
Here we use these types to test:
1.byte type data plus short type data;
2. Data of type short plus data of type int;
3. Data of type int plus data of type long;

Here we use this method to get the final data type

public static String getType(Object o){ 
        return o.getClass().toString(); 
    }

Code:

short a=1;
byte b=2;
int c = 3;
long d = 4;
System.out.println(getType(a));
System.out.println(getType(b));
System.out.println(getType(c));
System.out.println(getType(d));
System.out.println(getType(a+b));
System.out.println(getType(a+c));
System.out.println(getType(c+d));

result:

class java.lang.Short
class java.lang.Byte
class java.lang.Integer
class java.lang.Long
class java.lang.Integer
class java.lang.Integer
class java.lang.Long

Are you surprised to see the results? So why?
When the data of different numerical types are added, they will be automatically converted to the one with the highest level.
So why is short type data plus byte type data int type?
Because in the world of java, if the type smaller than the int type performs operations, java will cast them uniformly to the int type when compiling.
If the above is not easy to understand, then here we will do an experimental test and verify it through the results.
Example:

short a=1,b=2;
byte c=3,d=4;
System.out.println(getType(a+b));
System.out.println(getType(c+d));

result:

class java.lang.Integer
class java.lang.Integer

Then it can be concluded here that in the numerical type, if the level is less than int, the final data type will be automatically converted to int when calculating, and if it is higher than int, the final data result will take the highest one!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325633799&siteId=291194637
Recommended