Friday, 8 April 2016

What is Constants and How To Declare in Java with example

A constant in java refers to fixed value that do not change during the execution of a program. java supports several types of constants. They are Numeric and character constants.Numeric constants are again divided into two types integer and real constants, character constants are divided into character and string constants. We can also define symbolic constants using the keyword " final" like const keyword in C Programming.

Integer constant 

                           An integer constant refers to a sequence of digits. There are three types of integers, namely, integer, octal integer, hexadecimal integer.

  • Decimal integer consists of a set of digits, 0 throw 9, preceded by an optional minus sign. valid examples of decimal integer constants are 123, -32, 0, 654321
  • An octal constant consists of any combination of digits from the set 0 throw 7, with a leading zero. Some examples of octal constants are 037, 0, 0435, 0551
  • A sequence of digits preceded by 0x or 0X is considered as hexadecimal integers. They may also include alphabets 'A' throw 'F' or 'a' throw 'f'. A letter 'A' throw 'F' represents the numbers '10' throw '15'. The following are the examples of valid hex integers 

                             0x2, 0x9F, 0xbcd, 0x

Java SE 7 introduces some language enhancements for defining integer constants. These are

  • Binary literals 
  • Numeric literals with underscore 

Binary literals 

                       Just like octal and hexadecimal numbers systems, integer types can be expressed in binary number systems as well. The following example depicts how integer constant is defined in binary numbers system.

int num1=0b01010101;

 Numeric literals with underscore 

int num1= 1_000;

Real Constants 

             Integer numbers are inadequate to represent quantities that vary continuously, such as distances, heights, temperatures, price, and numbers so on. These quantities are represented by numbers containing fractional parts like 17.548. such numbers are called real constants or floating point constants. A floating point constant may thus comprise four parts

  • a whole number
  • a decimal number
  • a fractional part
  • an exponent 

Single character constant 

 A Single character constant or simple character constant contains a single character enclosed within a pair of single quote marks. examples are  '5' , 'x'

String constants 

A string constant is a sequence of characters enclosed between double quotes. The character may be alphabets, digits, special character and blank spaces.

Ex: " Helljava", " 1997", "x"

Defining Symbolic constants 

                                 If any numeric value is repeatedly appeared in your program, such type of values are defined as constants like

In Java, a keyword called" final" is used to define symbolic constants and it has the following syntax

final type symbolic_name = Value

Ex: final int STRENGTH=100;


Post a comment

Find Us On Facebook

Contact Us


Email *

Message *

C Programming


C++ Tutorial


Java Tutorial


software engineering


MS Office


Database Management