CodeGym /Courses /New Java Syntax /List of basic data types

List of basic data types

New Java Syntax
Level 12 , Lesson 2
Available

"Hi, Amigo!"

"Hi, Rishi!"

"You've already mastered the basics of Java syntax, so now I want to give you some more details."

"Today, we'll talk about primitive types and how much memory they occupy. This knowledge will come in handy, maybe even today. Here are the basic types:"

Type Size,
bytes
Value range Default value Description
byte 1 -128 .. 127 0 The smallest integer, 1 byte
short 2 -32,768 .. 32,767 0 Short integer, 2 bytes
int 4 -2*109 .. 2*109 0 Integer, 4 bytes
long 8 -9*1018 .. 9*1018 0L Long integer, 8 bytes
float 4 -10127 .. 10127 0.0f Fractional number, 4 bytes
double 8 -101023 .. 101023 0.0d Fractional number that is twice the size of a float, 8 bytes
boolean 1 true, false false Boolean type (only true or false)
char 2 0..65,535 '\u0000' Characters, 2 bytes, all unsigned values
Object 4 Any reference or null. null Stores references to instances of Object or classes that descend from Object

"Let me tell you more about each type."

"The byte type is the smallest integer type. Variables of this type occupy just 1 byte of memory. A byte can store values in the range between -128 and 127."

"Why do we need such a small type? Why can't we always use int?"

"We could. But if you're creating large arrays whose elements never need to store values greater than 100, why not use this type? Does that make sense?"

"A short is twice as long as a byte, and it also only stores integers. The biggest positive number it can store is 32,767. The biggest negative number it can store is -32,768."

"The int type you are already familiar with. It can store integers in the range ±2,000,000,000."

"The float type was created to store real (fractional) numbers. Its size is 4 bytes."

"Fractional numbers are stored in a rather interesting form."

"For example, the number 987654.321 can be represented as 0.987654321*106. This means that it can be represented as two numbers in memory: 0.987654321 (mantissa, or significand) and 6 (base-10 exponent)."

"What do we need that for?"

"This approach lets us use 4 bytes to store numbers much greater than what an int can store. To do this, we have to sacrifice accuracy. Only a portion of those bytes are used to store the mantissa, which means that these numbers only store 6-7 decimal places. Less significant decimal places are discarded."

"These numbers are also called floating-point numbers. This is where the name the float type came from."

"I see."

"The double type is similar to float, but twice as long (hence the name), taking up 8 bytes. It can accommodate a larger mantissa and more significant digits. If you need to store real numbers, always try to use this type."

"char is a hybrid type. Its values can be interpreted both as numbers (which can be added or subtracted) and characters. This is possible because even if characters have a visual representation, the computer mainly sees them as numbers. And it's more convenient to treat them as numbers. One more thing: the char type is always positive. It can't hold negative values."

"The boolean type is a logical type that can store only two values: true or false ."

"Despite its presence in this chart, the Object type isn't a primitive type. It's the base class for all classes in Java. First, all classes are derived from it and therefore contain its methods. Second, an Object variable can store references to objects of any type, including null (a null reference)."

"I've learned a lot today. Thank you for the lesson, Rishi."

Comments (22)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
19 November 2024
Given name to types might extend further in a case as with double suggested for real numbers[don't mix up data types in programming with number types in a math].
abhishe_kira Level 18, India Expert
22 June 2023
To every learner here keep it up. Let's do it. In India we say ALL IS WELL.
Fadhil Radhian Level 18, Semarang, Indonesia
24 March 2023
valuable knowledge here
LX40GL Level 28, Birmingham, United States
6 September 2022
Correct Value Range Form
Anonymous #10775689 Level 14, United Kingdom
3 August 2021
Head First Java says to think of types like coffee cups in a Starbucks. Byte, short, int and long could be seen as tall (12 ounces), grande (16), venti (24), and trenta (31). You pick the type/cup based on the size of the data/coffee you plan to get.
sgubow Level 10, Forest Grove, United States
11 May 2021
Typo on the boolean type line... "false .'"
Sinisa Level 11, Banja Luka, Bosnia and Herzegovina
26 March 2021
Char is declared by single quotes. However if you declare an int with single quotes, it will refer to ASCII table. For instance, int a='9' will correspond to 57 when printed out, since char 9 equals to decimal number 57. Java differentiates single and double quotes. In Python, it's all the same.
Renat Mukhametshin Level 16, Pervouralsk, Russain Federation
7 August 2019
oh... it's good task
Ewerton Level 30, Belo Horizonte, Brasil
29 June 2019
Hmm.. interesting, we have "long long" in C++, but Java doesn't have it.
11 January 2020
is long long 12 bytes long?
Agent Smith Level 38
21 August 2020
I think data type size in C/C++ can vary, depending on compiler/platform/hardware and etc.
Syed Tayyab ul Mazhar Level 12, Karachi, China
16 June 2019
Size of int(4 bytes) should be -2^31 to +(2^31)-1 Range of long type is also wrong.
11 January 2020
yeah you are right! leaving 1 bit for sign, we are left with 31 bits to store!
Kent Hervey Level 19, United States Expert
15 March 2020
Agreed. Maybe those professors mentioned in previous page know stuff after all
Gellert Varga Level 23, Szekesfehervar, Hungary
25 June 2020
I agree. We may criticize someone only if we can do the same much better, without any mistakes.
Zach Level 22, Fort Collins, United States
12 February 2021
Yeah except it's not in BITS. It's in BYTES. 8 bits is a byte. Very different things. A simple google search will prove you are both wrong.