Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

danchisholm.net exam3 Question 6

 
Dmitry Golynkin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi friends. It seems that must be an error because method variable d doesn't initialized, but the code compiles fine. WHY?
[Code tags added by Val]
[ August 19, 2002: Message edited by: Valentin Crettaz ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables are not required to be initialized as long as they are not used within the method's body. In this case, d is not used, and thus, the compiler doesn't need to complain.
For more information, please read JLS 14.4 Local Variable Declaration Statements
 
Dmitry Golynkin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Guru
 
Dmitry Golynkin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:
__________________________________________________
Question 19
1. class Amber {
2. public static void main(String[] args) {
3. int[][] a = {{1,2},{'a'-'a','b'-'a','c''a'},{~0,2&3,1<<1}};
4. Object[] obj = (Object[])a.clone();
5. for(int i = 0;i<obj.length; i++) {
6. int[] ia = (int[])obj[i];
7. System.out.print(ia[i]);
8. }
9. }
10. }
__________________________________________________
What {1,2} and {~0,2&3,1<<1} are doing in the array initialization? ARE they array members?
[ August 19, 2002: Message edited by: Dmitry Golynkin ]
[ August 19, 2002: Message edited by: Dmitry Golynkin ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, you should start another discussion when posting a new question unrelated to the one already under discussion.
Let's rewrite this:

As you can see, the array "a" is a bidimensional array containing integer primitives. The components of the array "a" are initialized by the expressions given in the array initializer (after the '=' sign). Thus, the array after initialization contains the following values:
row 1: 1,2
row 2: 0,1,2
row 3: -1,2,2
Concerning the last subarray:
~0 inverts all bits of 0, which yields -1
2&3, that is, 00000010 & 00000011 yields 2
1 << 1 yields 2
And by the way (Dan?), I think there is a typo in the array initializer (unless the cut and paste didn't work).
Second row, third component, 'c''a' should be something like 'c'-'a'.
[ August 19, 2002: Message edited by: Valentin Crettaz ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Valentin Crettaz:

And by the way (Dan?), I think there is a typo in the array initializer (unless the cut and paste didn't work).
[ August 19, 2002: Message edited by: Valentin Crettaz ]

It must have been a cut and paste problem, because the web page doesn't have the problem.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I thought Dan Keep up the good work...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic