Working with Java Datatypes

Gaurav Shah
6 min readDec 17, 2023

--

Java Datatypes

The Java programming language is statically typed, meaning all variables must be declared before being used. This involves stating the variable’s type and name, for example:

int id = 1;

Doing so tells your program that a field named “id” exists, holds numerical data, and has an initial value of “1”.

Java data types

In Java, the data types are broadly classified into two types.
1. Primitive Data Type
2. Non-Primitive Data Type

Primitive Data Type

A primitive type is predefined by the language, specifies the size and type of variable values, and is named by a reserved keyword.

There are eight primitive data types in Java:

Primitive data type hierarchy
Primitive data types

1. Integers

Byte Data Type
The byte data type is an 8-bit signed two’s complement integer. The byte data type is useful for saving memory in large arrays.

Short Data Type
The short data type is a 16-bit signed two’s complement integer. Similar to byte, use a short to save memory in large arrays, in situations where the memory savings matter.

Integer Data Type
It is a 32-bit signed two’s complement integer.

Long Data Type
The long data type is a 64-bit two’s complement integer and is used when an int type is not large enough to hold the desired value. To designate an integer literal value as a long value, add the suffix L or l. The size of the Long Datatype is 8 bytes (64 bits).
— Literal: A literal is a fixed value that doesn’t need further calculations for it to be assigned to any variable.

Integers size, default value and range
Integers

Integer literal values come in four flavors:

  1. Binary number system: A base-2 system, that uses only 2 digits, 0 & 1.
  2. Octal number system: A base-8 system, that uses digits 0 through 7 (a total of 8 digits). Here the decimal number 8 is represented as octal 10, decimal 9 as 11, and so on.
  3. Decimal number system: The base-10 number system that you use every day. It’s based on 10 digits, from 0 through 9 (a total of 10 digits).
  4. Hexadecimal number system: A base-16 system, that uses digits 0 through 9 and the letters A through F (a total of 16 digits and letters). Here, the number 10 is represented as A or a, 11 as B or b, 12 as C or c, 13 as D or d, 14 as E or e, and 15 as F or f.
Example of how to convert an integer from decimal to octal
Converting an integer from decimal to octal
Example of how to convert an integer from decimal to Hexadecimal
Converting an integer from decimal to Hexadecimal

We can assign integer literals in base decimal, binary, octal, and hexadecimal.
1. For octal literals: use the prefix 0
2. For binary: use the prefix 0B or 0b
3. For hexadecimal: use the prefix 0X or 0x.

We can use underscores as part of the literal values. Grouping individual digits or letters of literal values makes them more readable. The underscores do not affect the values.

Rules to remember:
Here’s a quick list of rules for the usage of underscores in the numeric literal values
■ You can place an underscore right after the prefix 0, which is used to define an octal literal value.
■ You can’t start or end a literal value with an underscore.
■ You can’t place an underscore right after the prefixes 0b, 0B, 0x, and 0X, which are used to define binary and hexadecimal literal values.
■ You can’t place an underscore before an L suffix (the L suffix marks a literal value as long).
■ You can’t use an underscore in positions where a string of digits is expected

2. Floating points

Float Data Type
The float data type is a single-precision 32-bit IEEE 754 floating-point. Use a float (instead of double) if you need to save memory in large arrays of floating-point numbers. The size of the float data type is 4 bytes (32 bits).

Double Data Type
The double data type is a double-precision 64-bit IEEE 754 floating-point. For decimal values, this data type is generally the default choice. The size of the double data type is 8 bytes or 64 bits.

Floating points size, default value and range
Floating point

The default type of a decimal literal is double, but by suffixing a decimal literal value with F or f, you tell the compiler that the literal value should be treated like float and not a double.
You can also add the suffix D or d to a decimal number value to specify that it’s a double value. Because the default type of a decimal number is double, the use of the suffix D or d is redundant.

The following rules are specific to floating-point literals:
You can’t place an underscore before a D, d, F, or f suffix (these suffixes are used to mark a floating-point literal as double or float).
You can’t place an underscore adjacent to a decimal point.

Character

Char Data Type
A char is an unsigned integer. It can store a single 16-bit Unicode character; that is, it can store characters from virtually all the existing scripts and languages.
char stores the value from \u0000 (or 0) to a maximum value of \uffff (or 65,535) inclusive.
Never use double quotes to assign a letter to a char variable. Double quotes are used to assign a value to a variable of type String.
char values are unsigned integer values, so if you try to assign a negative number to one, the code won’t compile but you can forcefully assign a negative number to a char type by casting it to char

Character size, default value and range
Character

Boolean

Boolean Data Type
The simplest primitive data type is boolean. It can contain only one of two values: true or false. It stores its value in a single bit.

Identifiers

All Java variables must be identified with unique names. These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (name, id, address). It is recommended to use descriptive names to create understandable and maintainable code.

Properties of valid identifiers

  1. Identifiers can be of any length
  2. Starts with a letter ( a–z, lowercase), a currency sign, or an underscore
  3. Can use a digit (not at the starting position)
  4. Can use an underscore (at any position)
  5. Can use a currency sign (at any position): ¤, $, £, ¢, ¥, and others
  6. Identifiers are case-sensitive

Note: Identifiers can not have the same name as Java keywords or reserved words.

Java keywords or reserved words that can’t be used as names for Java variables
Java keywords and reserved words

Oracle Certified Associate (OCA) certification overview:
Oracle Certified Associate: Java SE 8 Programmer

For more Java and Spring-Boot-related blogs, check out my profile:
https://medium.com/@gauravshah97

Reach me out at:
https://www.linkedin.com/in/gauravshah97/

--

--