• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String a = 'a'; //1
String c = '\u0041'; //2
String e = 'abcd'; //3
String b = 'abc'; //4


When i am trying to compile the above code ,It showing error in line 3 not in line line4.

but if i replace line3 with line4 ..like this

String a = 'a'; //1
String c = '\u0041'; //2
String b = 'abc'; //3
String e = 'abcd'; //4

Now ,also its showing error with line3.....

I am not able to understand the logic behind this.
Can somebody help me out..?






 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see here http://faq.javaranch.com/java/ScjpFaq#invalidUnicode

0041=65='A'.


 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the actual problem is that you can't assign a character to a String.
Even the first line on its own won't compile. You need to have:
String a = "a"; //Double quotes
You could however do:

This uses the String constructor that takes a char array.

But lines 3 and 4 will fail regardless, because the syntax 'ab' is not allowed (you need double quotes.)
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am not able to understand the logic behind this.
Can somebody help me out..?



This is why when you have compile errors, you have to fix the first couple, and then recompile. When the compiler encounters an error, it gets confused after awhile.

One error can chain react into many other compiler errors later in the program. Or one error can actually mask another error. In this case, all 4 lines are compile errors, but it looks like the compiler gets comfused and missed the fourth one.

Don't worry about it. Once you fix the eariler ones, it will eventually catch the fourth one.

Henry
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Compiler performs checking on valid syntax in some order.
First it sees valid construction of data types, then it sees valid assignments.

here at line 3:


'abcd' is not a valid construction of a char as ' ' //single quote is used to make a char, so compiler will show error that 'abcd' is invalid char constant.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Singh wrote:I think Compiler performs checking on valid syntax in some order.
First it sees valid construction of data types, then it sees valid assignments.

here at line 3:


'abcd' is not a valid construction of a char as ' ' //single quote is used to make a char, so compiler will show error that 'abcd' is invalid char constant.


That's interesting Punit. Is the order that the compiler uses set in the specification?
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


That's interesting Punit. Is the order that the compiler uses set in the specification?



yes the compiler compilte line by line and in your case everytime the compilation fails at the 3rd line. Because your are using single quote for representing the String.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually compiler must have some sequence for checking.

But one thing to notice is that here Priya Jaspal is not compiling on command line I think, as command line is showing error on both line. see my code:


output:

F:\workspace\scjp\src>javac javaranch/CompilerIssue.java
javaranch\CompilerIssue.java:8: unclosed character literal
String e = 'abcd'; //3
^
javaranch\CompilerIssue.java:8: unclosed character literal
String e = 'abcd'; //3
^
javaranch\CompilerIssue.java:8: not a statement
String e = 'abcd'; //3
^
javaranch\CompilerIssue.java:9: unclosed character literal
String b = 'abc'; //4
^
javaranch\CompilerIssue.java:9: unclosed character literal
String b = 'abc'; //4
^
javaranch\CompilerIssue.java:9: not a statement
String b = 'abc'; //4
^
6 errors



 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both are invalid syntax for declaring of constant char. Hence, compiler complaining for that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic