• 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

Question on warnings/error

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a doubt on Questions in the SCJP exams regarding difference between warning and error.

Here is a sample Question:

public class GeneralTest
{
public static void main(String arg[])
{
int x=10;
int y;
if (x<100) y=100;
if (x>100) y= x*10;
System.out.println(y);
}
}

This simple program results in compile time error :
GeneralTest.java:9: variable y might not have been initialized
System.out.println(y);
^
1 error

But I find in a practice exam, four choices as

1.The code will compile with a warning that variable y might not have been initialized
2.The code will compile without any warnings.
3.None of the above.

Please suggest the appropriate answer. As there are no warnings in the code I am confused which option to select.

Thanks & Regards
cml
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This code will not compile

Because we did not initiate value y,

variable y might not have been initialized


This is only correct check and tell me

Rgs
k.krishnamoorthy
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
The answer is 3. "None of above" as it will give no warnings neither the code compiles...
 
cm lak
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
---------------------------------------------------------------------------
Hi,


This code will not compile

Because we did not initiate value y,

variable y might not have been initialized


This is only correct check and tell me

Rgs
k.krishnamoorthy
---------------------------------------------------------------------------

yes, I also agree that the code will not compile.

This is a bit tricky...So as the code does not even compile one can go ahead choosing "None of the Above" ..right??
 
cm lak
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi cherish,

Thanks for your response.
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi cm lak,

There is a case that validates the compilation failure, which may be misleading you. The sample code:

looks like y is being set to 100 or x * 10.

*But*, the key here, is what if x == 100? What's the value of y in this case? Uninitialized.

Hope this helps.
Aloha,
Doug
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy cowboys,

Doug Slattery wrote:


*But*, the key here, is what if x == 100? What's the value of y in this case? Uninitialized.



The compiler is even stricter with it, example:


Compiler complains about your abilities to program:

variable i might not have been initialized


If you want i to be initialized, you have to have an else clause:

Now it compiles and prints seven.



Back to the question:
cm lak asked:


I have a doubt on Questions in the SCJP exams regarding difference between warning and error.



A compile with a warning is still a sucessfull compile.
You e.g. get a warning when you use a collection non-generically and try to add something to it. This is not type safe, so you get a warning, but the code compiles and runs. Perhaps without an exception.



Yours,
Bu.
 
Doug Slattery
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Burkhard Hassel:

A compile with a warning is still a sucessfull compile.



To expand on Bu's reply, it is good practice to look at every warning and make sure it makes sense. It also doesn't hurt to add a comment where the warning occurred so your brothers in arms won't have to waste time re-justifying it when they encounter it.

I've seen (and had to clean up) a lot of sloppy code from careless programmers in the past that would make your head explode .

Aloha,
Doug
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code compiles fine!
 
cm lak
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This code compiles fine!

code:
--------------------------------------------------------------------------------

void method { final int x = 10; // added final
int y;
if (x<100) y = 100;
if (x>100) y = x*10;
System.out.println(y);}

--------------------------------------------------------------------------------



can anybody explain this?
[ September 26, 2007: Message edited by: cm lak ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quote: This code compiles fine!

code:
--------------------------------------------------------------------------------

void method { final int x = 10; // added final
int y;
if (x<100) y = 100;
if (x>100) y = x*10;
System.out.println(y);}
--------------------------------------------------------------------------------
can anybody explain this?
-----------------------------

My take on this is: Declaring a local variable final makes compiler happy regarding the "local variable might not have been initialized".Now compiler will not be worried about the initialization of the local variable.
 
cm lak
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


original program


public class GeneralTest
{
public static void main(String arg[])
{
int x=10;
int y;
if (x<100) y=100;
if (x>100) y= x*10;
System.out.println(y);
}
}

This simple program results in compile time error :
GeneralTest.java:9: variable y might not have been initialized
System.out.println(y);
^
1 error





Modified by Ahmed


void method
{
final int x = 10; // added final
int y;
if (x<100) y = 100;
if (x>100) y = x*10;
System.out.println(y);
}


but here x is declared final and the previous error was on y!!!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
reply for both the problems from Ahmed & cm
1. variable y error. whenever you initialize (first time) a variable it should not be in 'if' statement. because the uncertainty of the program (whether 'if' is true or false). or write a 'else' statement too.

2.if we change x as 'final'. Compiler looks at the condition in 'if' where
variable x is final. And thats ok for compiler. If you have a final (which is initialized) variable in 'if' then program compiles file.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi cm lak !!

In the original program it shows error on variable y
as below


This simple program results in compile time error :
GeneralTest.java:9: variable y might not have been initialized
System.out.println(y);
^
1 error




and wen the variable x is set to final it compiles
and you said

but here x is declared final and
the previous error was on y!!


please can anyone explain why this behaviour,

previously also it must have shown compiler error
on the variable x.

Thanks in advance.
 
reply
    Bookmark Topic Watch Topic
  • New Topic