Hello,
I am just starting out with Java. But I had a big puzzle when I am in the first chapter.
It says that we have to "initialize" the handle when we are creating one, like String s = "asdf",
And later it says we have create a new object for the String s again, String s = new String("asdf");
I think the String s already has an object (asdf) in the "initialization, but why we still have to re-create it again(if I am understanding it right)?
Hope someone can explain this more to me, in book it is just skipped. Thanks a lot.
kenadams
0
Newbie Poster
Recommended Answers
Jump to Postehm ... you don't "have to", not according to Java anyway, maybe it's a part of the exercise you are reading?
anyway, the following code:
String s = "asdf"; String s = new String("asdf");
would not compile, since you are declaring two variables with an identical …
All 4 Replies
stultuske
1,116
Posting Maven
Featured Poster
kenadams
commented:
Yeah, now I kinda figured that out.. But really I didn't realise that the book is trying to show me two different examples for a same case...Maybe I took a leap in picking a Java book for myself XD
+0
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
kenadams
commented:
Okayyy, I think your answer just removed my mist. So in the normal case, we would most likely use String s1 ="asdf", right?
+0
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
kenadams
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.