• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

String class meaning

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

my question is :

String class is not java primitive type,right
it is one java class but use it same as primitive patteren,
like,
String s1 = "abc";
it does not give "nullpointerxaception"
why that exception not throws even we not use "new" keyword ?

pls, send me reply

thanks
 
Ranch Hand
Posts: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rekha...


have a look at the API...for the function intern()...in java.lang.String


Regards
 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From white paper:

http://java.sun.com/docs/white/langenv/index.html


Although strings are Java programming language objects, Java compiler follows the C tradition of providing a syntactic convenience that C programmers have enjoyed with C-style strings, namely, the Java compiler understands that a string of characters enclosed in double quote signs is to be instantiated as a String object
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any string that you create in java will be a treated as object..... for example as
String str1 = "abc"; implicitly the string "abc" will be an object!!...
and also this is the short-hand form to create any immutable string .

you can create a string in two ways:
1) String str1 = "abc";
2) String str1 = new String("abc");

the difference is that , for the first , you are creating just only one object (i.e "abc") and for the second , you are creating two objects (i.e str1,a reference object and "abc");

thats it... you can use either way......
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the difference is that , for the first , you are creating just only one object (i.e "abc") and for the second , you are creating two objects (i.e str1,a reference object and "abc");



Madhu, are you saying that in the first case str1 reference object is not being created before its initialization?
 
machines help you to do more, but experience less. Experience this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic