Java1作為一種面向對象的編程語言,自1995年以來就一直在快速發展。它的特點是跨平台、高效、安全、可靠性高等。本文將從多個方面對Java1做詳細的闡述。
一、基礎語法
Java1的基礎語法包含了變量、運算符、流程控制、數組等內容。
Java1的變量有三種類型:整型、浮點型和字符型。可以通過聲明變量的方式來給變量賦初始值。例如:
int a = 1; //整型變量 float b = 2.0; //浮點型變量 char c = 'A'; //字符型變量
Java1中的運算符包括算術運算符、比較運算符和邏輯運算符等。例如:
int a = 5; int b = 3; int c = a + b; //算術運算符 boolean d = a > b; //比較運算符 boolean e = a > b && a < 10; //邏輯運算符
流程控制包括條件語句和循環語句。其中,條件語句分為if語句和switch語句,循環語句分為for語句、while語句和do-while語句。
if (a > b) {
System.out.println("a > b");
} else {
System.out.println("a <= b");
}
switch (a) {
case 1:
System.out.println("a=1");
break;
case 2:
System.out.println("a=2");
break;
default:
System.out.println("a is not 1 or 2");
break;
}
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
int i = 0;
while (i < 10) {
i++;
System.out.println(i);
}
int j = 0;
do {
j++;
System.out.println(j);
} while (j < 10);
數組是Java1中非常重要的數據結構,它可以存儲多個相同類型的數據。Java1中的數組分為一維數組和多維數組。
int[] array = new int[3]; //一維數組 array[0] = 1; array[1] = 2; array[2] = 3; int[][] array2d = new int[3][3]; //二維數組 array2d[0][0] = 1; array2d[0][1] = 2; array2d[0][2] = 3; array2d[1][0] = 4; array2d[1][1] = 5; array2d[1][2] = 6; array2d[2][0] = 7; array2d[2][1] = 8; array2d[2][2] = 9;
二、面向對象
Java1是一種面向對象的編程語言,它有類、對象、繼承、多態等特性。
類是Java1中的基本單位,每個類都包含了屬性和方法。
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void study() {
System.out.println("學生" + name + "正在學習");
}
}
通過類可以創建出對象,對象可以調用類中的方法。
Student s = new Student();
s.setName("Tom");
s.setAge(18);
System.out.println(s.getName()); //輸出Tom
System.out.println(s.getAge()); //輸出18
s.study(); //輸出學生Tom正在學習
繼承是Java1面向對象編程的一大特點,子類可以繼承父類的特性。
public class GraduateStudent extends Student {
public void attendLecture() {
System.out.println("研究生" + getName() + "正在上課");
}
}
多態是Java1的面向對象編程中非常重要的特性之一,它可以讓父類對象指向子類實例。
Student s = new GraduateStudent();
s.setName("Mary");
s.setAge(23);
System.out.println(s.getName()); //輸出Mary
System.out.println(s.getAge()); //輸出23
s.study(); //輸出學生Mary正在學習
((GraduateStudent) s).attendLecture(); //輸出研究生Mary正在上課
三、IO操作
Java1中的IO操作包括文件的讀寫、網絡通信等內容。
文件的讀寫可以通過Java1的File類和相關的輸入輸出流實現。
import java.io.*;
public class Test {
public static void main(String[] argc) {
File file = new File("test.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
osw.write("Hello World!\n");
osw.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
isr.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
網絡通信可以通過Java1的Socket和ServerSocket實現。
import java.net.*;
import java.io.*;
public class Server {
public static void main(String args[]) {
ServerSocket server = null;
Socket socket = null;
try {
server = new ServerSocket(6666);
System.out.println("服務器啟動成功...");
socket = server.accept();
System.out.println("客戶端連接成功...");
} catch (IOException e) {
e.printStackTrace();
}
try {
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
isr.close();
is.close();
socket.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class Client {
public static void main(String args[]) {
Socket socket = null;
try {
socket = new Socket("localhost", 6666);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw);
pw.write("Hello World!");
pw.flush();
pw.close();
osw.close();
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
四、並發編程
Java1中的並發編程是一項非常重要的技術,能夠提高程序的運行效率。
Java1中的並發編程包括線程、鎖、同步、線程池等內容。
線程是Java1中的基本單位,它可以獨立運行並執行代碼。Java1中可以通過繼承Thread類或實現Runnable接口來創建線程。
public class MyThread extends Thread {
public void run() {
System.out.println("Hello World!");
}
}
public class Test {
public static void main(String[] argc) {
MyThread thread = new MyThread();
thread.start();
}
}
鎖是Java1中的重要概念,可以通過synchronized關鍵字來實現。
public class SyncTest {
private int count = 0;
private synchronized void add() {
count++;
}
public synchronized void testSync() {
for (int i = 0; i < 100000; i++) {
add();
}
System.out.println("count=" + count);
}
}
public class Test {
public static void main(String[] argc) {
SyncTest syncTest = new SyncTest();
new Thread() {
public void run() {
syncTest.testSync();
}
}.start();
new Thread() {
public void run() {
syncTest.testSync();
}
}.start();
}
}
同步可以通過Lock對象來實現。
import java.util.concurrent.locks.*;
public class SyncTest {
private int count = 0;
private Lock lock = new ReentrantLock();
private void add() {
lock.lock();
try {
count++;
} finally {
lock.unlock();
}
}
public void testSync() {
for (int i = 0; i < 100000; i++) {
add();
}
System.out.println("count=" + count);
}
}
public class Test {
public static void main(String[] argc) {
SyncTest syncTest = new SyncTest();
new Thread() {
public void run() {
syncTest.testSync();
}
}.start();
new Thread() {
public void run() {
syncTest.testSync();
}
}.start();
}
}
線程池是Java1中用來管理線程的重要工具。
import java.util.concurrent.*;
public class ThreadPoolTest {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
executorService.submit(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + "正在執行任務");
}
});
}
executorService.shutdown();
}
}
總結
本文對Java1做了基礎語法、面向對象、IO操作、並發編程四個方面的闡述,從多個角度深入探討了Java1的使用。
原創文章,作者:QKCB,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/131698.html
微信掃一掃
支付寶掃一掃