第一段代碼:
目前創(chuàng)新互聯(lián)已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、阿里地區(qū)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
你要先理解 i++ 的意思, 他是先賦值再自增。
和++i剛好相反, 它是先自增再賦值。 理解了吧!
第二段:
你只是在main 方法中定義了 a1..TestStatic 對(duì)象t1...看不到這個(gè)變量,所以不行。
補(bǔ)充:
因?yàn)槟銓?shí)在main 方法中定義的變量類的對(duì)象是看不到的,所以調(diào)用不了,改成這樣就好了:
public class TestStatic {
int a1;
int a2;
public static void main(String[] args) {
TestStatic t1 = new TestStatic ();
TestStatic t2 = new TestStatic();
int b1 = t1.a1++ ;
int b2 = t1.a2++ ;
int b3 = t2.a1++ ;
int b4 = t2.a2++ ;
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
}
}
最簡單的java代碼肯定就是這個(gè)了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是應(yīng)該是所有學(xué)java的新手看的第一個(gè)代碼了。如果是零基礎(chǔ)的新手朋友們可以來我們的java實(shí)驗(yàn)班試聽,有免費(fèi)的試聽課程幫助學(xué)習(xí)java必備基礎(chǔ)知識(shí),有助教老師為零基礎(chǔ)的人提供個(gè)人學(xué)習(xí)方案,學(xué)習(xí)完成后有考評(píng)團(tuán)進(jìn)行專業(yè)測(cè)試,幫助測(cè)評(píng)學(xué)員是否適合繼續(xù)學(xué)習(xí)java,15天內(nèi)免費(fèi)幫助來報(bào)名體驗(yàn)實(shí)驗(yàn)班的新手快速入門java,更好的學(xué)習(xí)java!
1、首先得搞清楚什么叫內(nèi)存泄露,簡單來說就是一個(gè)東西放在內(nèi)存里的時(shí)間太長了,當(dāng)你的程序都跑完了,它還存在那里。這時(shí)它是白白的占用了你的內(nèi)存,累積起來占用的內(nèi)存越來越多……最后就會(huì)導(dǎo)致JVM報(bào)錯(cuò):out of memory。
2、一般情況下,別人如果能指出你的系統(tǒng)(程序)內(nèi)存溢出,這個(gè)人應(yīng)該還是挺厲害的。通常對(duì)于新人來說,喜歡把變量直接定義在class下(此時(shí)稱之為實(shí)例變量,或者成員變量),那么在方法里調(diào)用后,這個(gè)實(shí)例變量是不會(huì)被釋放的,大量的這樣使用就可能會(huì)引發(fā)內(nèi)存泄露。
3、把變量定義在方法里,當(dāng)這個(gè)方法執(zhí)行完畢后內(nèi)存就得到釋放了,這是個(gè)好習(xí)慣。
4、如果想要看到內(nèi)存溢出,可以按這樣的思路去嘗試一下:定義一個(gè)靜態(tài)的實(shí)例變量(list或其它集合),然后在一個(gè)方法里循環(huán)往這個(gè)靜態(tài)變量塞東西,直到這個(gè)實(shí)例變量撐爆你的jvm內(nèi)存。很快你就能看到out of memory……
123456789101112131415161718192021222324import java.util.ArrayList;import java.util.List; public class MemoryTest { private static List list = new ArrayList(); private static int count = 0; public static void main(String[] args) throws InterruptedException { System.out.println("申請(qǐng)前的可用內(nèi)存 = "+getFreeMemory()); while(true){ list.add(new byte[1024*1024]);//用實(shí)例變量申請(qǐng)1M內(nèi)存,當(dāng)方法執(zhí)行完畢時(shí),這個(gè)static的變量是不會(huì)被釋放 count++; if (count % 100 == 0) { System.out.println("當(dāng)前l(fā)ist.size()="+list.size()+",可用內(nèi)存 = "+getFreeMemory()); Thread.sleep(500); } } } public static long getFreeMemory() { return Runtime.getRuntime().freeMemory() / (1024 * 1024); } }
/**
* 將2進(jìn)制字符串置換為10進(jìn)制整數(shù)
* @author developer_05
*/
public class Exam05 {
public static void main(String[] args) {
String strBin = "11000000111001101"; //98765
long num;
num = convert(strBin);
System.out.println(strBin + "(2) : " + num + "(10)");
}
/**
* 將2進(jìn)制字符串置換為10進(jìn)制整數(shù)
* @param strHex 2進(jìn)制字符串
* @return 10進(jìn)制整數(shù)
*/
public static long convert(String strBin){
char[] arr = strBin.toCharArray();
long retValue = 0;
for(int i=0; iarr.length; i++){
retValue = retValue * 2 + (arr[i] - '0');
}
return retValue;
}
}
運(yùn)行演示:
11000000111001101(2) : 98765(10)
import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;public class JEncrytion{
public static void main(String[] argv) {
try{ KeyGenerator keygenerator = KeyGenerator.getInstance("DES"); SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher; // Create the cipher
desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Initialize the cipher for encryption
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey); //sensitive information
byte[] text = "No body can see me".getBytes();
System.out.println("Text [Byte Format] : " + text);
System.out.println("Text : " + new String(text));
// Encrypt the text
byte[] textEncrypted = desCipher.doFinal(text);
System.out.println("Text Encryted : " + textEncrypted);
// Initialize the same cipher for decryption
desCipher.init(Cipher.DECRYPT_MODE, myDesKey); // Decrypt the text
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
System.out.println("Text Decryted : " + new String(textDecrypted));
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(NoSuchPaddingException e){
e.printStackTrace();
}catch(InvalidKeyException e){
e.printStackTrace();
}catch(IllegalBlockSizeException e){
e.printStackTrace();
}catch(BadPaddingException e){
e.printStackTrace();
}
}
}
package?im01.hw.gsy;
import?java.awt.Dimension;
import?java.awt.Toolkit;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?java.io.File;
import?javax.swing.ImageIcon;
import?javax.swing.JFrame;
import?javax.swing.JMenu;
import?javax.swing.JMenuBar;
import?javax.swing.JMenuItem;
import?javax.swing.JScrollPane;
import?javax.swing.JTextArea;
import?javax.swing.JToolBar;
import?javax.swing.KeyStroke;
public?class?JMenuDemo02?{
public?static?void?main(String?args[]){
JFrame?f?=?new?JFrame("Welcom?To?gsh");
JTextArea?text?=?new?JTextArea();
text.setEditable(true);
f.getContentPane().add(new?JScrollPane(text));
JMenu?menuFile?=?new?JMenu("基本信息");
menuFile.setIcon(new?ImageIcon("e:軟件圖片"?+?File.separator?+?"文件夾打開.jpg"));
JMenuBar?menuBar?=?new?JMenuBar();
JMenuItem?newItem?=?new?JMenuItem("新建",new?ImageIcon("e:\\軟件圖片"?+?File.separator?+?"8.jpg"));
JMenuItem?openItem?=?new?JMenuItem("打開",new?ImageIcon("e:\\軟件圖片"?+?File.separator?+?"1.jpg"));
JMenuItem?closeItem?=?new?JMenuItem("關(guān)閉",new?ImageIcon("e:\\軟件圖片"?+?File.separator?+?"3.jpg"));
JMenuItem?exitItem?=?new?JMenuItem("退出",new?ImageIcon("e:\\軟件圖片"?+?File.separator?+?"4.jpg"));
JMenu?menuFile1?=?new?JMenu("進(jìn)貨管理");
menuFile1
.setIcon(new?ImageIcon("e:\\軟件圖片"?+?File.separator?+?"5.jpg"));
JMenuBar?menuBar1?=?new?JMenuBar();
JMenuItem?newItem1?=?new?JMenuItem("新建",new?ImageIcon("e:\\軟件圖片"?+?File.separator?+?"8.jpg"));
JMenuItem?openItem1?=?new?JMenuItem("打開",new?ImageIcon("e:\\軟件圖片"?+?File.separator?+?"1.jpg"));
JMenuItem?closeItem1?=?new?JMenuItem("關(guān)閉",new?ImageIcon("e:\\軟件圖片"?+?File.separator?+?"3.jpg"));
JMenuItem?exitItem1=?new?JMenuItem("退出",new?ImageIcon("e:\\軟件圖片"?+?File.separator?+?"4.jpg"));
newItem.setMnemonic('N');
openItem.setMnemonic('O');
closeItem.setMnemonic('C');
exitItem.setMnemonic('E');
newItem.setAccelerator(KeyStroke.getKeyStroke('N',java.awt.Event.ALT_MASK));
openItem.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.ALT_MASK));
closeItem.setAccelerator(KeyStroke.getKeyStroke('C',java.awt.Event.ALT_MASK));
exitItem.setAccelerator(KeyStroke.getKeyStroke('E',java.awt.Event.ALT_MASK));
menuFile.add(newItem);
menuFile.addSeparator();
menuFile.add(openItem);
menuFile.addSeparator();
menuFile.add(closeItem);
menuFile.addSeparator();
menuFile.add(exitItem);
menuFile1.add(newItem1);
menuFile1.addSeparator();
menuFile1.add(openItem1);
menuFile1.addSeparator();
menuFile1.add(closeItem1);
menuFile1.addSeparator();
menuFile1.add(exitItem1);
menuBar.add(menuFile);
menuBar.add(menuFile1);
f.addWindowFocusListener(new?WindowAdapter(){
public?void?windowClosing(WindowEvent?e){
System.exit(1);
}
});
f.setJMenuBar(menuBar);
//f.setJMenuBar(menuBar1);
f.setVisible(true);
Dimension?dim?=?Toolkit.getDefaultToolkit().getScreenSize();
f.setBounds(0,?0,?dim.width,?dim.height?-?22);
f.setFocusable(true);
//f.setSize(300,200);
//
}
}
這個(gè)可以不,圖片路徑自己加上去,可以不加,那就刪了。列如 刪掉這些new ImageIcon("e:\\軟件圖片" + File.separator + "8.jpg") ? 這里有些我還設(shè)置了快捷鍵,你也可以參考下。
分享題目:java定義一段代碼,編寫一段java代碼
當(dāng)前鏈接:http://chinadenli.net/article14/hohpde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、網(wǎng)站設(shè)計(jì)公司、企業(yè)建站、響應(yīng)式網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)