- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Constants
In Java, a constant value is created by using a literal representation of it. For example, below are some literals :
- 100 - specifies an integer literal
- 98.6 - specifies a floating-point literal
- 'X' - specifies a character constant
- "This is a test" - this literal specifies a string literal
A literal can be used anywhere a value of its type is allowed.
Types of Literals
In Java, there are the following types of literals:
- Integer Literals
- Floating-point Literals
- Boolean Literals
- Character Literals
- String Literals
Now let's take a look at all the above literals types in Java.
Java Integer Literals
In Java, Integers are the most commonly used type in normal programs. Any whole number value represents an integer literal.
Java allows these three types of integer literals :
- Decimal (base 10) Integer Literals
- Octal (base 8) Integer Literals
- Hexadecimal (base 16) Integer Literals
Let's take a look at these three Integer Literals one by one.
Java Decimal Integer Literals
Decimal Integer Literals are decimal values i.e, they describes a base 10 number. For instance, 1, 4, 7, and 62 etc.
To convert a number from any number system to decimal number system, check the following :
- Java Binary to Decimal Conversion
- Java Octal to Decimal Conversion
- Java Hexadecimal to Decimal Conversion
Example
Following is an example program for decimal integer literals :
/* Java Program Example - Java Decimal Integer Constant */ public class JavaProgram { public static void main(String args[]) { int nump = +100, numm = -100, numn = 100; System.out.println("The first decimal integer constant value is " + nump); System.out.println("The second decimal integer constant value is " + numm); System.out.println("The third decimal integer constant value is " + numn); } }
When the above Java program is compile and executed, it will produce the following output:
Java Octal Integer Literals
In Java, octal values are denoted by a leading zero. Normal decimal numbers cannot have a leading zero. Therefore, the seemingly valid value 09 will give an error from the compiler, since 9 is outside of octal's 0 to 7 range. For instance, 8 (decimal integer) will be written as 010 as octal integer (810 = 108) and decimal integer 12 will be written as 014 as octal integer (1210 = 148).
- Java Decimal to Octal Conversion
- Java Binary to Octal Conversion
- Java Hexadecimal to Octal Conversion
Java Hexadecimal Integer Literals
A more common base for numbers used by programmers is hexadecimal, which matches cleanly with modulo 8 word sizes, like 8, 16, 32, and 64 bits. You signify a hexadecimal constant with a leading zero-x, (0x or 0X). The range of hexadecimal digit is from 0 to 15, therefore A through F (or a through f) are substituted for 10 through 15.
Therefore the number 12 will be written either as 12 (as decimal) or 14 (as octal) or 0XC (as hexadecimal).
To convert a number from any number system to Hexadecimal Number System, check the following :
- Java Decimal to Hexadecimal Conversion
- Java Binary to Hexadecimal Conversion
- Java Octal to Hexadecimal Conversion
Java Floating-point Literals
Floating-point numbers represent decimal values with fractional component. They can be expressed in either standard or scientific notation. Standard notation comprises of a whole number component followed by a decimal point followed by a fractional component. For instance, 6.0, 3.14159, and 0.6667 represent valid standard notation floating-point numbers.
Scientific Notation
Scientific notation uses the standard-notation, floating-point number plus a suffix that specifies a power of 10 by which the number is to be multiplied. And the exponent is indicated by an E (uppercase) or e (lowercase) followed by a decimal number which can be positive or negative. For example, 6.022E25, 314159E-05, and 2e+100.
In Java, floating-point literals default to double precision. To determine a float literal, you must append an F (uppercase) or f (lowercase) to the constant. You can also specify a double literal explicitly by appending a D (uppercase) or d (lowercase). Doing so is redundant. The default double type consumes 64 bits of storage, while the smaller float type requires only 32 bits.
Hexadecimal Floating-point Literals
In Java, hexadecimal floating-point literals are also supported, but they are rarely used. They must be in a form similar to the scientific notation, but a P or p, instead of an E or e, is used.
Java Boolean Literals
Boolean Literals are simple. There are only two logical values that a boolean value can have, i.e., true and false. The values of true and false do not convert into any numerical representation. In Java, the true literal doesn't equal to 1, nor does the false literal equal to 0. The Boolean literals in Java, can only be assigned to the variables declared as boolean or used in expressions with Boolean operators.
Java Character Literals
Character in Java are indices into the Unicode character set. They are 16-bit values that can be converted into integers and manipulated with integer operators, such as addition and subtraction operators. A literal character is represented within a pair of single quotes.
All the visible ASCII characters can be directly entered inside the quotes, like 'a', 'z', and '@'. For characters that are impossible to enter directly, there are several escape sequences which allow you to enter the character you need, such as '\'' for the single-quote character itself and '\n' for the newline character.
Here this table lists the escape sequences :
Escape Sequence | Name |
---|---|
\ddd | Octal character (ddd) |
\uxxxx | Hexadecimal Unicode character (xxxx) |
\' | Single quote |
\" | Double quote |
\\ | Backslash |
\r | Carriage return |
\t | Tab |
\n | New line (line feed) |
\f | Form feed |
\b | Backspace |
Java String Literals
String literals in Java are specified like they are in most other programming languages - by enclosing a sequence of characters between a pair of
double quotes. Below are some examples of the string literals:
"Hello, World"
"two\nlines"
"\"This is in quotes\""
You will learn about strings in Java Strings chapter later.
« Previous Tutorial Next Tutorial »