• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please explain its output

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A
{
int i;
A(int v)
{
add(v);
System.out.println("A");}
void add(int a)
{
i=+a;
System.out.println("A_add");
System.out.println("i="+i);

}

void print()
{
System.out.println("i="+i);
}
}

class B extends A
{

B(int v)
{
super(v);
add(v);}
void add(int a)
{
i=+a*2;
System.out.println("B_add");
System.out.println("i="+i);
}
}

class C
{
public static void main(String args[])
{
B b=new B(2);
//b.print();
}

}



output is B_add
i=4
A
B_add
i=4


why does we getting i = 8 second time please explain ???
 
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags while posting any code here, since the code becomes very difficult to read in the absence of the code tags.

Rakesh Bagaria wrote:why does we getting i = 8 second time please explain ???


Couldn't understand your question. The output that you have mentioned above does not have i=8.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

In your code there are two places where you are writing the following:

i=+a;
i=+a*2;

Did you mean += instead of =+? If not, then why are you writing the +?
 
Rakesh Bagaria
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i mean
i=i+a;

i=i+a*2;

as i think value of i for second time should be 8 but its not so why please explain
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since, we are creating an instance of B... Then in Constructing the B's instance, the method "in" B are being invoked!
 
Rakesh Bagaria
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i am agree with you instance of B will be created but i want to ask that why I get i=4 both time as first time add() function has called by cunstructer of A , it assigned the value to i and as next time cunstructer of B called add(), the value of i should be updated to 8 as i think but still it is gives i=4 as output. please explain this .
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with this....

Did you mean += instead of =+? If not, then why are you writing the +?




The only problem over here is the sign "+"



is equal to



and it is not equal to



I hope you'll get it.
 
Rakesh Bagaria
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot i got it.
 
Rakesh Bagaria
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why should it not showing error when i wrote i=+a*2 at compile time
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rakesh Bagaria wrote:i mean
i=i+a;

i=i+a*2;

as i think value of i for second time should be 8 but its not so why please explain



For

i=i+a; for this you can write in i+=a not the i=+a.

becouse i=+a mean your only try to assign the value 'a' of a to i and your specifing the sign of that.
thats way the your answer comming same in both the time.

use the i+=a then it will work properly..
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rakesh Bagaria wrote:why should it not showing error when i wrote i=+a*2 at compile time



As a matter of syntax, there wont be any error. Compiler doesnt matter about signs.

You can also try giving "-" instead of "+".

Logically its not correct, i mean its not advisable.
 
swapnil kachave
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aditya Kanitkar wrote:

Rakesh Bagaria wrote:why should it not showing error when i wrote i=+a*2 at compile time



As a matter of syntax, there wont be any error. Compiler doesnt matter about signs.

You can also try giving "-" instead of "+".

Logically its not correct, i mean its not advisable.



if you output the '-' then check the value it will be '-2'
 
Marshal
Posts: 79273
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you work out that question for yourself, or did it come from somewhere else?
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swapnil kachave wrote:if you output the '-' then check the value it will be '-2'



I know that, this will be the out, but what i'm telling is, there will never be any error regarding
the sighns you append to the variable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic