aspose file tools*
The moose likes Beginning Java and the fly likes String explanation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String explanation" Watch "String explanation" New topic
Author

String explanation

Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
i just need some explanation why this is so:

i have:
assuming that var1 and var2 are equal


which works just fine

but if i do this


it outputs an error that somestring has to be initialized so i have to:


for this to work
why is that?
Kia Phia Ben
Ranch Hand

Joined: Dec 16, 2005
Posts: 48
I think you code
String somestring ;

if(var1.equals(var2))
somestring = "something";
error because you not initialized value of variable somestring
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
if i use .equals and i don;t initialize somestring, it outputs an error
but if i use ==, it runs fine even if i don;t initialize somestring
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Maybe you could post your whole test code. I can't seem to duplicate the results you're getting.

If I try


or



They both compile fine. However if I try to use the String like:


or I test identity (x == y)
I get the same error, aString may not have been initialized.
[ March 10, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
that's exactly my point
i did the one you did. it outputs the error
"may not be initialized", if i use .equals but if i just use ==, it prints just fine, even without initializing.

Originally posted by Garrett Rowe:
Maybe you could post your whole test code. I can't seem to duplicate the results you're getting.



or I test identity (x == y)
I get the same error, aString may not have been initialized.

[ March 10, 2006: Message edited by: Garrett Rowe ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
My point was, this generates an error on my machine
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
this did not generate an error for me.


however, this did


Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Your first code generates an error on my machine.

Is it possible that you accidentally include a ; after the if statement?

The reason it is giving you the error is that variables of automatic duration or local variables have to be initialized before they are used. Since you assign the variable aString a value following an if where the condition may or may not be true, it's possible that the variable doesn't get initialized.

Notice that this code compiles without any problem since the condition of the if statement is known to be true at compile-time.


[ March 10, 2006: Message edited by: Keith Lynn ]
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
Originally posted by Keith Lynn:

The reason it is giving you the error is that variables of automatic duration or local variables have to be initialized before they are used. Since you assign the variable aString a value following an if where the condition may or may not be true, it's possible that the variable doesn't get initialized.

[ March 10, 2006: Message edited by: Keith Lynn ]


if this is so,then why does it only outpur an error if i use if(x.equals(y)) and no error when i use (x==y)???
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Like I said, on my machine and on Garrett's machine it does generate an error. I'm not sure why it doesn't generate an error when you compile it.
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
then why does it generate an error oy yours and garrett's machine and not on mine? is that like, a bug in jdk or soemthing?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

An agressively optimizing compiler could turn this:



into this:



at compile time. I am not sure that's legal according to the JLS, but it's certainly a valid optimization. I know Borland's IDE compiler has done similar kinds of nonsense in some contexts; it looks like Shuini's compiler might be doing that. So what are you compiling with, exactly (vendor, version...)


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[EFH]: I am not sure that's legal according to the JLS,

I'm pretty sure it's not legal according to the JLS, but I'm not particularly surprised that some compiler might do it. If x and y were declated final it would be a different story. And Shuini - are you sure you haven't oversimplified the code somehow? E.g. by omitting a "final" declaration? If not, I echo EFH - I would really like to know what compiler this comes from.


"I'm not back." - Bill Harding, Twister
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
my java version is 1.4.2 which i downloaded from Sun and i'm using jcreator le to compile.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Shuini, let's be as specific as possible here. The following code generates errors for me using JDK 1.3_15, 1.4.2_10, and 1.5.0_06:

If you can indeed get it to compile and run, can you please tell us the exact version number it prints?
 
wood burning stoves
 
subject: String explanation