Java yog hom lus-hom phiaj tsim hom lus tsim xyoo 1995 los ntawm James Gosling. Ntawd yog, cov lus nthuav qhia cov ntsiab lus raws li "khoom" nrog "thaj chaw" (piv txwv li cov yam ntxwv uas piav qhia cov khoom) thiab "cov txheej txheem" (kev ua uas cov khoom tuaj yeem ua tau). Java yog "sau ib zaug, khiav txhua qhov chaw" lus. Ntawd yog, cov lus tau tsim los khiav ntawm txhua lub platform uas muaj Java Virtual Machine (JVM). Vim tias Java yog cov lus programming uas siv ntev heev, nws yooj yim rau cov pib kawm thiab nkag siab. Cov lus qhia hauv qab no yog qhia txog kev sau cov haujlwm nrog Java.
Kauj ruam
Txoj Kev 1 ntawm 3: Sau Prime Java Programs
Kauj Ruam 1. Txhawm rau pib sau cov haujlwm nrog Java, txheeb xyuas koj thaj chaw ua haujlwm
Ntau tus programmer siv Kev Txhim Kho Ib Leeg Ib puag ncig (IDE) xws li Dab noj hnub thiab Netbeans rau Java programming, tab sis peb tuaj yeem sau Java cov haujlwm thiab sau lawv yam tsis muaj IDE.
Kauj Ruam 2. Txhua hom phiaj xwm zoo ib yam li Notepad yuav txaus rau kev ua haujlwm nrog Java
Hardline programmers qee zaum nyiam cov ntawv sau ua hauv lub davhlau ya nyob twg, xws li vim thiab emacs. Cov ntawv nyeem muaj zog uas tuaj yeem teeb tsa ntawm ob lub Windows thiab Linux-based computers (Mac, Ubuntu, thiab lwm yam) yog Sublime Text. Nws yog cov ntawv sau uas peb yuav siv hauv qhov kev qhia no.
Kauj Ruam 3. Nco ntsoov tias koj tau teeb tsa Java Software Development Kit
Koj yuav xav tau nws los sau koj qhov program.
-
Ntawm Windows-based systems, yog tias qhov hloov pauv ib puag ncig tsis sib xws, koj yuav ntsib kev ua tsis tiav thaum ua haujlwm
javac ua
- . Nyeem kab lus Yuav Ua Li Cas Txhim Kho Java Software Development Kit kom paub meej ntxiv txog kev teeb tsa JDK kom zam qhov ua yuam kev no.
Txoj Kev 2 ntawm 3: Nyob Zoo Hauv Ntiaj Teb
Kauj Ruam 1. Ua ntej tshaj plaws, peb yuav tsim ib txoj haujlwm uas qhia cov lus "Nyob Zoo Ntiaj Teb
"Hauv koj cov ntawv sau, tsim cov ntawv tshiab thiab khaws nws nrog lub npe" HelloDunia.java ". HelloDunia yog koj lub npe chav kawm thiab npe chav kawm yuav tsum zoo ib yam li koj lub npe ntaub ntawv.
Kauj Ruam 2. Txheeb cov chav kawm tseem ceeb thiab cov txheej txheem
Txoj kev loj
pej xeem zoo li qub tsis muaj dab tsi tseem ceeb (Txoj hlua args)
yog ib txoj hauv kev uas yuav ua tiav thaum txoj haujlwm tau ua haujlwm. Txoj hauv kev tseem ceeb no yuav muaj tib txoj kev tshaj tawm hauv txhua txoj haujlwm Java.
public class HelloWorld {pej xeem zoo li qub tsis muaj dab tsi tseem ceeb (Txoj hlua args) {}}
Kauj Ruam 3. Sau kab kab ntawv uas yuav tshwm "Hello World
System.out.println ("Nyob Zoo Ntiaj Teb.");
-
Ua tib zoo saib cov khoom ntawm kab no:
-
Txheej txheem
- qhia lub kaw lus kom ua ib yam dab tsi.
-
tawm
- qhia rau lub system tias peb yuav tsim cov zis.
-
println
- luv rau "print line". Yog li, peb qhia cov kab ke kom pom kab hauv qhov tso tawm.
-
Cov lus hais tawm ntawm
("Nyob zoo ntiaj teb.")
txhais tau tias txoj kev
System.out.println ()
dhau hauv qhov ntsuas, uas, hauv qhov no, yog Txoj hlua
"Nyob Zoo Ntiaj Teb."
-
-
Nco tseg tias muaj ntau txoj cai hauv Java uas yuav tsum tau ua raws:
- Koj yuav tsum tau ntxiv ib semicolon (;) kawg ntawm txhua kab.
- Java yog cov ntaub ntawv rhiab. Yog li koj yuav tsum sau lub npe txheej txheem, lub npe hloov pauv, thiab lub npe chav kawm hauv cov tsiaj ntawv raug lossis koj yuav ua tsis tiav.
- Thaiv cov cai uas tau hais tshwj xeeb rau ib txoj hauv kev tshwj xeeb lossis lub voj tau muab tso rau hauv kab zauv.
Kauj Ruam 4. Muab txhua yam tso ua ke
Koj qhov kev pab cuam Halo World zaum kawg yuav tsum zoo li cov hauv qab no:
pej xeem chav HelloWorld {pej xeem zoo li qub tsis muaj dab tsi tseem ceeb (Txoj hlua args) {System.out.println ("Nyob Zoo Ntiaj Teb."); }}
Kauj Ruam 5. Khaws koj cov ntaub ntawv thiab qhib qhov hais kom sai lossis davhlau ya nyob twg los sau cov haujlwm
Mus rau daim nplaub tshev uas HaloDunia.java tau txais kev cawmdim thiab ntaus hauv
javac HelloDunia.java
. Cov lus txib no qhia Java compiler uas koj xav sau HaloDunia.java. Yog tias muaj qhov ua yuam kev tshwm sim, tus sau ntawv yuav qhia koj tias ua tsis raug. Txwv tsis pub, koj yuav tsis pom ib qho lus los ntawm tus sau. Yog tias koj saib cov npe uas koj tam sim no muaj HaloDunia.java khaws cia, koj yuav pom HaloDunia.class. Nov yog cov ntaub ntawv uas Java yuav siv los khiav koj txoj haujlwm.
Kauj Ruam 6. Khiav qhov program
Thaum kawg, peb yuav ua peb txoj haujlwm! Hauv qhov hais kom ua sai lossis davhlau ya nyob twg, ntaus
java HelloWorld
. Cov lus txib no qhia Java tias koj xav khiav HaloWorld chav kawm. Koj yuav pom "Nyob Zoo Ntiaj Teb." tshwm ntawm lub console.
Kauj Ruam 7. Nrog koj zoo siab, koj thawj Java program tau npaj lawm
Txoj Kev 3 ntawm 3: Kev Tawm Tswv Yim thiab Kev Tso Tawm
Kauj Ruam 1. Tam sim no peb yuav nthuav dav Hello World program kom suav nrog cov neeg siv tswv yim
Hauv Qhov Kev Pabcuam Nyob Hauv Ntiaj Teb, peb tso ib txoj hlua rau tus neeg siv pom, tab sis kev sib tham hauv ib feem ntawm qhov haujlwm yog thaum tus neeg siv yuav tsum nkag mus rau hauv cov haujlwm. Tam sim no peb yuav txuas ntxiv qhov haujlwm thov kom tus neeg siv nkag mus rau lawv lub npe thiab tom qab ntawd tos txais tus neeg siv los ntawm lub npe ntawd.
Kauj ruam 2. Ntshuam chav kawm Scanner
Hauv Java, peb muaj qee yam ntawm cov tsev qiv ntawv uas peb tuaj yeem nkag tau, tab sis peb yuav tsum tau import nws ua ntej. Ib ntawm cov tsev qiv ntawv no yog java.util, uas muaj lub Scanner kwv uas peb xav tau kom tau txais kev nkag los ntawm tus neeg siv. Txhawm rau import cov chav kawm Scanner, ntxiv kab hauv qab no thaum pib ntawm txoj cai.
ntshuam java.util. Scanner;
- Txoj cai no qhia rau txoj haujlwm uas peb xav siv Scanner yam khoom uas nyob hauv pob java.util.
-
Yog tias peb xav nkag mus rau txhua qhov khoom hauv java.util pob, tsuas yog sau
ntshuam java.util.*;
- thaum pib ntawm txoj cai.
Kauj Ruam 3. Sab hauv txoj hauv kev tseem ceeb, hloov qhov xwm txheej tshiab ntawm Scanner yam khoom
Java yog hom lus-hom lus programming. Yog li, hom lus no piav qhia lub tswv yim ntawm kev siv khoom. Lub Hom Phiaj Scanner yog piv txwv ntawm cov khoom uas muaj cov haujlwm thiab cov hau kev. Txhawm rau siv chav kawm Scanner, peb yuav tsum tsim lub Scanner tshiab yam khoom uas nws cov teb uas peb tuaj yeem sau tau thiab cov txheej txheem uas peb tuaj yeem siv tau. Txhawm rau ua qhov no, sau:
Scanner userInputScanner = Scanner tshiab (System.in);
-
userInputScanner
- yog lub npe ntawm Scanner yam peb nyuam qhuav ua piv txwv. Nco ntsoov tias lub npe tau sau rau hauv rooj plaub thiab hauv qab; qhov no yog qhov hloov pauv npe lub rooj sib tham hauv Java.
-
Peb siv tus neeg teb xov tooj
tshiab
los tsim qhov khoom tshiab piv txwv. Yog li, hauv qhov piv txwv no, peb tsim qhov piv txwv tshiab ntawm Scanner kwv los ntawm kev sau ntawv
Tshiab Scanner (System.in)
- .
-
Lub hom phiaj Scanner suav nrog qhov ntsuas uas qhia tus yam ntxwv yuav tsum luam theej duab. Hauv qhov no, peb nkag mus
System.in ua
raws li tsis.
System.in ua
- qhia qhov program txhawm rau tshuaj xyuas cov tswv yim los ntawm kab ke, piv txwv li cov tswv yim uas tus neeg siv yuav ntaus rau hauv txoj haujlwm.
Kauj Ruam 4. Nug cov tswv yim los ntawm tus neeg siv
Peb yuav tsum thov nkag los ntawm tus neeg siv kom tus neeg siv paub thaum twg yuav ntaus ib yam dab tsi rau hauv lub console. Cov kauj ruam no tuaj yeem ua los ntawm
System.out.print
los yog
System.out.println
System.out.print ("Koj lub npe yog dab tsi?");
Kauj Ruam 5. Nug lub Scanner yam khoom kom nkag mus rau kab tom ntej uas tus neeg siv sau thiab khaws nws hauv qhov sib txawv
Lub tshuab luam ntawv ib txwm nkag mus rau cov ntaub ntawv uas muaj tus neeg siv ntaus. Cov kab hauv qab no yuav nug Scanner kom siv lub npe tus neeg siv ntaus hauv thiab khaws nws hauv qhov sib txawv:
Txoj hlua userInputName = userInputScanner.nextLine ();
-
Hauv Java, qhov kev pom zoo rau kev siv cov txheej txheem ntawm ib yam khoom yog
objectName.methodName (tsis)
. Hauv
userInputScanner.nextLine ()
peb hu lub Scanner kwv nrog lub npe peb nyuam qhuav muab rau nws ces peb hu nws txoj kev
tom ntejLine ()
- uas tsis suav nrog ib qho kev ntsuas.
-
Daim ntawv ceeb toom tias peb khaws kab tom ntej hauv lwm yam khoom: cov khoom siv hlua. Peb tau muab peb lub npe String object
userInputName
Kauj Ruam 6. Qhia kev tos txais rau tus neeg siv
Tam sim no uas peb tau khaws lub npe siv, peb tuaj yeem tso cov lus tos txais rau tus neeg siv. Nco nrog
System.out.println ("Nyob Zoo Ntiaj Teb.");
uas peb sau hauv chav kawm tseem ceeb? Txhua txoj cai peb nyuam qhuav sau yuav tsum yog siab dua kab ntawd. Tam sim no peb tuaj yeem hloov kho kab ntawd los ua:
System.out.println ("Nyob zoo" + userInputName + "!");
-
Txoj kev uas peb koom nrog "Nyob Zoo", lub npe siv, thiab "!" los ntawm kev sau ntawv
"Nyob zoo" + userInputName + "!"
- hu ua String concatenation.
- Ntawm no peb muaj peb txoj hlua: "Nyob Zoo", userInputName, thiab "!". Cov hlua hauv Java raug tsau, txhais tau tias lawv hloov tsis tau. Yog li, thaum peb sib txuas peb txoj hlua no, peb pib tsim txoj hlua tshiab uas muaj kev tos txais.
-
Tom qab ntawd peb coj txoj hlua tshiab no thiab dhau nws ua qhov ntsuas rau
System.out.println
- .
Kauj Ruam 7. Npaj txhua yam thiab txuag
Peb cov cai tam sim no yuav zoo li no:
ntshuam java.util. Scanner; pej xeem chav HelloWorld {pej xeem zoo li qub void lub ntsiab (Txoj hlua args) {Scanner userInputScanner = tshiab Scanner (System.in); System.out.print ("Koj lub npe yog dab tsi?"); Txoj hlua userInputName = userInputScanner.nextLine (); System.out.println ("Nyob zoo" + userInputName + "!"); }}
Kauj Ruam 8. Sau thiab khiav
Mus rau qhov hais kom ua tam sim lossis lub davhlau ya nyob twg thiab khiav cov lus txib ib yam li peb ua HaloDunia.java. Peb yuav tsum xub suav qhov program:
javac HelloDunia.java
. Tom qab ntawd peb tuaj yeem khiav nws:
java HelloWorld
Lub tswv yim
- Java yog hom lus-hom lus programming. Yog li nws yog lub tswv yim zoo los nyeem ntawm cov hauv paus ntawm hom phiaj-qhia cov lus kom paub ntau ntxiv.
-
Lub hom phiaj-taw qhia lub hom phiaj muaj ntau yam tshwj xeeb. Peb ntawm lawv yog:
- Encapsulation: muaj peev xwm txwv kev nkag mus rau qee yam khoom tivthaiv. Java muaj tus kheej, tiv thaiv, thiab hloov kho pej xeem rau cov haujlwm thiab cov hau kev.
- Polymorphism: yam khoom muaj peev xwm siv ntau tus kheej. Hauv Java, ib qho khoom tuaj yeem tso rau hauv lwm yam khoom siv txoj hauv kev ntawm lwm yam khoom ntawd.
- Txojsia: muaj peev xwm siv thaj chaw thiab cov hau kev los ntawm lwm cov chav kawm hauv tib qib raws li cov khoom tam sim no.