JavaForBeginners

Data in Java


Data is the information we work with. This could be a character such as letter A, a String such as name of a person, a number such as 4 or more complex types such as collection of objects. Data types in Java are classsified as primitive or nonprimitive.

Primitive data types

There are eight primitive data types in Java namely: byte, short, int, char, long, float, double and boolean.

byte, short, int, long, float and double represent number values of various formats.

char represent a letter value like A.

boolean represent only two possible values of true or false.

Each of these data types can hold deffering values.

To declare a primitive data type use the syntax containing the data type name, followed by the name you have given the data and finally a semicolon as shown below:

         byte number1;   short number2;   int number3;  char alphabet;   long number4;  float number5;   double number6;  boolean yesOrNo; 

NonPrimitive data types

Non primitive data types are String , Arrays and Classes.

An example of a String is a name such as ‘‘John’’ . Please note that String as a data type is written with a capital S. String is any group of letters written together to form a word. To represent a String in your code first declare it as shown below:

    String name;

Array is a collection of values of any data type.

class is a specification from which an object is created.

Primitive data types and allowed values

Primitive data types can hold values assigned to them, but the range of values vary from one to the other.

Data type Size value
byte 1byte Stores whole numbers from -128 to 127
short 2byte Stores whole numbers from -32,768 to 32,767
int 4bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8bytes Stores whole numbers -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 2bytes Stores true or false valuesStores
char 1byte Stores a single character/letter or ASCII values

Working with Data types

The working environment for Java needs to be installed before working with data types. click on the arrow below for instructions.

Java JDK 17.0.8 installation

The Java JDK includes tools useful for developing and testing programs written in the Java programming language. There are many versions of Java. The latest at the time of writing is Java 20, but we are going to use 17 which is a Long time support version. To install Java 17.0.8, go to https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html and select and download the Windows x64 Installer. Run the installer and follow the instructions to stall the Java development environment.

Install an IDE: The Integrated Development Environment has a user interface and tools that makes working with codes easier.

Install Eclipse IDE at https://www.eclipse.org/downloads/packages/release/2022-12/r/eclipse-ide-java-developers

In the next page we will explore data types