What is definition of an invalid literal?

0 votes
Apr 20 in Java by Stephen
• 120 points
35 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 by Gitika
• 29,290 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
• 53,080 points
1,190 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,190 points
1,603 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,190 points
103 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,090 points
1,330 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
• 53,080 points
126 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
• 53,080 points
284 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,580 points
319 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,090 points
100 views
0 votes
1 answer

What is Modeshape. How can i use this?

ModeShape is an open-source implementation of the JSR-283 specification and ...READ MORE

answered Mar 30 in Java by Gitika
• 29,290 points
209 views
0 votes
1 answer

What Kind of Files IOC Container can Read other than XML directly or Indirectly?

Hi, @Ferry, The configuration metadata is nothing but bean ...READ MORE

answered Apr 23 in Java by Gitika
• 29,290 points
114 views