Home Java Javagetting Started What are java constants?

What are java constants?

Dec 26, 2019 pm 05:40 PM
java constant

What are java constants?

What are Java constants?

JAVA constants are fixed values ​​in the program and data that cannot be changed. For example, the number 1, the character "a", the floating point number 3.2, etc. In Java, constants include integer constants, floating-point constants, Boolean constants, character constants, etc.

Let’s take a look at these constants in java.

1. Integer constants

Integer constants are integer type data, with four specific representation forms: binary, octal, decimal and hexadecimal. as follows.

Binary: A sequence of numbers consisting of the digits 0 and 1. In JDK7.0, literal values ​​are allowed to represent binary numbers, which must start with 0b or 0B in order to distinguish them from decimal numbers, such as 0b01101100, 0B10110101.

Octal: A sequence of numbers starting with 0 and followed by integers in the range of 0 to 7 (including 0 and 7), such as 0342.

Decimal: A sequence of numbers consisting of integers in the range of 0 to 9 (including 0 and 9). Such as 198.

Hexadecimal: A sequence of numbers starting with 0x or 0X and followed by 0~9, A~F (including 0 and 9, A and F), such as 0x25AF.

It should be noted that in order to indicate different bases in the program, the data has specific identifiers. Octal must start with 0, such as 0711, 0123; hexadecimal must start with 0x or 0X. Such as 0xaf3, 0Xff; when the integer is expressed in decimal, the first digit cannot be 0, except 0 itself. For example, 127 in decimal is represented as 011111 in binary, 017 in octal, and 0x7F or 0X7F in hexadecimal.

What are java constants?

2. Floating-point constants

Floating-point constants are decimals used in mathematics, divided into feet There are two types of single-precision floating-point numbers and double-precision floating-point numbers. Among them, single-precision floating-point numbers end with F or f, while double-precision floating-point numbers end with D or d. Of course, when using floating point numbers, you can also add no suffix at the end. In this case, the virtual machine will default to double precision floating point numbers. Floating point constants can also be represented in exponential form. Specific examples are as follows:

 2e3f 3.6d 0f 3.84d 5.022e+23f
Copy after login

3. Character constants

Character constants are used to represent a character. A character constant should use a pair of single quotes in English half-width format'' It can be English letters, numbers, punctuation marks, and special characters represented by escape sequences. Specific examples are as follows:

‘a’ ‘1’ ‘&’ ‘\r’ ‘\u0000’
Copy after login

In the above example, '\u00' represents a blank character, that is, there are no characters between single quotes. The reason why it can be represented like this is because Java uses the Unicode character set. Unicode characters start with \u, and the corresponding value of blank characters in the Unicode code table is '\u0000'.

4. String constants

String constants are used to represent a series of consecutive characters. A string constant should use a pair of double quotes in English half-width format." ”, specific examples are as follows:

“HelloWorld" “123" "We come \n XXX" "”
Copy after login

A string can contain one character or multiple characters, or it can not include any characters, that is, the length is zero.

5. Boolean constants

Boolean constants are the two values ​​of Boolean type, true and false. This constant is used to distinguish the true or false of a thing.

6. Null constant

The null constant has only one value, null, which means that the reference to the object is empty.

Recommended learning: Java video tutorial

The above is the detailed content of What are java constants?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles