Copyright

Overview of Data Types in Java

Instructor: Alpa Pathak
In the following lesson, we will learn about data types in Java. Data types are containers for the data or information we have to store. We will learn about both primitive and non-primitive data types.

Storing Your Achievements - Introducing Data Types

In this lesson, we will learn about the basic data types in Java. You may be asking yourself, why do we need so many different kinds? Good question! To make it more clear let's take an example.

Over the period of your adult life, you collect various documents, trophies, and medals that you received for the brilliant work you have done. Can you keep them all in Mason jars? No - you will need different types of containers to hold each unique kind of award you earned across your lifespan. For lots of flat certificates, newspaper clippings, and various paper awards, you will need three-ring binders, because there would be too many to put up on a wall. And for all those shiny medals and ribbons you won, an album wouldn't do, would it? So you will need them to be framed up on the wall. Last but not least, for those dazzling trophies, you really need a display case with a glass door (because trophies don't really fit in albums or picture frames, do they?). In the same way, you need different data types to hold different types of data in a logical way. You're in luck - because Java offers eight primitive data types and three non-primitive data types.

Primitive Data Types

Primitive data types are data types that are defined by the language i.e. the compiler knows how to allocate memory for that data type and how to reference them. They also have a default value stored in them. The following are the primitive data types defined by Java.

  • char - this will hold anything that fits in a container that holds 8 bits of information (but needs to be signed)
  • byte - this will hold anything that fits in a container that holds 8 bits of information (and doesn't need to be signed)
  • short - this will hold anything that fits in a container that holds 16 bits of information (but needs to be signed)
  • int - this will hold anything that fits in a container that holds 32 bits of information (but needs to be signed)
  • long - this will hold anything that fits in a container that holds 64 bits of information (but needs to be signed)
  • float- this will hold anything that fits in a container that holds a single-precision 32-bit number
  • double - this will hold anything that fits in a container that holds a double-precision 64-bit number
  • boolean - this will hold only two possible values: true and false. It is used for simple flags. The default value stored is false.

Declaring a primitive data type is easy:


public static void main(String[ ] args) {
  int i;
  char c;
  double d;
}


Non-Primitive Data Type

Non-primitive data types are data types that are not defined by the language, so you (the programmer) have to tell the compiler how to define and reference them. Non-primitive data types are sometimes called 'reference variables', or 'object references', since they reference a memory location, which stores the data. They are not defined by the language, but are instead created by the programmer. The following are the non-primitive data types.

  • class
  • interface
  • array

Let's look at how to define each of these.

Class

The class is a user-defined data type. It is a blue print to create as many objects as you like. A class can have both variables (things that hold data) and functions (operations that do something, also known as methods).

Example:


class accolades{
  char event;
  int year;
  public void addAccolade(char evt, int yr){}
}
  accolades a = new accolades();


In the above example accolades is user-defined class. Its variables are event and year. It has one method called addAccolade().

Class objects can be created using new as shown above.

Interface

An interface is a user-defined data type, just like a class. It also has variables or fields that must be declared both static and final. (they are not instance variables) and functions (also known as object states or methods). But unlike a class, an interface is has an abstract method. We cannot create objects with an interface, but a class can implement the interface and thereby inherit and implement its methods.


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support