Java: Primitive data types
Quick summary
- byte: a whole number between -128 and 127.
- short: a whole number between -32,768 and 32,767.
- int: a whole number between -231 and a maximum value of 231-1.
- long: whole number between -263 and a maximum value of 263-1. The long is the 64-bit, whereas the int is 32-bit.
- float:
- double: Used for decimals.
- boolean: true or false values only.
- char: range between 0 and 65,535.
More detail
The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. 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:
See more detail from Oracle about Primitive Data Types here.
Primitive means that no class exists to define a variable of that type. – Java 7 for Absolute Beginners
If you need decimals, you must use double or float. There’s also a type called BigDecimal
(which is similar to BigInteger
), which can hold any number. But it takes up more memory. With floats, you put an f
at the end of the value. Otherwise it gets cast as a double
.
About Tom Johnson
I'm an API technical writer based in the Seattle area. On this blog, I write about topics related to technical writing and communication — such as software documentation, API documentation, AI, information architecture, content strategy, writing processes, plain language, tech comm careers, and more. Check out my API documentation course if you're looking for more info about documenting APIs. Or see my posts on AI and AI course section for more on the latest in AI and tech comm.
If you're a technical writer and want to keep on top of the latest trends in the tech comm, be sure to subscribe to email updates below. You can also learn more about me or contact me. Finally, note that the opinions I express on my blog are my own points of view, not that of my employer.