Copyright

Java Constants: Definition, Declaration & Types

Instructor: David Gloag

David has over 40 years of industry experience in software development and information technology and a bachelor of computer science

In this lesson, we'll take a look at Java, what it is, what a computer programming constant is, how we declare them in Java, and the types of constants available in Java.

Our Mobile World

Mobile technology has revolutionized the way we communicate and work. If you don't agree, just look around when you're at your favorite coffee shop, or in the park. You'll see people talking on their cell phones, you'll see them creating documents or presentations on their tablets, and you'll notice them listening to their devices through headphones. It's a sign of the times. We're busy, and we want to take our lives with us while we move. But how do we do that? How do we make the variety of mobile devices available work together seamlessly? It isn't easy, and there are a number of technologies that contribute. One popular one, in particular, is Java.

What is Java?

Java is a high-level computer programming language created in 1991 by Sun Microsystems. Originally called OAK, it is general purpose, meaning that it is meant for a variety of programming tasks. It was designed for cell phones, tablets, and set-top (television) boxes, but proved to be ineffective. In 1999, elements of the language were changed to take advantage of the Internet. It was subsequently bought by Oracle in 2009. These days, Java is quite popular, existing on some three billion mobile phones world-wide, and supported by over nine million developers.

What is a Constant?

As the name suggests, a constant is an entity in computer programming that is immutable. In other words, its value doesn't change. If you think about it, there are a number of constants in the world, things that don't change. The mathematical value Pi which equals 3.1415926 …, the boiling point of water 100C (212F), or the acceleration due to gravity 9.8 meters per second squared. Values like these have existed forever, and we grew up learning and accepting them. Computer programming constants perform a similar function. For the existence of the program that contains them, their values don't change.

How Do You Declare a Java Constant?

Java constants are declared like any other variable, you give it one or more modifiers, a type, a symbolic name, and the value. For a simple floating point value (a number with a fractional part) like Pi, it would look something like:

private static final double PI = 3.1415926;

The keyword 'private' indicates visibility, the keyword 'static' indicates the storage class, and the keyword 'double' indicates the variable type. Ignore them for now, they are beyond the scope of this lesson. The really important part is the keyword 'final'. This is what determines that the variable 'PI' is a constant. It says that for the life of this variable, the value will not change.

What Types of Java Constants Exist?

There a number of Java constant types supported by the language, and they have the same general form as the example mentioned above. They include:

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