Java中的字面量

南楼画角 提交于 2020-02-22 12:19:39

Literals in Java – Integral, Floating-Point, Char, String, Boolean

BY DATAFLAIR TEAM · UPDATED · AUGUST 29, 2019

Literals are number, text, or anything that represent a value. In other words, Literals in Java are the constant values assigned to the variable. It is also called a constant.

For example,

  1. int x = 100;

So, 100 is literal.

There are 5 types of Literals can be seen in Java. But before we start the discussion, you should revise the concept of Variables in Java.

Types of Literals in Java

转存失败重新上传取消
Literals in Java with examples

1. Integral Literals in Java

We can specify the integer literals in 4 different ways –

  • Decimal (Base 10)

Digits from 0-9 are allowed in this form.

  1. Int x = 101;
  • Octal (Base 8)

Digits from 0 – 7 are allowed. It should always have a prefix 0.

  1. int x = 0146;

Don’t forget to check Datatypes in Java with examples

Integral Literals in Java with example

  • Hexa-Decimal (Base 16)

Digits 0-9 are allowed and also characters from a-f are allowed in this form. Furthermore, both uppercase and lowercase characters can be used, Java provides an exception here.

  1. int x = 0X123Face;
  • Binary

A literal in this type should have a prefix 0b and 0B, from 1.7 one can also specify in binary literals, i.e. 0 and 1.

  1. int x = 0b1111;

Example-

package com.dataflair.literals;
public class IntegralLiteral {
  public static void main(String[] args)
  {
    int decimalValue = 123; // decimal-form literal
    int octalValue = 01200; // octal-form literal
    int hexaDecimalValue = 0xAce; // Hexa-decimal form literal
    int binaryValue = 0b00101; // Binary literal
    
    System.out.println("Decimal form literal is "+decimalValue);
    System.out.println("Octal form literal is "+b);
    System.out.println("Hexa-decimal form literal is "+hexaDecimalValue);
    System.out.println("Binary literal is "+binaryValue);
  }
}

Output-

转存失败重新上传取消

转存失败重新上传取消

We can specify explicitly as long type by suffixed with l or L but there is no way to specify byte and short, but if it’s in the range the compiler automatically treats it as a byte.

Do you know what is Java Abstract Data Types (ADT)?

2. Floating-Point Literals in Java

Here, datatypes can only be specified in decimal forms and not in octal or hexadecimal form.

  • Decimal (Base 10)
package com.dataflair.literals;
public class FloatingPointLiteral 
{
  public static void main(String args[])
  {
    double decimalValue = 101.230; // decimal-form literal
    double decimalValue1 = 0123.222; // It also acts as decimal literal
    double hexaDecimalValue = 1.234e2; // Hexa-decimal form
    System.out.println("Decimal form literal is "+decimalValue);
    System.out.println("Second Decimal form literal is "+decimalValue1);
    System.out.println("Hexa decimal form literal is "+hexaDecimalValue);
  }
}

Output –Floating-point-literal-in-java

Every floating type is a double type and this the reason why we cannot assign it directly to float variable, to escape this situation we use f or F as suffix, and for double we use d or D.

Have you checked the Latest Career Opportunities of Java?

3. Char Literals in Java

These are the four types of char-

转存失败重新上传取消
Char Literals in Java with rela-time examples

  • Single Quote

Java Literal can be specified to a char data type as a single character within a single quote.

  1. char ch = 'a';
  • Char as Integral 

A char literal in Java can specify as integral literal which also represents the Unicode value of a character.
Furthermore, an integer can specify in decimal, octal and even hexadecimal type, but the range is 0-65535.

  1. char ch = 062;
  • Unicode Representation

Char literals can specify in Unicode representation ‘\uxxxx’. Here XXXX represents 4 hexadecimal numbers.

  1. char ch = '\u0061';// Here /u0061 represent a.
  • Escape Sequence

Escape sequences can also specify as char literal.

  1. char ch = '\n';

Example-

 

package com.dataflair.literals;

public class CharacterLiteral {
public static void main(String[] args)
{
char character = 'd';
//char number = 0789; error: Integer number too large
char unicodeCharacter = '\u0064';
System.out.println(character);
System.out.println(unicodeCharacter);
System.out.println("\" is a symbol");
}
}

Output –

转存失败重新上传取消
Character-literal-in-java

4. String Literals

Java String literals are any sequence of characters with a double quote.

  1. String s = "Hello";

They may not contain unescaped newline or linefeed characters.

However, the Java compiler will evaluate compile-time expressions.

Example-

  1. package com.dataflair.literals;
  2.  
  3. public class StringLiteral {
  4. public static void main(String[] args)
  5. {
  6. String myString = "Hello! Welcome to DataFlair";
  7.  
  8. // If we assign without "" then it treats as a variable
  9. // and causes compiler error
  10. // String myString1 = Hello;
  11.  
  12. System.out.println(myString );
  13.  
  14. }
  15.  
  16. }

Output –

转存失败重新上传取消

转存失败重新上传取消

5. Boolean Literals

They allow only two values i.e. true and false.

  1. boolean b = true;

Example –

  1. package com.dataflair.literals;
  2.  
  3. public class BooleanLiteral {
  4. public static void main(String[] args)
  5. {
  6. boolean boolVar1 = true;
  7. boolean boolVar2 = false;
  8. // boolean boolVar3 = 0; error: incompatible types: int cannot be converted to boolean
  9. // boolean boolVar1 = 1; error: incompatible types: int cannot be converted to boolean
  10. System.out.println(boolVar1);
  11. System.out.println(boolVar2);
  12. }
  13. }

Output –

转存失败重新上传取消
Boolean-literal-in-java

Summary

Literals in Java are an important concept, which will help you to build your basics in programming. Now, you can implement 5 types of literals in your program.

Just practice and implement!!

Nourish your fundamentals with Operators in Java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!