What is definition of an invalid literal

0 votes
Apr 20, 2020 in Java by Stephen
• 120 points
423 views

1 answer to this question.

0 votes

Hi, @ Stephen,

I will explain to you the fact why and where you get invalid literal in Java with a simple example with context: Suppose, you have taken,

x=5 and y=x++ which is correct in Java, then why is int y=++4 isn't correct!

Here it goes,

Literal: Any constant value which can be assigned to the variable is called as literal/constant.

// Here 5 is a constant/literal.
int x = 5; 

That's because 5 is a value (represented by the literal 5), not a variable (which is basically a named container for a value) like x. You can change the value contained in a variable, but not the value itself. This is why you get the syntax error: Invalid literal.

++ is the pre-increment operator in Java — it takes a variable, increments the value contained within it, and returns the updated value. Which is the reason why constructs like ++(++x) are invalid in Java — the increment and decrement operators return the value instead of a (variable) reference to that value.

I hope this will help you to understand the concept.

answered Apr 20, 2020 by Gitika
• 65,870 points

Related Questions In Java

0 votes
2 answers

In Java, what is the best way to determine the size of an object?

I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,050 points
4,204 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
2,081 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,220 points
194 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,110 points
1,848 views
0 votes
2 answers

what is the best way to convert an ArrayList to a String

You could probably use the Joiner class ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,050 points
256 views
0 votes
2 answers

What is the use of @Override annotation in Java ? When do we use it ?

@Override annotation is used when we override ...READ MORE

answered Aug 14, 2019 in Java by Sirajul
• 59,050 points
739 views
0 votes
1 answer

What is the concept of Immutability for strings in Java ? Why are strings immutable ?

According to Effective Java, chapter 4, page 73, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,640 points
520 views
0 votes
2 answers

What is the use of final class in java?

In Java, items with the final modifier cannot be ...READ MORE

answered Oct 5, 2018 in Java by Daisy
• 8,110 points
274 views
+1 vote
4 answers

What is the difference between string object and string literal?

A String literal is a Java language concept. This ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 59,050 points
22,260 views
0 votes
6 answers

How can I separate the digits of an int number in Java?

You can also have a look here: To ...READ MORE

answered Dec 9, 2020 in Java by Roshni
• 10,430 points
69,771 views