Primitive Data Types in Java | Tiffany R. White Blog

Primitive Data Types in Java

Reading time: 3 mins

Variables in Java need to be declared before they can be used in Java because it is a statically-typed language. Because of this, you need to state the type of variable you are declaring.

A variable’s data type tells Java what type of data the variable contains. For instance, an int contains an integer value. You can perform operations on the int data type like any other value number value.

There are 8 primitive data types in Java. Primitive types are defined by Java and cannot be used elsewhere in your code. 1

The primitive data types in Java are:

  • int: -231 - 231-1
  • short: -32,768 - 32,767 (inclusive)
  • byte: -128 - 127 (inclusive)
  • float: decimal value
  • double: decimal value
  • long: -263 - 263-1
  • boolean: true or false values
  • char: Unicode characters - ‘\u0000’ to ‘\uffff’ or 0 - 65,535 (inclusive)

Literals

Primitive data types are not objects instantiated from classes. You do not create a new one with the new keyword. Instead, you write the literal values of the data type when declaring and initilaizing the variables. For instance:

public static void main(String[]args) {
	double balance = 0.00;
	char grade = 'B';

Note: You need to use single quotes in the initialization of a Unicode character with the char data type.

Thoughts

Java primitive data types are different than JavaScript data types. I will post more on JavaScript data types soon.

Variables in Java need to be declared before they can be used in Java because it is a statically-typed language. Because of this, you need to state the type of variable you are declaring.

A variable’s data type tells Java what type of data the variable contains. For instance, an int contains an integer value. You can perform operations on the int data type like any other value number value.

There are 8 primitive data types in Java. Primitive types are defined by Java and cannot be used elsewhere in your code. 1

The primitive data types in Java are:

  • int: -231 - 231-1
  • short: -32,768 - 32,767 (inclusive)
  • byte: -128 - 127 (inclusive)
  • float: decimal value
  • double: decimal value
  • long: -263 - 263-1
  • boolean: true or false values
  • char: Unicode characters - ‘\u0000’ to ‘\uffff’ or 0 - 65,535 (inclusive)

Literals

Primitive data types are not objects instantiated from classes. You do not create a new one with the new keyword. Instead, you write the literal values of the data type when declaring and initilaizing the variables. For instance:

public static void main(String[]args) {
	double balance = 0.00;
	char grade = 'B';

Note: You need to use single quotes in the initialization of a Unicode character with the char data type.

Thoughts

Java primitive data types are different than JavaScript data types. I will post more on JavaScript data types soon.

  1. For instance, you cannot write String int = "Integer"; which is a convulted example but it stresses the point I’m trying to make.  2



No Ads. No Sponsored Content. Ever.

I've decided that I no longer want to have sponsored posts on this blog. I also, though as tempting as it may sound, don't want ads; even if I got big enough for Carbon ads I don't want them. I want to post free, engaging content for beginning/junior devs. This means that I won't have Setapp posts or affiliates on this blog, though I will keep them on the Uses page. If you enjoy the content, buy me a coffee. Just click the button below.




Webmentions 0

No webmentions were found.