![]() |
![]() |
Java if statement | |
Java if-else statement | |
Java nested if-else | |
Java else-if ladder | |
Java Switch case statement | |
Java Nested Switch Case | |
Java Ternary/Conditional operator |
Java while loop | |
Java do-while loop | |
Java for loop | |
Java Enhanced loop/for each loop | |
Java Labelled for loop | |
Java Nesting of loops | |
Java Break and Continue |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions | |
The java constants refers to fixed
values that don’t change during the execution of a program. Java supports several types of constants as shown in diagram.
A programming language consists of data in the form of numbers, characters and strings, which provides output known as information.The program is sequence of instructions performs the task of processing data.
The instructions are formed using certain symbols and words according to some rules known as syntax rules.Every program instruction must perform conform precisely to the syntax rules of the language.
An integer constant means the sequence of digits
. In java There are 3 types of integer constants.
0 to 9
, preceded by an optional minus sign ex- 975, -634,6632.
NOTE : The spaces between digits are not allowed ex – 9 75, 9 3 5 etc. |
An octal integer constant consists of any combination of digits from 0 to 7
. Ex- 873, 0, 04412:
class IntConst { public static void main(String args[]) { int decimal = 8227; int octal = 0757; int hexadecimal = 0X1EF; } }
Integer numbers are insufficient
to represent numbers that vary continuously, such as temperature, heights, prices etc. number having fractional parts like 61.678 represents these quantities. Such numbers are called real
constants.
Real number constant can include 4
parts
these are shown in the decimal notation, having whole number followed by a decimal point and the fractional part, which is integer.
Ex- 0.0415, 901.12
Example
class RealConst { public static void main(String args[]) { float pi = 3.14f; double g = 9.8; } }
A character constant contains a single
character like ‘s’, ‘1’,’@’ enclosed within single quotes. They require different data type to store. like char.
Example
class CharConst { public static void main(String args[]) { char myChar = 'A'; char myChar2 = '@'; } }
A string constant is a sequence or collection of characters and always enclosed between double quotes it can be characters, alphabets, digits, special characters and black spaces.
Ex- “AndroidBerry”, “@*!”, “javatutorials”
Example
class RealConst { public static void main(String args[]) { String Name = "Harry"; String password = "@189*p"; } }
The backslash char constants are used with output
statements for getting more effects like tab, new line, single quote, double quote, back slash etc.
Constants | Meaning/Used for | Example |
---|---|---|
‘ \b ’ | Back space | println(“hello \b”); |
‘ \f ’ | Form feed | println(“hello \f”); |
‘ \n ’ | New line | println(“hello \n”); |
‘ \r ’ | Carriage return | println(“hello \r”); |
‘ \t ’ | Horizontal Tab | println(“hello \t java”); |
‘ \ ’ | Single quote | println(“ \’ hello \’ ”); |
‘ \ ’’ | Double quote | println(“ \” hello \” ”); |
‘ \\ ’ | Double quote | println(“ hello ‘ \\’ ”); |
public class BackSlashDemo { public static void main(String[] args) { System.out.println("1. AndroidBerry\b"); System.out.println("2. AndroidBerry\f"); System.out.println("3. Android\nBerry"); System.out.println("4. Android\rBerry"); System.out.println("5. Android\tBerry"); System.out.println("6. \'AndroidBerry\'"); System.out.println("7. \"AndroidBerry\""); System.out.println("8. AndroidBerry '\\'"); } }