Numeric Literals and Type Suffixes
Java allows you to write numeric literals in several different forms to represent values directly in your code. The most common is the decimal form, which uses digits 0–9 without any prefix. For example, 123 is a decimal integer literal. Java also supports hexadecimal, octal, and binary representations for integer literals:
- A hexadecimal literal starts with
0xor0X(such as0x7Bfor123); - An octal literal starts with
0(like0173for123); - A binary literal starts with
0bor0B(for example,0b1111011also equals123).
When writing floating-point or long integer literals, you may need to add a suffix to indicate the type explicitly. By default, integer literals are of type int, and floating-point literals are of type double.
- To specify a
longliteral, you appendLorl(such as123L); - For a
floatliteral, useForf(like1.23F); - You can also use
Dordfor adoubleliteral, though this is optional since floating-point numbers aredoubleby default.
Main.java
12345678910111213141516171819202122232425package com.example; public class Main { public static void main(String[] args) { int decimal = 123; int hex = 0x7B; int octal = 0173; int binary = 0b1111011; long longValue = 123L; float floatValue = 1.23F; double doubleValue = 1.23; double doubleWithSuffix = 1.23D; System.out.println("Decimal: " + decimal); System.out.println("Hexadecimal: " + hex); System.out.println("Octal: " + octal); System.out.println("Binary: " + binary); System.out.println("Long: " + longValue); System.out.println("Float: " + floatValue); System.out.println("Double: " + doubleValue); System.out.println("Double with suffix: " + doubleWithSuffix); } }
1. Which suffix must be used to indicate a long integer literal in Java?
2. What is the value of the Java binary literal 0b1010
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Awesome!
Completion rate improved to 7.14
Numeric Literals and Type Suffixes
Glissez pour afficher le menu
Java allows you to write numeric literals in several different forms to represent values directly in your code. The most common is the decimal form, which uses digits 0–9 without any prefix. For example, 123 is a decimal integer literal. Java also supports hexadecimal, octal, and binary representations for integer literals:
- A hexadecimal literal starts with
0xor0X(such as0x7Bfor123); - An octal literal starts with
0(like0173for123); - A binary literal starts with
0bor0B(for example,0b1111011also equals123).
When writing floating-point or long integer literals, you may need to add a suffix to indicate the type explicitly. By default, integer literals are of type int, and floating-point literals are of type double.
- To specify a
longliteral, you appendLorl(such as123L); - For a
floatliteral, useForf(like1.23F); - You can also use
Dordfor adoubleliteral, though this is optional since floating-point numbers aredoubleby default.
Main.java
12345678910111213141516171819202122232425package com.example; public class Main { public static void main(String[] args) { int decimal = 123; int hex = 0x7B; int octal = 0173; int binary = 0b1111011; long longValue = 123L; float floatValue = 1.23F; double doubleValue = 1.23; double doubleWithSuffix = 1.23D; System.out.println("Decimal: " + decimal); System.out.println("Hexadecimal: " + hex); System.out.println("Octal: " + octal); System.out.println("Binary: " + binary); System.out.println("Long: " + longValue); System.out.println("Float: " + floatValue); System.out.println("Double: " + doubleValue); System.out.println("Double with suffix: " + doubleWithSuffix); } }
1. Which suffix must be used to indicate a long integer literal in Java?
2. What is the value of the Java binary literal 0b1010
Merci pour vos commentaires !