Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

about char declair

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following two are valid declarations of a char? (Choose Two)
A.char ch = "a";
B.char ch = '\'';
C.char ch = 'cafe';
D.char ch = "cafe";
E.char ch = '\ucafe';
F.char ch = '\u10100';
G.char ch = (char) true;

the answer is B,E.
but i run this in elipse, only C is fail.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean that A. char ch = "a"; is compiling fine ??
Please post your sample, there must be something
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shiao Kung Chux!!

Well What u say is not to the point.
U see when u paste the code on Eclipse say something like this :

char cha = "a";
char chb = '\'';
char chc = 'cafe';
char chd = "cafe";
char che = '\ucafe';
char chf = '\u10100';
char chg = (char) true;

At first only char chc = 'cafe'; line is the one shown with error highlighting.
But as soon as u comment that line u will see that
no other line except
char chb = '\'';
char che = '\ucafe';
is compiling.
This is because of incremental compiling feature of eclipse
there is nothing wrong either with eclipse or the answer.B and E are only valid
Hope this helps
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No need to post the sample then
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I run it in the elipse,also Found this problem !
may be elispe which found some compile time time have some limits
but you do it like this


char ch1 = "a";//can not cast String to char
char ch2 = '\'';
//char ch3 = 'cafe';//invalid character constant
char ch4 = "cafe";//can not cast String to char
char ch5 = '\ucafe';
char ch6 = '\u101000';//invalid character constant
char ch7 = (char) true; //can not cast boolean to char


you maybe see more compile time error
hope this help you
I am chinese ,my english is poor
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class CharDeclar
{
public static void main(String[] args)
{
//char ch2 = "a";
char ch = '\'';
//char ch = 'cafe';
//char ch = "cafe";
char ch1 = '\ucafe';
//char ch = '\u10100';
//char ch = (char) true;
System.out.println("Hello World!" +ch1);
System.out.println("Hello World!" +ch);

}
}
O/P is:
---------- Run ----------
Hello World!?
Hello World!'


Please tell me,why i am getting "?" and "'" symbols
 
Sayan Bhattacharya
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shreedhar


//char ch2 = "a";
char ch = '\'';
//char ch = 'cafe';
//char ch = "cafe";
char ch1 = '\ucafe';
//char ch = '\u10100';
//char ch = (char) true;
System.out.println("Hello World!" +ch1);
System.out.println("Hello World!" +ch);

}
}
O/P is:
---------- Run ----------
Hello World!?
Hello World!'


Please tell me,why i am getting "?" and "'" symbols


U are getting Hello World!' because u have declared ch to hold that value
char ch = '\'';
\ is acting as a escape character
the value held by ch is '
and as far as ch1 is concerned there is no matching unicode value for '\ucafe' that is why a ? is returned
Hope this helps

Sayan
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic