• 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 add null to begining

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I have following problem

when I print value of name it gives output as nulltest. How it is? and suppose I want to only test as output what should I do?
Thanks in advance
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't initialised the name field, which retains its default value of null. So your catenation call is equivalent to creating a String "null" which you are immediately adding the text "test" to.
 
vikas mhatre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sir, But I don't want that null then how should I do that?
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Initialize your String with something. Like,
 
vikas mhatre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I don't want to initialize that string, then in that case will it possible?
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vikas mhatre wrote: I want to only test as output what should I do?




try this.......



 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
without initializing the string.........




output is
test
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why line no 16 is ging compile time error.....





can anybody cmment
 
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

shanky sohar wrote:why line no 16 is ging compile time error.....

can anybody cmment



Code must be in a block -- either an initializer, constructor, or method. The only code that is allowed, in a class, but not in a code block, are declarations.

Henry
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To take Henry's reply one step further...when exactly would that code run? It's not part of the main() method, it's not part of the test() method. It's not part of any constructor. It's just kind of hanging out somewhere, with nobody using it.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to take it further (and pretend i have profound knowledge )

Always remember, classes are abstract entities(not to be confused with abstract classes). They are not supposed to execute any code or have sideeffects. I am not sayint they dont, but they are not supposed to. So this is one of the reason, most of the executable statements are tried to be avoided.

A class is just like a class which you would attend in school. Makes no sense, has nothing in real life except a schedule attached on the college website. It becomes real only if at least one student attends it. If no one attends, it should be as if the class never existed. Just like a good college will make sure thie resources which were to be utilized by this class were given to other classes, a good VM will do exactly the same. In such cases, construction side effects will create problems

Theoretically speaking
Always remember, declarations are memoryless statements. Definitions are memory statements. Even though internally they are executables, from the outside perspective, they just assign values. They have no strict rule about when they are initialized. All that is guaranteed is that they will be initialized before you actually need to use them.
In order to guarantee this, java has stated in its specifications, when the class will be loaded(and thus when the static fields will be initialized)> But this is not what a good programmer should rely on. You should thank god that java has such a specification. There are some languages which have neither.

As a programmer,
1) all executables have to be enclosed inside method bodies.
2) if you perform complex logic for initializing fields, it makes sense to put them in constructors or static initializer blocks(for static fields)

All said and done... as a conflicting argument
1) Declarations do take up space albeit very small(computer just doesnt remember what names variables have or stuff)... but this is taken care of by the linker(or prolly JVM in this case... wil edit if some language guru tells me... bin a while since i did this)
2) Even declarations are executable statements... but only with respect to the compiler. Not for us. Again this is hidden away from us.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gaurav Raje wrote: . . . A class is just like a class which you would attend in school. . . .

No, it isn't. The word "class" can mean "type or "sort", as well as "lesson".
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aye aye sherriff.... you know how to create analogies better... but i hope i attempted to make a valid point
reply
    Bookmark Topic Watch Topic
  • New Topic