• 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 values

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

are the following 3 valid uses of string ?

String s ="i'm a string";
String s = (String) 'i'm a string';
String s = null;

cheers

chris
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s = "i'm a string"; -- valid
String s = (String) 'i'm a string'; -- not valid , invalid char, no ''
String s = null; -- valid, can always set an object to null


cheers Rille
 
Chris Davies
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s = "i'm a string"; -- valid
String s = (String) 'i'm a string'; -- not valid , invalid char, no ''
String s = null; -- valid, can always set an object to null


so you can't expicitly cast an object to a value ? is it because string is not a wrapper class ?

cheers

chris
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris,

Did you try to compile your code snippet? A few compiler errors are worth a thousand words of explanation in a forum; OK, maybe only a hundred.

In any case, your example 2 uses single quotes.

Let me ask you (pull out your compiler please) is this one valid?

String s = (String) "i'm a string";
 
Chris Davies
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i see it. its what i originally thought but number 2 threw me a bit in terms of casting.

cheers

chris
 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic