Friday, November 26, 2021

Literals in Java

Java literals are the fixed values assigned to a variable. A literal doesn’t require any computation and it is possible to assign a literal to a variable of a primitive type and also to a String even though String is not a primitive type in Java.

As example–

Short s = 100;
int num = 9000;
Boolean flag = true;

Literals in Java can be classified into four types–

  • Integer Literals
  • Floating-point Literals
  • Character and String Literals
  • Boolean literals

Integer Literals in Java

Literal assigned to a type byte, short, int or long is called an integer literal in Java. Make sure when you assign a literal value to a byte or short it is within the range of that particular type.

An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1.

You will generally use a base 10 number i.e. decimal as an integer literal. But integer literals can be expressed by Binary (base two) and hexadecimal (base 16) number system also. Integer literals can be expressed by these number systems:

  • Decimal: Base 10, whose digits consists of the numbers 0 through 9; this is the number system you use every day.
  • Hexadecimal: Base 16, whose digits consist of the numbers 0 through 9 and the letters A through F.
  • Binary: Base 2, whose digits consists of the numbers 0 and 1 (you can create binary literals in Java SE 7 and later).

As example if you want to write 50 using hexadecimal–

int hNum = 0x32;
System.out.println("" + hNum);

Same way, if you want to use binary system to assign literal 50–

int bnum = 0b110010;
System.out.println("" + bnum);

Note - The prefix 0x indicates hexadecimal and 0b indicates binary.

Floating-point Literals in Java

Literal assigned to a type float or double is called floating-point literal in Java. A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double in that case it can end with the letter D or d but that is optional.

Floating-point literal can also be expressed using E or e (for scientific notation).

As example

double dNum1 = 156.7;
// same value as d1, but in scientific notation
double dNum2 = 1.567e2;
float fNum  = 34.8f;

Character and String Literals in Java

Literals of types char and String may contain any Unicode (UTF-16) characters. Always use 'single quotes' for char literals and "double quotes" for String literals. The Java programming language also supports a few special escape sequences for char and String literals: \b (backspace), \t (tab), \n (line feed), \f (form feed), \r (carriage return), \" (double quote), \' (single quote), and \\ (backslash).

As example if you want to assign single quote (‘) itself to a char variable you need escape sequence in that case.

char c ='\'';
System.out.println(c);

That will give as output.

Example of String literals

String s = "Hello";
String lines = "This is the first line.\n"+
        "This is the second line.\n"+
        "This is the third line.\n";

One thing to note here is that String is not a primitive type when you create a String literal a reference to an instance of String class is created.

Boolean literal in Java

Boolean literal can have only one of two values true and false. A Boolean literal can only be assigned to a variable of type boolean. It can also be used be used with conditions which evaluate to a boolean value.

As example

int num1 = 9;
int num2 = 7;
if(num1 > num2 == true){
 System.out.println(" num1 is greater than num2");
}

Here if condition num1 > num2 evaluates to either true or false. Though it can be expressed implicitly also (preferred)

int num1 = 9;
int num2 = 7;
if(num1 > num2){
 System.out.println(" num1 is greater than num2");
}

Here you can see with if only expression is used (num1 > num2) as it will evaluate to true or false anyway.

That's all for this topic Literals in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. Java Variable Types With Examples
  2. Primitive Data Types in Java
  3. Type Casting in Java With Conversion Examples
  4. Object Creation Using new Operator in Java
  5. String in Java Tutorial

You may also like-

  1. Java Abstract Class and Abstract Method
  2. Why main Method static in Java
  3. static Block in Java
  4. ArrayList in Java With Examples
  5. How to Loop Through a Map in Java
  6. Java Exception Handling And Method Overriding
  7. Java ReentrantLock With Examples
  8. Is String Thread Safe in Java

No comments:

Post a Comment