File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[eckel] on string initialization

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In eckel, chap4; what is the answer to:
What is the difference between the (S1) String field initialized at point of definition and (S2) one initialized by the constructor ?
My guess on the answer would be that S1 is initialized FIRST prior to S2. What if I created another String field which is 'final', will that get initialized first instead, if it is also initialized at point of definition ?
Pho
 
Allen Alchian
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to read the postings at this thread. It gives an excellent response by Stephanie Grasson. http://www.javaranch.com/ubb/Forum33/HTML/000601.html
Allen
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allen,
I think what Pho is asking is about the difference between
class Hi {
String s = "HI"
}
and
class Hi {
String s;
public void Hi() {
s = "Hi"
}
}
 
Allen Alchian
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, Randall, you're right. I guess I read the question just too fast. I don't know the answer to Pho's question; it's a good question. I hope someone knows the answer 'cause I'd like to know, too.
Allen
 
Rajendra Deshpande
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allen,
My curiosity with the URL you posted took me to that wonderful discussion on 'String'. I am given to understand that everything in Java is an object. However, the string 'literal' that figures in the discussion has me confused. Is this 'literal' also an object in true sense. Please clarify.
thanks,
Raj.
 
Lalit Sha
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajendra,
In java, Strings are considered to be objects. String objects can be made in two ways either using the new modifier(like other objects) or without the new modifier(like other primitives).Memory storage for String reference variables are same like other object references.
Lalit.
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pho Tek,
It has been a while since I studied this so I waited for someone else to answer. The way I remember is that when you create an object, first super() is called, then declaration/assignments and initializers, lastly the rest of the constructor. So S1 will be initialized before S2.
------------------
Dont blindly believe everything I say.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your "attempts" at answering.
I'm going to read the Java language spec and see if there's anything in there. Also does anybody know how to track the progress of the initializations of members via reflection (is that possible) - so that I can verify my conjenctures..
Pho
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pho,
I can't tell if you are asking about the order that these get created or if you are asking the difference between saying
String s = "XYZ";
and
String s = new String("xyz");
So I'll try for both.

The order of execution is:
1 - static initializer blocks
2 - initializer blocks
3 - constructors.
4 - main()

Rajendra
Objects are created on the heap. Garbage collection is done to keep the heap clean. When you use the "new" operator you are creating an object.
Variables are created in the stack. Each method has it's own stack. If it is a local variable, when the method completes, the stack for that method is discarded. The garbage collector does not do this. The variable s will be discarded whenever the variable goes out of scope.
However the String literal "XYZ" will get created at class load time in the Constant Pool, the garbage collector will not get rid of that. String Literals are NOT objects. They do not exist on the heap.
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pho,
The JLS is the place to look. It describes in detail the order.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic