This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

sop gives different output....... why

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all..
here the simple program. see the code and explane why its happening..

public class test {
static int c;
public static void main(String args[]){
int a=10;
int b=5;
String d;
System.out.println(a + b + c); // it gives 15
System.out.println("" +a + b + c); // it gives 1050
System.out.println(a + b + c +""); // it gives 15

}
}

here three diff. outputs are. how the compiler differentiate them... means how the compiler judge when it do addition and when it works like a string opperation.


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

if one operand of + is String then the second one is casted to String.
But if both are integers neither is casted to String, then it is calculation not concatenation.

Here is how statements are evaluated.
System.out.println(((" +a)+ b)+ c)
System.out.println(((a + b) + c) +"");

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

public class test {
static int c;
public static void main(String args[]){
int a=10;
int b=5;
String d;
System.out.println(a + b + c); // it gives 15
System.out.println("" +a + b + c); // it gives 1050
System.out.println(a + b + c +""); // it gives 15

}
}

Lets take the output one by one.
In line one sop a,b,c are just integers ( Remember c is an instance variable so it gets a default value of 0) Hence no problem, its integer addition of a,b,c. Hence the result is 15.

In line two sop, Notice that "" is put before a+b+c.Since operation is performed from left to right the + operator is actually overloaded here as the first operand is a string " ",So it converts the other integers also to strings and uses + operator to just concatenate. Hence the result.

In line three sop, Notice that " " is after the addtion of a,b,c. Hence First the integer addition is performed and then when a string is encountered + gets overloaded and String concatenation takes place.

I hope that should solve your doubt. If not please feel free to get back to me.

Take care,
Aryaan
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope java compiler is doing left precedence rule for the operations
case 1 : a+b+c ===> normal arithmetic operation results 15
(10+5+0=15)
case 2 : ""+a+b+c ===> takes the string operation
"" +10 = "10"
"10" + 5 = "105"
"105"+ 0 = "1050"
case 3 : a+b+c+"" ===> same as first one and then added with
empty string (15 + "" = "15")

Hope this will be useful
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is due to + operator, in java it is implecitly overloaded.
& precedence & association of operators applied here
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic