Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the output here..

 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I got it from Deepak Bala's website.
Can anyone tell me what is the output?

class arraymock
{
public static void main(String args[])
{
int[] arr={1,2,3,4,5};
int[] aar2=new int[4];
arr2=arr; //cannot find symbol in arr2=arr
System.out.println(arr2[4]); //cannot find symbol in arr2[4]
}
}

when i tried this i got these 2 errors,but the answer given is it prints 5.

if this is true ,please clear me ...


Thanks in advance

Preparing scjp 1.5
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code should run perfectly and give output as 5
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
agaign i tried the same code ,now its giving 5. I am just checking the code but couldnt find where i did wrong.
 
Ravissant Markandey
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Preetha,
Go through your code again, the array variable names are different(arr /aar).

regards,
Ravissant Markenday
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravissant you got me! That was brilliant. I read the code three times and could not recognize that error....
 
Ravissant Markandey
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies, i meant arr2 and aar2.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks now i got.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic