• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

doubt regarding the output??

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following,
class PassA {
public static void main(String [] args) {
PassA p = new PassA();
p.start();
}
void start() {
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3) {
a3[1] = 7;
return a3;
}
}


the output is given as 15 15 ...so i think the compiler sees from left add the elements if present and if comes accross the string element then if there are any more elements after it then it concatnates with the first result ...

like when i run with ""+a1[0] + a1[1] + a1[2] i get 375

with a1[0]+""+ a1[1] + a1[2] i get 375

with a1[0]+ a1[1]+"" + a1[2] i get 105

with a1[0]+ a1[1]+ a1[2]+"" i get 15


thanks & regards

srikanth reddy
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start scanning from left.
If you add String + long (Or int) the expression result will be String.
If you are adding two long (or int), the result of expression will be long(or int).
e.g. " "+ 3 + 7 + 5
= " 3" + 7 + 5
= " 37" + 5
= " 375"
And 3 + 7 + " " + 5 = 10 5
 
Why is the word "abbreviation" so long? And this ad is so short?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic