Java coding :: Eclipse shows a compile-time error on line 4 just why?

public static void main(String[] args){ char a=true;//Line 1 char b=null; //Line 2 char c='\n'; //Line 3 char d='Hell'; //Line 4 } 

Eclipse shows a compile-time error on line 4 just why? My compiler of understanding reads from top to bottom. therefore, he should say a compile-time error in line number 1. but how priority is given to line number 4. Please clarify. thanks

+5
source share
2 answers
  • I think there is no line here. 4 there is a syntax error, so the compiler will check the expression syntax first so that you know that this is not the right way for such a character (β€œHello”).

  • You say why the compiler does not show an error in lines 1 and 2. First, due to the statement in lines 1 and 2, they are incorrect according to the syntax. This is logically incorrect.

    / li>

therefore, according to me, the compiler first prefers the syntax error of your code. I hope you understand this (Syntax and semantics).

+8
source

To answer this question, we need to understand how the java compiler works in the case of Char during lexical analysis.

Ideally, the compiler expects Char to have only one character so that it looks at the opening ' and its end.

In the above case, it throws an error, since the analyser indicates an error, indicating that: it has detected more than one character, which leads to the results of the "Unclosed Character literal" , which, unfortunately, occurs before the compiler really checks the casting exception or Type incompatibity .

Poor IDE do not know about everything that happens behind the scenes, give it more priority.

You can get all the errors at your disposal by changing ' to " :

  char a=true;//Line 1 char b=null; //Line 2 char c='\n'; //Line 3 char d="Hell"; //Line 4 

As it is now in the above state, Lex happy and moving forward.

+2
source

Source: https://habr.com/ru/post/1245958/


All Articles