Win a copy of Rust Web Development this week in the Other Languages 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

why in x=x++ x keep not changed?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't under in x=x++ x keep not changed?
and another question:
class Process {
byte b=127;
Process() {
this.methodA();
}

void methodA() {
System.out.println("Value of b is = " + b );
}

public static void main(String [] args) {
Processor p = new Processor();
}
}

class Processor extends Process {
byte b=126;

Processor() {
System.out.println("Value of b = " + b);
}

void methodA() {
System.out.println("Value of b = " + this.b);
}
}
What is the Output?
1.Prints Value of b = 0 and Value of b is = 126.
2.Compile-time error occurs.
3.Prints Value of b = 126 and Value of b = 126.
4.Prints Value of b = 127 and Value of b = 126.
why answer is 1--Prints Value of b = 0 and Value of b is = 126.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have added line numbers to explain the exeution sequence.
When you create a new Processor(), the super() is invoked, from Process, when you invoke this.method it will go to Processor method, at that time the instance of the processor is not initialized, so the default value of b =0 is printed. Then the remaining part of super() is exeuted.(i.e SOP).
Next, the instance variables are intialiazed in Processor.(b = 126), then the SOP is printed b = 126.


class Process {
byte b=127;
Process() { //(2)
this.methodA();
System.out.println("Constructor in Super");
}
void methodA() {
System.out.println("Value of b is = " + b );
System.out.println("superMethod");
}
public static void main(String [] args) {
Processor p = new Processor();
}
}
class Processor extends Process {
byte b=126;
Processor() { //(1)
System.out.println("Sub constructor");
System.out.println("Value of b = " + b); //(4)
}
void methodA() { //(3)
System.out.println("Sub method");
System.out.println("Value of b = " + this.b);
}
}


Refer JLS chapter 12.5
http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44670
Hope this helps,
Vanitha.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for your first question refer this discussion http://www.javaranch.com/ubb/Forum24/HTML/009768.html
 
jack zhu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much,Vanitha.
But I still have a question:
The statement x=x++ ,why x keep no change?
 
jack zhu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Anshul Manisha! I have learn more.Thanks again!
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x=x++; ???
when u execute this statement u'll find no change in the value of (x), this is bcuz in this statement it will first execute (x=x) and then (x++). but there will be no change in the value of (x) bcuz final value of x is (x=x) that was assigned b4 (x++).
clear??? or need more explanation.......
===
FAF
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic