java程序计数器存的什么

Python022

java程序计数器存的什么,第1张

java中的程序计数器,确切的来说是jvm中的程序计数器:程序计数器是一块较小的内存空间,它的作用可以看作是当前线程所执行的字节码的行号指示器,内存中的一块空间

而 指向下一条指令地址 这个程序计数器,是指的cpu中的程序计数器,是硬件层面的东西,是计算机处理器中的寄存器

参考下面代码:

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

public class Test extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L

private JLabel lbl

private JButton btn1

private JButton btn2

private JButton btn3

private int con

public static void main(String args[]) {

try {

Test test = new Test()

test.setVisible(true)

} catch (Exception e) {

e.printStackTrace()

}

}

public Test() {

super()

getContentPane().setLayout(null)

setTitle("Test")

setName("")

setResizable(false)

setBounds(100, 100, 300, 216)

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

lbl = new JLabel()

lbl.setText(String.valueOf(con))

lbl.setBounds(84, 58, 96, 25)

getContentPane().add(lbl)

btn1 = new JButton()

btn1.setText("+ 1")

btn1.setBounds(29, 106, 64, 26)

btn1.addActionListener(this)

getContentPane().add(btn1)

btn2 = new JButton()

btn2.setText("- 1")

btn2.setBounds(99, 106, 64, 26)

btn2.addActionListener(this)

getContentPane().add(btn2)

btn3 = new JButton()

btn3.setText("Clear")

btn3.setBounds(169, 106, 64, 26)

btn3.addActionListener(this)

getContentPane().add(btn3)

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == btn1) {

con = Integer.parseInt(lbl.getText())

con++

lbl.setText(String.valueOf(con))

}

if (e.getSource() == btn2) {

con = Integer.parseInt(lbl.getText())

con--

lbl.setText(String.valueOf(con))

}

if (e.getSource() == btn3) {

lbl.setText(String.valueOf(0))

}

}

}

public class Count{ int countValueCount(){ countValue=0} public void increment() { countValue++ } public void decrement() { countValue-- } public void reset() { countValue=0 } public int getCountValue(){ return countValue} public static void main(String args[]){ Count c = new Count()c.increment()System.out.println(c.getCountValue())c.reset()System.out.println(c.getCountValue())} } 运行结果: 1 0

采纳哦