Memory Units And Java Primitive data types

Memory Units
  • Bit: The computer memory units start from bit.
  • Byte: It is the fundamental unit to measure data. It contains 8 bits or is equal to 8 bits.
  • Kilobyte: A kilobyte contains 1024 bytes.
  • Megabyte: A megabyte contains 1024 kilobytes.
  • Gigabyte: A gigabyte contains 1024 megabyte.
  • Terabyte: A terabyte contains 1024 gigabytes.
UnitEquivalent
1 kilobyte (KB)1,024 = 2 10 2^{10} 210 bytes
1 megabyte (MB)1,048,576= 2 20 2^{20} 220 bytes
1 gigabyte (GB)1,073,741,824= 2 30 2^{30} 230 bytes
1 terabyte (TB)1,099,511,627,776= 2 40 2^{40} 240 bytes
1 petabyte (PB)1,125,899,906,842,624= 2 50 2^{50} 250 bytes
Java Primitive data types

There are majorly two types of languages.

  • First one is Statically typed language where each variable and expression type is already known at compile time. Once a variable is declared to be of a certain data type, it cannot hold values of other data types.
    Example: C, C++, Java.
  • The other is Dynamically typed languages. These languages can receive different data types over time.
    Example: Ruby, Python

Java is statically typed and also a strongly typed language because in Java, each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.
This involves stating the variable’s type and name, as you’ve already seen:

int gear = 1;

Doing so tells your program that a field named “gear” exists, holds numerical data, and has an initial value of “1”. A variable’s data type determines the values it may contain, plus the operations that may be performed on it. In addition to int, the Java programming language supports seven other primitive data types. A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are:

  • byte: The byte data type is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).
  • short: The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
  • int: By default, the int data type is a 32-bit signed two’s complement integer, which has a minimum value of - 2 31 2^{31} 231and a maximum value of 2 31 2^{31} 231-1.In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2 32 2^{32} 232-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information
  • long: The long data type is a 64-bit two’s complement integer. The signed long has a minimum value of - 2 63 2^{63} 263 and a maximum value of 2 63 2^{63} 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2 64 2^{64} 264-1. Use this data type when you need a range of values wider than those provided by int.
  • float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead.
  • double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
  • boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its “size” isn’t something that’s precisely defined.
  • char: The char data type is a single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).

Data Types in Java:
在这里插入图片描述

Summary

在这里插入图片描述
It’s not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.
Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值