Java Fields: Types & Examples

Instructor: Lyna Griffin

Lyna has tutored undergraduate Information Management Systems and Database Development. She has a Bachelor's degree in Electrical Engineering and a Masters degree in Information Technology.

In this lesson, we will be examining the different types of data fields used in Java. We will look at examples to better illustrate and understand their characteristics.

What Is a Java Field?

Java, like any high-level programming language, has its own syntax and semantics to be adhered to for successful program execution and application development. It is an object-oriented programming language with objects represented by variables associated with methods that are used to describe its classes.

A Java field is a container with associated data. It serves as the descriptive properties of a class. A class is any entity on which data is collected. If for example we are collecting data on a set of students (class), then the student properties (Java fields) will be: name, address, age, and subject.

The Java class will be defined as follows:

Public Class Student {string name; string address; integer age; string subject;}

From the above syntax we can see that the different descriptions may consist of different data types, such as strings or integers. Data types are declared using variables.

Types of Java Fields

Java fields are characterized by various data types. There are two data types in Java: primitive data types and reference object data types.

Primitive Data Types

There are eight primitive data types. Each data type occupies a certain amount of space in the program's memory. The amount of space occupied is determined by the data type. The computer understands and interprets memory in binary form, i.e. 1s and 0s. A single digit consisting of a 1 or 0 is known as a bit. The characters of this paragraph you are reading are currently being transformed into binary digits which the computer can understand.

Boolean Data Type

This data type is used in 'yes' and 'no' statements. For example, in our student database the class can contain a field called Registered, which tells whether the student is registered or not.

Boolean: a value or either true or false (Single bit)

The boolean variable is declared as follows:

Example: var Reg= true;

var Notreg = false;

Byte

This data type occupies more memory space than the boolean. It consists of a series of signed 8-bits known as a byte. It is the smallest integer.

Byte: 8-bit value ranging from -128 to 127

128 and 127 signify the decimal representation of the byte. Because the byte is 8 bits long, its values in binary lie between 00000000 and 11111111.

The byte variable is declared as follows:

Example: byte s =100;

Short

This data type is larger than the byte, as it is 16 bits.

Short: 16-bit value ranging from -32,768 to 32,767

The short variable is declared as follows:

Example: short s =100;

Char

This data type is represented in Unicode characters. Unicode characters are the universally used unique numbers representing every character irrespective of the platform, operating system or language used. Its values range from '\u0000' (or 0) to '\uffff' (or 65,535 inclusive). These values cannot be negative.

Char: 16-bit Unicode character

The char variable is declared as follows:

Example: char let8 =H;

Int

This data type is 32 bits. They are normally used to control loops and arrays.

Int: 32-bit value ranging from -2,147,483,648 to 2,147,483,647

The int variable is declared as follows:

Example: int g = 234543;

If the space is not large enough for the purpose of the program, a 'long' data type is used.

Long

This data type is 64 bits.

Long: 64-bit value ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Unicode provides a unique number for every character, no matter the platform, program, or language. This is normally used in complicated long calculations.

The long variable is declared as follows:

Example: Long schoolbal = 1234567895;

Float

This data type comprises real numbers which will be used for calculating expressions. They are said to have 32-bit floating points. A floating point is a number that contain a fractional value.

Double

This is a data type with a 64-bit floating point. It is typically used in mathematical calculations involving functions such as sin() and cos(). The declaration and initialization is as follows:

Example: double sqrootX = 123.4567895;

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 200 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