Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java define constants for any characters such as SPACE?

Does Java include any constants for single characters such as SPACE?

Having names taken from Unicode would be handy when doing string manipulations.

I want this:

String musician = "Lisa" + Character.SPACE + "Coleman" ;

…rather than this:

String musician = "Lisa" + " " + "Coleman" ;

(not to be confused with the java.lang.Character class)

If nothing bundled with Java, alternatives?

like image 488
Basil Bourque Avatar asked Oct 17 '15 06:10

Basil Bourque


People also ask

How is a constant defined in Java?

A constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants. A constant can make our program more easily read and understood by others. In addition, a constant is cached by the JVM as well as our application, so using a constant can improve performance.

How do you declare a character constant in Java?

A single character constant ( or simply character constant) is a single character enclosed within a pair of single quote. The example of single-character constants are as follows: '5' 'x' ';' ' ' etc. The last constant is blank space.

Are constants in a Java program?

Java doesn't have built-in support for constants, but the variable modifiers static and final can be used to effectively create one. Constants can make your program more easily read and understood by others.

Does Java include constants for single characters such as space?

Does Java include any constants for single characters such as SPACE? Having names taken from Unicode would be handy when doing string manipulations. If nothing bundled with Java, alternatives? Show activity on this post. Show activity on this post. There is no such constant, and for good reasons, IMO.

What are constants in Java?

Like any other programming languages, Java supports its own set of data types. Constants in java are fixed values that are not changed during the Execution of the program java supports several types of Constants those are:

Are string literals singleton or constant in Java?

The string literal thus becomes a de facto constant or singleton. More precisely in java, objects representing Java String literals are obtained from a constant String pool which is internally kept by a java virtual machine.

What is a character constant in a string?

A Character constant is a single alphabet, digit or any special symbol enclosed using single quotes. For example, 'Y', 'd', '6', '#', '&'. The maximum length of a character constant is 1 character long.


Video Answer


3 Answers

There are some in Apache Commons

org.apache.commons.lang3.StringUtils.SPACE

more

like image 115
Mike Avatar answered Oct 18 '22 01:10

Mike


There is no such constant, and for good reasons, IMO.

Why use a constant in the first place in your example?

String musician = "Lisa" + Character.SPACE + "Coleman" ;

is less readable than

String musician = "Lisa Coleman";

or even than

String musician = "Lisa" + ' ' + "Coleman";

So I guess it's not for readability reasons.

I thus guess that you want a constant to avoid repeating yourself in several portions of code. But using Character.SPACE instead of ' ' everywhere doesn't lead to less repetitions. Only to more verbose and less readable code.

I thus guess that you want to be able to change the constant value in one place, and have it changed everywhere it's used. But then using a built-in Character.SPACE constant wouldn't allow you achieveing that goal. You would still need your own constant, and its name shouldn't be what the value is, but what the value is for:

private static final char FIRST_NAME_LAST_NAME_SEPARATOR = ' ';

Now, there is a good reason to use that constant: if you later decide to use a tab instead of a space, you can change the value of the constant and recompile all your code.

like image 42
JB Nizet Avatar answered Oct 18 '22 03:10

JB Nizet


Java does have a list of character constants used to substitute "untypeable" characters such as a backspace (represented by \r). the full list can be found at https://docstore.mik.ua/orelly/java-ent/jenut/ch10_05.htm However here is a few examples:

\\      Backslash
\r      Carriage Return
\"      Double Quote (same for single Quote)
\n      Newline

You get the idea. Hope this is what you were looking for since you did mention character constants in your title.

Edit: When using character constants as characters surround them with single quotes rather than double quotes. Despite being two (or more) characters long they denote a single character or a single binary digit rather than a string.

Note: When attempting to compare the 'enter' character in the java.awt.event.KeyListener keyTyped function the \n character constant should be user rather than the \r character constant.

like image 20
Samuel Newport Avatar answered Oct 18 '22 02:10

Samuel Newport