• 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 explanation

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My point was, this generates an error on my machine
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this did not generate an error for me.


however, this did


 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...)
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my java version is 1.4.2 which i downloaded from Sun and i'm using jcreator le to compile.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
reply
    Bookmark Topic Watch Topic
  • New Topic