Introduction to Java

August 20, 2021 | Hollin Ariyawansa

Java යනු 1995 වර්ශයේ නිර්මාණය වුනු, අදටත් ඉතාමත් ප්‍රසිද්ධ Programming Language එකක්. Consumer devices සිට Heterogeneous Enterprise Systems දක්වා පුළුල් පරාසයක Applications Develop කිරීම සඳහා Java තාක්‍ෂණය භාවිතා කෙරෙනවා. Java කියන්නෙ මොකක්ද කියලා specifically කිව්වොත්, ඒක high-level, class based, object-oriented programming language එකක්. අපි කලින් ලිපියකත් කතා කරපු විදියට High-level language එකක් කියන්නෙ human languages වලට සමීපයි කියන එකට. Class-based, object-oriented programming (OOP) කියන්නෙ ටිකක් complex topic එකක් නිසා, මං ඒ ගැන මේ ලිපියෙ කතා කරන්න බලාපොරොත්තු වෙන්නෙ නෑ.

Java කියන්නෙ general-purpose programming language එකක් වගේම එකේ විශේෂත්වයක් වෙන්නෙ write once, run anywhere (WORA) feature එක. මේ feature එකෙන් compile කරපු Java code එකක් නැවත recompilation එකක් නැතුව ඕනම Java supported platform එකක run කරන්න පුලුවන්. Java applications සාමාන්‍යන් compile වෙන්නෙ bytecode එකකට, මේ bytecode එක PC එකේ computer architecture එකේ බලපෑමකින් තොරව ඕනම Java Virtual Machine (JRM) එකක run වෙනවා.

Why Use Java?

  • Platform රැසක යොදා ගන්න පුලුවන් වීම (Java works on different platforms) (Windows, Mac, Linux, Raspberry Pi, etc.)

  • එය ලොව ජනප්‍රියතම programming language එකක් ( It is one of the most popular programming language in the world)

  • එය ඉගෙන ගැනීමට පහසුව සහ භාවිතා කිරීමට සරල බව.

  • එය open-source සහ free වීම.

  • විශාල community support එකක් තිබීම. (මිලියන 10කට වැඩි developer community එකක්)

  • Java යනු object-oriented language වන අතර එමඟින් program වලට clear structure එකක් ලබා දෙන අතර code නැවත භාවිතා කිරීමට ඉඩ සලසමින්, development cost අඩු කරයි.

Java is used for

  • Mobile applications (විශේෂයෙන් Android apps)

  • Desktop applications

  • Web applications

  • Web servers සහ application servers

  • Games

  • Database connection

The Java compiler

Java platform එක සඳහා program කිරීමේදි, source code එක .java file වල ලියා ඒවා compile කරනවා. ඊට පස්සෙ compiler එක language එකෙ syntax rule’s අනුව අපේ code එක check කරලා, පසුව bytecode එක .class file විදියට හැදෙනවා. Bytecode එකක් කියන්නෙ Java virtual machine (JVM) මත ක්‍රියාත්මක කිරීම target කරපු instructions set එකක්.

The JVM

Runtime එකේදි, JVM එක .class files interpret කරලා JVM එක ලියලා තියෙන native hardware platform එකෙන් program instructions execute කරනවා. Java වල “write-once, run-anywhere” principle එකේ heart එක JVM එක කියන්න පුලුවන්. මේ නිසා ඔයාගෙ Java code එක, JVM implementation එක කරන්න පුලුවන් ඕනම chipset එකක run කරන්න පුලුවන්. Linux™ and Windows® වගෙ major platform වගේම, mobile phones සහ hobbyist chips වලත් JVM implement කරන්න හැකියාව තියෙනවා.

The Java Development Kit

Java Development Kit (JDK) එකක් download කරද්දි, compiler සහ අනෙකුත් tools වලට අමතරව common application-development tasks වලට අවශ්‍ය complete class library of prebuilt utilities ලැබෙනවා.

The Java Runtime Environment

The Java Runtime Environment (JRE; also known as the Java runtime) වලට JVM, code libraries සහ Java language එකෙන් ලියා ඇති වැඩසටහන් ක්‍රියාත්මක කිරීම සඳහා අවශ්‍ය components ඇතුළත් වෙනවා. Multiple platforms සඳහා JRE ලබා ගත හැකියි.

Setting up your Java development environment


Java Install

සමහර විට, ඔබගේ PC එකෙ Java install වෙලා තියෙන්න පුලුවන්. ඒක බලන්නෙ මෙහෙමයි.

ඔබගේ windows PC එකෙ command prompt එකෙ මෙලෙස java -version යනුවෙන් type කරන්න.


Java installed නම් මේ වගෙ message එකක් display වෙයි. (version එක අනුව message එක වෙනස් වෙන්න පුලුවන්)

ඔයාගෙ PC එකෙ Java install වෙලා නැත්තම්, ඔයාට JDK එක oracle website එකෙන් free download කරන්න පුලුවන්.

Setup for Windows

  1. Download කර ගත් setup file එක install කිරිමෙන් පසුව "System Properties" වෙත පිවිසෙන්න (Control Panel > System and Security > System > Advanced System Settings)

2. "Advanced" tab එකේ "Environment variables" button එක click කරන්න.

3. System variables හි "Path" variable එක select කර "Edit" button එක click කරන්න.

4. "New" button එක click කර Java install කල path එක \bin ලෙස අවසන් වන විදියට add කරන්න.

"OK" click කර, settings save කරන්න.


5. අවසානයට Command Prompt (cmd.exe) open කර java -version ලෙස type කරන්න.

සාර්ථකව install වෙලා නම්, මෙහෙම message එකක් display වෙවි.

Setup for Linux

Oracle වෙබ්සයිට් එකෙන් ඔබගේ Operating System එකට අදාල Java JDK file එක download කරගන්න. ඊළගට download වු zip file එක unzip කරගන්න. System variable සඳහා Path එක ඇතුළත් කිරීමට පහත command එක terminal එකේ type කරන්න

sudo nano /etc/profile

එයින් open වන file එකේ පහත පරිදි type කරන්න.

ඊළඟට පහත command එක terminal එකේ type කරන්න. එම​ඟින් environment එක reload වනු ඇත.

./etc/profile

Operating System එකට JDK/JRE හි location එක ලබාදීමට පහත commands run කරන්න. <Directory where JAVA has been extracted> වෙනුවට unzip කල folder path එක ලබාදෙන්න.

sudo update-alternatives --install "/usr/bin/java" "java" "<Directory where JAVA has been extracted>/bin/java" 1

sudo update-alternatives --install "/usr/bin/javac" "javac" "<Directory where JAVA has been extracted>/bin/javac" 1

sudo update-alternatives --install "/usr/bin/javaws" "javaws" "<Directory where JAVA has been extracted>/bin/javaws" 1


sudo update-alternatives --set java <Directory where JAVA has been extracted>/bin/java

sudo update-alternatives --set javac <Directory where JAVA has been extracted>/bin/javac

sudo update-alternatives --set javaws <Directory where JAVA has been extracted>/bin/javaws

ඉන්පසු ඔබේ පරිගනකය restart කරන්න.

java -version

Terminal එකේ ඉහත command එක run කිරීමෙන් java install වී ඇතිදැයි නිර්ණය කරගන්න.

සටහන - හොලින් ආරියවංශ