"Amigo, I envy you. Learning new things is so wonderful! And today a new and interesting topic awaits you — literals."

"Apparently I should say that I am the happiest robo-student on the planet, Rishi. Well, I'm ready to soak up the new knowledge!"

"Let's get to it right away then. Literals are specific data written directly in a program's code. We're not talking about just any data. We're only talking about primitive types and Strings.

For example, suppose you have this code:

Code Literals
int a = 5;
int b = a + 10;
String s = "Sum=" + (a + b);
5
10
"Sum="

"The literals in this code are the number 5, the number 10 and the string 'Sum = '.

Integer literals

"In Java, everything, including literals, has a type. All the integer literals (integers) in code are ints. The int type is the standard Java integer type."

"I know this type. I've used it."

"Do you remember other integer types besides int? Some that take up less space in memory?"

"Of course! Byte and short."

"Those are the ones. So, if in your code you decide to assign an integer literal to a variable of type byte or short, there will be no problems. The main thing is that the literal value lies within the range of values that the variable's type can store. The Java compiler is smart enough to understand that a byte variable can be assigned the integer literal 100.

Examples:

Code Description
int a = 300;
This will compile just fine.
byte a = 100; 
This will compile just fine.
byte a = 300; 
There will be a compilation error, because the maximum byte value is 127.

"What about the long type?"

"We can also write literals of this type in our code. To do this, add the Latin letter 'L' or 'l' at the end of the integer.

Examples:

Code Description
long a = 3000000000L; 
This will compile just fine.
long a = 3000000000; 
Compilation error: 3 billion is too large for an int literal.
int a = 3000000000L; 
Compilation error: the literal is a long, but the variable is an int. Additionally, 3 billion is more than the maximum int.

"Did you notice how difficult it is to read large numbers of 10 or more digits?

"Yeah, if you don't have your robovision enabled, then it won't be immediately clear whether we're talking about 3 billion or 30."

"To make code more readable (and this is important!), Java allows underscores to be inserted into numeric literals (they do not affect the value of the number).

"The example above can be rewritten with underscores to make it a little clearer:

Code Description
long a = 3_000_000_000L; 
This will compile just fine.
long a = 3_000_000_000; 
Compilation error: 3 billion is too large for an int literal.
int a = 3_000_000_000L; 
Compilation error: the literal is a long, but the variable is an int. Additionally, 3 billion is more than the maximum int.

"This is much more convenient to read, though a comma instead of an underscore would be more familiar!"

"But we can't use commas in numeric literals because they are already used for another purpose. For example, for separating arguments from each other when calling a method.

undefined
9
Task
New Java Syntax, level 9, lesson 1
Locked
Integer literals
Four public fields, corresponding to the four integer types, are declared in the Solution class. When declared, these fields are initialized with various values stored in integer literals. But the program doesn't compile, and you need to fix it. To do this, make the fewest possible changes to the va

Real number literals

"You've probably already guessed that in your code you can specify not only integers, but also floating point literals (real numbers)."

"Both doubles and floats?"

"Yes. Actually, the rule is quite simple: if a number in the code has a decimal point in it, then the number is a floating point literal. And not just any literal, but double literal. To create a float literal, you need to put the letter 'F' (or 'f') at the end of the number.

Examples:

Code Description
double a = 100.0; 
This will compile just fine.
double a = 100.;
This will compile just fine.
double a = .0;
This will compile just fine.
float a = 100.0f; 
This will compile just fine.
float a = 100.0; 
There will be a compilation error: the variable is a float, but the literal is a double.

"By the way, you can explicitly convert an integer literal into a float or double literal by simply appending the suffix 'F' (for float) or D (for double). Examples:

Code Description
double a = 100D; 
This will compile just fine.
float a = 100F; 
This will compile just fine.
int a = 300D; 
There will be a compilation error: the variable is an int, but the literal is a double.

"Floating-point literals can use scientific notation: in addition to the signed part of the number, you can also specify a power of ten. Example:

Literal Mathematical notation Final value
1.23E2 1.23*102 123.0
1.23E3 1.23*103 1230.0
1.23E-6 1.23*10-6 0.00000123
1E6 1.0 *106 1000000.0
1E-10 1.0*10-10 0.0000000001
undefined
9
Task
New Java Syntax, level 9, lesson 1
Locked
Floating point literals
Seven public fields are declared and initialized in the Solution class. They are initialized with various values stored in floating point literals. But the program doesn't compile, and you need to fix it. To do this, change the field types so they match the values. Do not change the field names or v

String literals

"You can also specify whole lines of text in your code. In order to tell the compiler to treat a string as data (a literal) and not as part of the code, the entire string is surrounded by double quotes on both sides.

"If a single line of code has several double quotes, then they are split into pairs. The first double quotation mark indicates the beginning of a literal. The next one indicates the end of the literal. The next one after that once again marks the beginning of a new literal. And the next marks the end of the second literal. And so on.

"Each such literal is a String.

Examples

Code Explanation
"+"+"+"+"+"+"+"+"+"
There are 5 literals in a line. Each of them consists of a single '+' character
""
This literal is an empty string. A string with no characters.
"2+3"+"-5"
There are two literals here. The result will be the string '2+3-5', not a number
"return"+";"
There are also two literals here. There is no return statement here.

"If a string literal is too long, it can be split into several lines and glued together with the 'plus operator':

Code Explanation
String s = "I hold it true, whate'er befall"
         + "I feel it when I sorrow most;"
         + "'Tis better to have loved and lost"
         + "Than never to have loved at all.";
If you output this line to the screen, then all the text will be displayed on a single line!
undefined
9
Task
New Java Syntax, level 9, lesson 1
Locked
String literals
A public string field is declared and initialized in the Solution class. But the string is too long and difficult to read. For better readability, you need to split it into 5 substrings and concatenate them with the "+" (string concatenation) operator, like this: - first line: "Always write code as

Character literals

"What if I need a literal that is a single character rather than a string? Should I create a string that consists of a single character?"

"No, in that case you need a literal whose type is char. Can you guess how the beginning and end of such a literal is designated?"

"Single quotes?"

"Yes, and inside the single quotes there must be a character and just one character. You cannot use empty single quotes.

Examples:

Code Explanation
'A'
A literal whose type is char. It contains the Latin letter 'A'.
'@' 
A literal whose type is char. It contains the '@' symbol
'' 
A literal whose type is char. It contains a Japanese character. And this is also possible.
'\u1f3a'
A literal whose type is char. It contains a Unicode character specified by its number.

"The last example assigns a Unicode character using a special notation: first we have the prefix \u, followed by 4 hexadecimal characters. You will learn more about this in the next lessons."

undefined
9
Task
New Java Syntax, level 9, lesson 1
Locked
Character literals
Four public character fields are declared in the Solution class. Some values have been assigned to them. The program doesn't compile. You need to fix this without changing the character values. All the fields are static — this is necessary in order to access them in the main() method. You can see th