Java Variables and Data Types

Ayana Bando
2 min readApr 24, 2021

--

Variables are containers that store data values.

In Java, there are different types of variables, for example:

  • String — stores text, such as “Hello” and are surrounded by double quotes
  • int — stores integers (whole numbers), without decimals, such as 225 or -225
  • float — stores floating point numbers, with decimals, such as 20.20 or -20.20
  • char — stores single characters, like ‘a’ or ‘B’ and are surrounded by single quotes
  • boolean — stores values with two states: true or false

Non-primitive data types are called reference types because they refer to objects. The main difference between primitive and non-primitive data types are:

Byte

The byte data type can store whole numbers from -128 to 127 and can be used in place of other integer types when memory is a concern and there is a certainty that the value will be within the specified range.

Short

The short data type can store whole numbers from -32768 to 32767.

Int

The int data type can store whole numbers from -2147483648 to 2147483647. In general, the int data type is the preferred data type creating variables with a numeric value.

Long

The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807 when int is not large enough to store the value. The value should end with an “L”.

Float

The float data type can store fractional numbers from 3.4e−038 to 3.4e+038 and should end with an “f”.

Double

The double data type can store fractional numbers from 1.7e−308 to 1.7e+308 and should end with a “d”.

Scientific Numbers

A floating-point number that includes an “e” to indicate the power of 10: float f1 = 35e3f.

Booleans

A boolean data type is declared with the boolean keyword and can only take/return the values true or false.

Char

The char data type is used to store a single character and must be surrounded by single quotes, like ‘A’ or ‘a’.

Additional information can be found below: https://www.w3schools.com/java/java_variables.asp

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

--

--

Ayana Bando
Ayana Bando

Written by Ayana Bando

Innovator and collaborator interested in continuing to grow and learn

Responses (1)