Literals

Java Syntax
Level 10 , Lesson 8
Available

"And, finally, a lesson from Rishi in the form of a lecture: a pile of useless information. That's what all lecturers love. Just take a glance at it, and that will be enough."

"I'm ready."

"Today I'll tell you about literals. Literals are all data written directly in Java code. Here are some examples:"

Code Description
"Rain In Spain" This is a literal. Its type is String
115 This is a literal. Its type is int
0.256 This is a literal. Its type is double
'\u1234' This is a literal. Its type is char

"Actually, there are plenty of other kinds of literals. You can use literals to assign values of any known type:"

Literal Type Description
123676 int Integer
22223333444433332222L long Long integer
12.323232323f float Fractional number
12.33333333333333333d double Long fractional number
"Rain"
""
"Rain\nIn\nSpain\u123"
String String
'\u3232'
'T'
'5'
char Character
true, false boolean Logical type
null  Object Object reference

"So, code consists of methods, classes, variables, etc., but literals are specific values written directly in the code. Did I get that right?"

"Yes, absolutely."

"Great. I'm finally starting to get this whole Java thing."

10
Task
New Java Syntax, level 10, lesson 8
Locked
Why you might fail to have a great career
You don’t have to unplug your brain to prevent a meltdown due to an overabundance of new information. We just need to feed it something simple that’s still interesting. Let’s watch an inspiring speech by Larry Smith about why people fail to have great careers unless they follow their passion and see opportunities instead of obstacles.
Comments (7)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Valeri Burlacu Level 15, Bamberg, Germany
27 March 2020
"Rain\nIn\nSpain\u123" "Rain\nIn\nSpain\u1234"
Ivan Duka Level 22, Winnipeg, Canada
6 April 2019
Isn't 0.256 literal a double instead of a float?
Bazsó Benjámin Level 20, Szombathely, Hungary
9 April 2019
it can also be a double or a float but float uses less resource {float 4 -10127 .. 10127 0.0f Fractional number, 4 bytes double 8 -101023 .. 101023 0.0d Fractional number that is twice the size of a float, 8 bytes} lvl 10 lecture 2
Ivan Duka Level 22, Winnipeg, Canada
10 April 2019
Yes, I understand that. I am talking about the initialization:

double x = 0.256;
float y = 0.256f;
But this will not compile (there is no "f" after the number):

float y = 0.256;
Roy Level 22, Bangkok, Thailand
23 April 2019
Yes you are right. 0.256 This is a literal. Its type is float double. Small error in the lesson.
Thang Za Thang Level 18, Melbourne, Australia
5 July 2020
They have fixed it, it seems : )
Hashirama Level 26, Port-Harcourt, Nigeria
31 January 2019
Hmmm...... This whole Java thing ehh!!!