মনে করি আমরা one নামে project name দিয়ে একটি জাভা প্রোজেক্ট তৈরি করলাম এবং first_program নামে একটি ক্লাস তৈরি করলাম।
তাহলে আমরা public class first_program {
}
প্রতিটি প্রোগ্রামে একটি মেন মেথড (main method) থাকে , উল্লেখ্য জাভা ভাষায় ফাংশানকে মেথড বলা হয়। c/c++ এ ফাংশন সম্পর্কে আমরা জেনেছি।
উপড়ে আমরা যে ক্লাস তৈরি করলাম তাতে কোন Main method নেই। তাই তাতে মেন মেথড যুক্ত করতে হবে ।জাভা ল্যাংগুয়েজে public static void main(String arg[]){ }
হল ডিফল্ট মেন মেথড ।
There is only one form of the main method in Java, shown as follows:
public static void main(String[] args) {
// Body of method
}
The following table shows elements of the main method:
Elements Meaning
Public—— The method is visible outside the class.
Static———— The method can be invoked without creating an object of the class type.
void ———–The method does not return anything.
Arg[]——————— An array of strings representing the arguments passed.
টিঊটোরিয়াল-৩
এখন আমরা জানব জাভা প্রোগ্রামে কিভাবে কোন কিছু প্রিন্ট/print করা যায়।
সাধারনত c তে প্রিন্ট করার জন্য printf() এবং c++এ cout<< ব্যাবহার করা হয় ।
জাভা প্রোগ্রামিং এ কোন কিছু print করার জন্য
System.out.print(); ব্যাবহার করা হয়। তবে জাভা ভাষায় একটি সুবিধা হল System.out.println(); ব্যাবহার করে লাইন ব্রেক করা যায় ,অর্থাৎ নিঊ লাইন তৈরি হয় । এখন আমরা একটি প্রোগ্রাম দেখব যার মাধ্যমে বিষয়টা ভালোভাবে বোঝা যাবে
public class first_program {
public static void main(String arg[]){
System.out.print(“This is my first java program! “);
System.out.print(“This is my first java program! “);
}
}
এখন উক্ত প্রোগ্রাম রান করলে আমরা
This is my first java program! This is my first java program!
আউটপুট পাব ।দেখতে পেলামএকটি আউটপুটের পরেই আরেকটি আউটপুট প্রিন্ট করেছে তাই একটি লাইনে দুটি আউটপুট দেখাচ্ছে । এখন যদি আমরা চাই একটির পরে আরেকটি অর্থাৎ দ্বিতীয়টি পরের লাইনে দেখা যাবে তাহলে আমরা কি করতে পারি । কিছু না , শুধু System.out.println() ; ব্যাবহার করলেই উক্ত ফলাফল পাব।
public class first_program {
public static void main(String arg[]){
System.out.println(“This is my first java program! “);
System.out.println(“This is my first java program! “);
}
}
তাহলে আমরা আউটপুট পাব
–
This is my first java program!
This is my first java program!
তবে আউটপুটের জন্য নিম্ন লিখিত বিষয় গুলো জানা দরকার
Escape sequence Description
\n Newline. Position the screen cursor at the beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
\r Carriage return. Position the screen cursor at the beginning of the current
line—do not advance to the next line. Any characters output after the car-
riage return overwrite the characters previously output on that line.
\\ Backslash. Used to print a backslash character.
\” Double quote. Used to print a double-quote character. For example,
System.out.println( “\”in quotes\”” );
displays “in quotes”.
টিঊটোরিয়াল-৪ঃ
এ পর্বে আমরা জানব কিভাবে জাভা প্রোগ্রামে কোন কিছু ইনপুট নিতে হয় ।
ইউজার হতে কোন কিছু ইনপুট নেওয়ার জন্য সচারাচর বর্তমানে Scanner use করা হয়ে থাকে ।
Scanner input =new Scanner(System.in);
এখানে input একটি ভেরিয়েবল , input না লিখে আমরা যে কোন কিছু দিতে পারি।
এখন আমরা এ বিষয়ক একটি প্রোগ্রাম দেখব যার মাধ্যমে কোন কিছু ইউজার হতে ইনপুট দিতে বলা হবে এবং তা প্রিন্ট করবে
import java.util.Scanner;
public class first_program {
public static void main(String arg[]){
Scanner input =new Scanner(System.in);
System.out.println(“Please write something “);
String line;
line=input.nextLine();
System.out.print(“Your write this —>”+line);
}
}
প্রোগ্রামটি রান কর Please write something
arif hasnat nstu
Your write this —>arif hasnat nstu
লেখা আসবে ,অর্থাৎ আমরা যে ইনপুট দেব তা প্রিন্ট করবে।
প্রোগ্রামটির ব্যাক্ষাঃ
আমরা জাভা প্রোগামে হেডার ফাইল ইম্পোর্ট করে থাকি । c/c++ এ include <Stdio.h>
করে থাকি কিন্তু জাভা ভাষায় হেডার ফাইল গুলো নির্দিষ্ট প্যাকেজের অন্তর্ভুক্ত থাকে যেগুলো আমরা ইমপোর্ট করে থাকি ।প্যাকেজ সম্পর্কে পরে আলোচনা করা হবে।
এখানে Scanner জাভা util package এর অন্তর্গত।
import java.util.Scanner;
first_program নামে ক্লাস
public class first_program {
মেন মেথড
public static void main(String arg[]){
input নামে একটি অব্জেক্ট তৈরি করলাম । যা পরে ব্যাবহার হবে।
Scanner input =new Scanner(System.in);
System.out.println(“Please write something “);
String type এর ভেরিয়েবল line create করলাম ।
String line;
এখনে line=input.nextLine(); তে বলা হল যে একটি স্ট্রিং টাইপের ইনপুট নেওয়ার জন্য ।
কয়েকপ্রকার ইনপুট নেওয়া যায় –যেমন
Input.nextInt(); নিলে ইন্টিজার টাইপের ইনপুট নিবে।
Input.nextFloat();নিলে Float টাইপের ইনপুট নিবে।
Input.nextDouble();নিলে Double টাইপের ইনপুট নিবে।
Input.nextChar();নিলে Char টাইপের ইনপুট নিবে।
line=input.nextLine();
line কে প্রিন্ট করে দিলাম ।
System.out.print(“Your write this —>”+line);
}
আশা করি বুঝতে পেরেছেন । কোন সমস্যা থাকলে অবশ্যি জানাবেন এই কামনা করি।