Java中的字面量

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

Java-literals-tutorial.jpguploading.4e448015.gif转存失败重新上传取消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-

Integral-Literal-in-java.jpguploading.4e448015.gif转存失败重新上传取消Integral-Literal-in-java.jpguploading.4e448015.gif转存失败重新上传取消Integral-Literal-in-java

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-

Java-Char-literal-with-example.jpguploading.4e448015.gif转存失败重新上传取消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.jpguploading.4e448015.gif转存失败重新上传取消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 –String-literal-program-in-java.jpguploading.4e448015.gif转存失败重新上传取消String-literal-program-in-java.jpguploading.4e448015.gif转存失败重新上传取消String-literal-program-in-java

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.jpguploading.4e448015.gif转存失败重新上传取消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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值