For Programmers: Free Programming Magazines  


Home > Archive > Java Help > January 2008 > Re: declaring an array.. now var-scope problem.....









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: declaring an array.. now var-scope problem.....
maya

2008-01-15, 7:25 pm

Nigel Wade wrote:
> maya wrote:
>
>
>
> String pages[];
>
> ...
>
> pages = new String[] {"page1", "page2", "page3", "page4", "page5", "page6"};
>
>


this worked.. thank you.. however: array is not available outside the
conditional...

String aa = "";
aa = "blue";

String pages[];
if (aa.equals("blue")) {
pages = new String[] {"page1", "page2", "page3", "page4", "page5",
"page6"};
}
System.out.println(pages[0]);
}

get error on out.print line: var pages may not have been
initialized....

??

thank you........


Patricia Shanahan

2008-01-15, 7:25 pm

maya wrote:
....
> String aa = "";
> aa = "blue";
>
> String pages[];
> if (aa.equals("blue")) {
> pages = new String[] {"page1", "page2", "page3", "page4", "page5",
> "page6"};
> }
> System.out.println(pages[0]);
> }
>
> get error on out.print line: var pages may not have been initialized....


At the println call, the value of pages is undefined if aa is not equal
to "blue". You may be clever enough to see that the condition is always
true, but the rules for tracking whether a local variable definitely has
a value are not that clever.

You could initialize pages:

String pages[] = null;

Patricia
maya

2008-01-15, 7:25 pm

Patricia Shanahan wrote:
> maya wrote:
> ...
>
> At the println call, the value of pages is undefined if aa is not equal
> to "blue". You may be clever enough to see that the condition is always
> true, but the rules for tracking whether a local variable definitely has
> a value are not that clever.
>
> You could initialize pages:
>
> String pages[] = null;
>
> Patricia


indeed.....:) thank you very much...

(so I guess

String pages[];

is NOT the same as

String pages[] = null;

;)

Patricia Shanahan

2008-01-15, 7:25 pm

maya wrote:
....
> (so I guess
>
> String pages[];
>
> is NOT the same as
>
> String pages[] = null;

....

Depends on the situation.

For fields in objects, they have very similar effect. A new object
starts out with a series of default values, null for object reference
fields, 0 for arithmetic fields, and false for boolean fields. In
practice, for the commonest encodings of data, those values can be
achieved by bulk initialization of the memory representing the new
object to the all zero bit pattern.

Local variables, however, do not get a fixed initial value by default.
Instead, the compiler does a series of tests that ensure each local
variable is "definitely assigned" before it is used. An initializer on
the declaration is an easy way of achieving definite assignment.

In practice, in your case, I would prefer not to initialize the variable
and instead ensure that the conditional covers all cases, so that the
assignments inside the conditionals amount to definite assignment at the
println.

Patricia
Lew

2008-01-15, 7:25 pm

maya wrote:
> ....
> ....
>
> Depends on the situation.

....

Patricia Shanahan wrote:
> In practice, in your case, I would prefer not to initialize the variable
> and instead ensure that the conditional covers all cases, so that the
> assignments inside the conditionals amount to definite assignment at the
> println.


i.e.,

String [] pages;
if ( aa.equals( "blue" ) )
{
pages = new String[]
{"page1", "page2", "page3", "page4", "page5", "page6"};
}
else
{
pages = null;
}
System.out.println(pages[0]);

Notice that now, as opposed to the version without the 'else' clause, no
matter what logic path the code follows (no matter the value of 'aa', IOW),
'pages' is "definitely assigned".

You should read the JLS:
<http://java.sun.com/docs/books/jls/.../defAssign.html>

--
Lew
Lew

2008-01-15, 7:25 pm

Lew wrote:
> String [] pages;
> if ( aa.equals( "blue" ) )
> {
> pages = new String[]
> {"page1", "page2", "page3", "page4", "page5", "page6"};
> }
> else
> {
> pages = null;
> }
> System.out.println(pages[0]);


This will thrown an NPE if the 'else' clause is taken.

--
Lew
Patricia Shanahan

2008-01-15, 7:25 pm

Lew wrote:
> maya wrote:
> ...
>
> Patricia Shanahan wrote:
>
> i.e.,
>
> String [] pages;
> if ( aa.equals( "blue" ) )
> {
> pages = new String[]
> {"page1", "page2", "page3", "page4", "page5", "page6"};
> }
> else
> {
> pages = null;


If I intended to print at the end, I would be more likely to have a
different, non-null, version:

pages = {"preamble", "intro1"}

> }
> System.out.println(pages[0]);
>
> Notice that now, as opposed to the version without the 'else' clause, no
> matter what logic path the code follows (no matter the value of 'aa',
> IOW), 'pages' is "definitely assigned".
>
> You should read the JLS:
> <http://java.sun.com/docs/books/jls/.../defAssign.html>
>

Lew

2008-01-15, 7:25 pm

Patricia Shanahan wrote:[color=darkred]
> Lew wrote:
>
> If I intended to print at the end, I would be more likely to have a
> different, non-null, version:
>
> pages = {"preamble", "intro1"}
>

Me, too.

--
Lew
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com