• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String add null to begining

 
vikas mhatre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Sir, But I don't want that null then how should I do that?
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initialize your String with something. Like,
 
vikas mhatre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I don't want to initialize that string, then in that case will it possible?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
why line no 16 is ging compile time error.....





can anybody cmment
 
Henry Wong
author
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12144
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic