最簡單的java代碼肯定就是這個了,如下:
創(chuàng)新互聯(lián)始終堅持【策劃先行,效果至上】的經(jīng)營理念,通過多達(dá)10多年累計超上千家客戶的網(wǎng)站建設(shè)總結(jié)了一套系統(tǒng)有效的全網(wǎng)整合營銷推廣解決方案,現(xiàn)已廣泛運用于各行各業(yè)的客戶,其中包括:成都純水機等企業(yè),備受客戶夸獎。
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是應(yīng)該是所有學(xué)java的新手看的第一個代碼了。如果是零基礎(chǔ)的新手朋友們可以來我們的java實驗班試聽,有免費的試聽課程幫助學(xué)習(xí)java必備基礎(chǔ)知識,有助教老師為零基礎(chǔ)的人提供個人學(xué)習(xí)方案,學(xué)習(xí)完成后有考評團進行專業(yè)測試,幫助測評學(xué)員是否適合繼續(xù)學(xué)習(xí)java,15天內(nèi)免費幫助來報名體驗實驗班的新手快速入門java,更好的學(xué)習(xí)java!
package?別踩白塊;
import?java.awt.GridLayout;
import?java.awt.event.MouseEvent;
import?java.awt.event.MouseListener;
import?javax.swing.JFrame;
public?class?Main?extends?JFrame?implements?MouseListener?{
Block[]?block=new?Block[9];
Block1?block1=new?Block1();
public?Main(){
setTitle("別踩白塊");
setBounds(200,300,200,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new?GridLayout(3,3,5,5));
addblock();
bk();
this.setVisible(true);
}
public?void?addblock(){
for(int?i=0;i9;i++)
{
block[i]=new?Block();
add(block[i]);
}
}
public?void?bk(){
block1.setbk1(block[(int)(Math.random()*2)]);
block1.setbk1(block[(int)(Math.random()*2+4)]);
block1.setbk1(block[(int)(Math.random()*2+6)]);
}
public?void?over(){
block[3].setText("g");
block[4].setText("g");
block[5].setText("!");
}
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
new?Main();
}
@Override
public?void?mouseClicked(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?mousePressed(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
if(e.getSource().equals(""));
over();
}
@Override
public?void?mouseReleased(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?mouseEntered(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
}
@Override
public?void?mouseExited(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
}
}
import java.awt.Color;import java.awt.GridLayout;import javax.swing.JButton;import javax.swing.JFrame; public class MiGong extends JFrame{ private static final long serialVersionUID = 1L; private static final int ROW = 8; private static final int COL = 9; int[][] map = { {2,0,1,0,0,0,1,0}, {0,0,1,0,0,0,1,0}, {0,0,1,0,1,1,0,0}, {0,1,1,1,0,0,0,0}, {0,0,0,1,0,0,0,0}, {0,1,0,0,0,1,0,1}, {0,1,1,1,1,0,0,1}, {1,1,0,0,0,1,0,1}, {1,1,0,0,0,0,0,3} }; public static void main ( String args[] ) { new MiGong (); } public MiGong () { setTitle ("迷宮"); setLayout (new GridLayout (ROW, COL, 0, 0)); for ( int h = 0; h COL; h++ ) { for ( int g = 0; g ROW; g++ ) { switch (map[h][g]) { case 0: JButton p = new JButton (); p.setBackground (Color.red); add (p); break; case 1: JButton q = new JButton (); q.setBackground (Color.blue); add (q); break; case 2: JButton m = new JButton ("入口"); m.setBackground (Color.green); add (m); break; case 3: JButton n = new JButton ("出口"); n.setBackground (Color.green); add (n); break; } } } pack (); setLocationRelativeTo (null); setDefaultCloseOperation (EXIT_ON_CLOSE); setVisible (true); }}
1. 局部代碼塊
作用在方法當(dāng)中,作用是控制變量的生命周期:
public void show(){
{
System.out.println("局部代碼塊運行!");
}
}123456
在程序中當(dāng)我們定義完成一個局部變量x之后,并且在接下來的代碼中,不想再用到它時,那么就沒必要讓x在內(nèi)存中繼續(xù)占用空間。因此就有了局部代碼塊。
2. 構(gòu)造代碼塊
作用在類的定義Body中,作用是給類的部分字段統(tǒng)一初始化:
public class Apple {
private String size;
//構(gòu)造代碼塊
{
System.out.println("構(gòu)造代碼塊運行!");
size = "E";
}
}
12345678910
構(gòu)造代碼塊與構(gòu)造函數(shù)的區(qū)別是:構(gòu)造代碼塊是給所有對象進行統(tǒng)一初始化,而構(gòu)造函數(shù)是給對應(yīng)的對象初始化,因為構(gòu)造函數(shù)是可以多個的,運行哪個構(gòu)造函數(shù)就會建立什么樣的對象,但無論建立哪個對象,都會先執(zhí)行相同的構(gòu)造代碼塊。也就是說,構(gòu)造代碼塊中定義的是不同對象共性的初始化內(nèi)容。所以理所當(dāng)然的,構(gòu)造代碼塊在構(gòu)造函數(shù)之前執(zhí)行。
3. 靜態(tài)代碼塊
作用有兩個:
(1)給類的靜態(tài)變量賦值;
(2)聲明靜態(tài)變量;
作用在類的Body中,對類中的靜態(tài)變量初始化:
public class APP {
static int x, y; // 靜態(tài)變量
static {
x = 5; // 給靜態(tài)變量x賦值
}
public static void myMethod() {
y = x++ + ++x; // x++ 先使用x的值再加1;++x先加1再使用x的值
}
public static void main(String[] args) {
x--;
myMethod();
System.out.println(x + y + ++x);
}
}
輸出:23