Java Programmer Interview Gold--i++

Source: Internet
Author: User

What is the output of the following program?
public class program2 {static {int x = 5;} static int x, y; public static void main (string[] args) {x--; MyMethod (); SYSTEM.OUT.PRINTLN (x + y++ + x); } private static void mymethod () {y=x++ + ++x;}}     
The result is 2. This problem if do not understand the following knowledge point is difficult to do ah, do not believe can run try
static变量(全局变量)的特性各种属性的加载顺序i++,++i的区别
Properties of the static variable
I. Static member variables1. belongs to the entire class instead of an object instance, so it can be called directly from the class name and object name.2. Static members belong to the entire class, and when the system first uses the class, it allocates memory space until the class is unloaded for resource reclamation two. The property load order is normal, the execution order is as follows: The parent class static variable, the parent class static code block, the subclass static variable, the subclass static code block, the parent class non-static variable, The parent class is non-static code block, parent class constructor method, subclass non-static variable, subclass non-static code block, subclass constructor method. Three. I++,++i the difference between this is Baidu bar next talk about the code flowPublicClassPROGRAM2 {static {int x = 5;//declares a local variable, has no effect on the back} static int X,y;//default 0 public  Static void main (String[] args) {x-- ; //x=-1 MyMethod (); SYSTEM.OUT.PRINTLN (x + y++ + x); //1+0+1} private static void mymethod () {y=x++ + ++x;//equivalent to y= (x + +) + (++x) from right to left y = 0+ 0 x = 1; } } 


Welcome to join the Learning Exchange Group 569772982, we learn to communicate together.

Java Programmer Interview Gold--i++

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.