Home >Java >Javagetting Started >How many bytes (bits) do each data type in java occupy?

How many bytes (bits) do each data type in java occupy?

王林
王林Original
2019-11-25 13:06:043715browse

How many bytes (bits) do each data type in java occupy?

There are 8 basic data types in Java: byte, int, short, long, boolean, char, float, double

The corresponding classes are: Byte, Int , Short, Long, Boolean, Charecter, Float, Double

Among them: boolean is a logical type, char is a text type, byte, short, int, and long are integer types, and float and double are floating point types

byte: 1 byte-128~127

short: 2 bytes-2^15~2^15-1

int: 4 bytes-2^31~ 2^31-1

long: 8 bytes -2^63~2^63-1

boolean: 1 byte true false (cannot be replaced by 0 or non-0 in java)

float: 4 bytes -3.403E38~3.403E38

double: 8 bytes -1.798E308~- 4.9E324

char: 2 bytes '\u0000' ~' '\uffff '(hexadecimal, converted to 0~65535)

(1 byte equals 8 bits)

java learning video recommendation: java introductory learning

Example:

package com.Howard.test01;
/**
 * java基本数据类型的字节、取值范围
 * 由于java.lang.Boolean没用提供size方法,
 * 所以这里只具体列出了8种基本数据类型中的7种
 * @author Howard
 * 2017年2月24日
 */
public class BasicDataTypeTest {
 
	public static void main(String[] args) {
		System.out.println(Integer.TYPE+":"+Integer.SIZE
				+"  "+Integer.MIN_VALUE+"~"+Integer.MAX_VALUE);
		System.out.println(Short.TYPE+":"+Short.SIZE
				+"  "+Short.MIN_VALUE+"~"+Short.MAX_VALUE);
		System.out.println(Byte.TYPE+":"+Byte.SIZE
				+"  "+Byte.MIN_VALUE+"~"+Byte.MAX_VALUE);
		System.out.println(Long.TYPE+":"+Long.SIZE
				+"  "+Long.MIN_VALUE+"~"+Long.MAX_VALUE);
		System.out.println(Character.TYPE+":"+Character.SIZE
				+"  "+(int)Character.MIN_VALUE+"~"+(int)Character.MAX_VALUE);
		System.out.println(Float.TYPE+":"+Float.SIZE
				+"  "+Float.MIN_VALUE+"~"+Float.MAX_VALUE);
		System.out.println(Double.TYPE+":"+Double.SIZE
				+"  "+Double.MIN_VALUE+"~"+Double.MAX_VALUE);
	}
}

Run result:

How many bytes (bits) do each data type in java occupy?

##More java related article recommendations:

Introduction to java development

The above is the detailed content of How many bytes (bits) do each data type in java occupy?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn