本文目錄一覽:
JAVA計數器
都什麼跟什麼啊?這明顯就是application的概念嘛,
application: 有效範圍涵蓋整個應用程序,也就是對整個網站均有效,只要是這個Web應用程序下面的JSP或者Servlet,那麼都能夠訪問到,這個application對象,你可以把內容存在application中,那麼在整個Web應用程序的生命周期中都是可以拿到這個application裏面的內容的,當然服務器重啟,此對象被垃圾回收,必然清零了,下面是代碼,頁面名字叫test.jsp,應你的要求,也做了一個按鈕
——-
%@ page language=”java” import=”java.util.*” pageEncoding=”GBK”%
%@ page contentType=”text/html; charset=GBK”%
%
Integer accessCount = (Integer)application.getAttribute(“accessCount”);
if (accessCount == null) { // 第一次訪問的時候就是一個null
accessCount = new Integer(0);
} else {
accessCount = new Integer(accessCount.intValue() + 1);
}
application.setAttribute(“accessCount”, accessCount);
out.println(“font size=’20’ color=’red'”+accessCount+”/font”);
%
html
head
titleTest Application/title
/head
body
center
form action=”test.jsp”
input type=”submit” value=”點一次就加一次”
/form
/center
/body
/html
試編寫java代碼實現一個計數器類(Computer),其中包括:變量value初始值為0
class Computer{
int value;
Computer(int value){
this.value=value;
}
public void add(){
System.out.println(“Value:”+value+”-“+(value+1));
value++;
}
public void sub(){
System.out.println(“Value:”+value+”-“+(value-2));
value-=2;
}
public void clear(){
System.out.println(“Value:”+value+”-“+0);
value=0;
}
}
public class Demo{
public static void main(String[] args){
Computer computer=new Computer(10);
computer.add();
computer.sub();
computer.clear();
}
}
java程序計數器存的什麼
java中的程序計數器,確切的來說是jvm中的程序計數器:程序計數器是一塊較小的內存空間,它的作用可以看作是當前線程所執行的位元組碼的行號指示器,內存中的一塊空間
而 指向下一條指令地址 這個程序計數器,是指的cpu中的程序計數器,是硬件層面的東西,是計算機處理器中的寄存器,
原創文章,作者:LUOY,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/140732.html