Studying Java-Part 4-Literal

1.First of all

This time about "literal". Also, I will talk about the type combined with the literal.

Some of you may remember that literals were mentioned in the article Study Java-Part 2-Variables.

2. What is a literal?

Let me explain about literals.

Literals are the values described in the source code. And that literal is a constant, and you have to rewrite the source code directly to change it.

For example, it refers to "1", "'A'", and "3.14159265" in the following source code.

Main.java


public class Main {
	public static void main(String[] args) {

		int number = 1;
		char ch = 'A';
		double pi = 3.14159265;

	}
}

3. A literal that matches the mold

Next, I will introduce literals that match the type. Also, you can add the source code of the assignment to the type that matches it.

Byte and short are basically the same as int type, so only int is used.

Main.java


public class Main {
	public static void main(String[] args) {

		//Integer int
		int integerValue;
		//Ordinary integer
		integerValue = 100;

		//Integer long
		long longValue;
		//For long, add l or L
		longValue = 1000l;

		//Floating point number float
		float floatValue;
		//For float, add f or F
		floatValue = 655.36f;

		//Floating point double
		double doubleValue;
		//For double, add d or D
		doubleValue = 512.256d;

		//Character char
		char characterValue;
		//Character''(Single quote)Surround with
		characterValue = 'A';

		//Boolean boolean
		boolean booleanValue;
		//Only true or false can be assigned
		booleanValue = true;

		//String String
		String stringValue;
		//The string is""(Double quotation)Surround with
		stringValue = "Hello!!";
	}
}

The right side at the time of substitution is a literal. Actually, there are other ways to write it, but I think it's enough to remember this for the time being.

4. n-ary literals

In general, there are four ways to write integer literals: binary, octal, decimal, and hexadecimal.

Decimal numbers are the way we usually write. Express using numbers from 0 to 9.

The problem is other than decimal numbers.

4.1. Binary literals

Binary numbers are represented only by the numbers 0 and 1. Java distinguishes between binary literal values by prefixing them with "0b". A binary number is something like "0b11001".

Here is a brief list of decimal and binary numbers.

Decimal number Binary number
0 0b0
1 0b1
2 0b10
3 0b11
4 0b100
5 0b101
6 0b110
7 0b111
8 0b1000
9 0b1001
10 0b1010
11 0b1011
12 0b1100
13 0b1101
14 0b1110
15 0b1111
16 0b10000

4.2. 8-base literals

Eighth numbers are represented using numbers from 0 to 7. Java can be distinguished from an octal number by prefixing it with "0" when writing it as a literal value. If you use something like "0115", it is an octal number.

Here is a brief list of decimal and octal numbers.

Decimal number 8 base
0 00
1 01
2 02
3 03
4 04
5 05
6 06
7 07
8 010
9 011
10 012
11 013
12 014
13 015
14 016
15 017
16 020

4.2. Hexagonal literals

Next is the hexadecimal number. Hexagonal numbers are represented using numbers and letters from 0 to f. Keep in mind that hexadecimal numbers are often used when touching a computer.

When writing as a literal value, prefix it with "0x".

Here is a brief list of decimal and hexadecimal numbers.

Decimal number Hexadecimal
0 0x0
1 0x1
2 0x2
3 0x3
4 0x4
5 0x5
6 0x6
7 0x7
8 0x8
9 0x9
10 0xa
11 0xb
12 0xc
13 0xd
14 0xe
15 0xf
16 0x10

5. Conclusion

Literals pop up on the source here and there. It is recommended to assign literals to variables (or constants) as much as possible before using them. Because it is related to the readability of the source.

~~ By the way, I call it "raw value on the source". You can't use it even though you have words properly lol ~~

Also, n-ary numbers are mathematical. It should be difficult for beginners. ~~ I hate octal numbers ~~

About this ...

Next, I will talk about "constants".

Next time → "Study Java-Part 5-Constant"

Recommended Posts

Studying Java-Part 4-Literal
Studying Java-Part 0-Overview
Studying Java-Part 5-Constant
Studying Java-Part 2-Variables
Studying Java-Part 9-Scope
Studying Java-Part 7-Array
Studying Java-Part 3-Type
Studying Java-Part 10-if statement
Studying Java-Part 1-Hello World
Studying Java-Part 11-switch statement
Studying Java ―― 3
Studying Java ―― 9
Studying Java ―― 4
Studying Java -5
Studying Java ―― 1
Studying Java # 0
Studying Java ―― 8
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10