أفضل أمثلة Java

ما هي الجافا؟

Java هي لغة برمجة تم تطويرها بواسطة Sun Microsystems في عام 1995 ، والتي حصلت عليها شركة Oracle لاحقًا. إنها الآن منصة كاملة بها الكثير من واجهات برمجة التطبيقات القياسية وواجهات برمجة التطبيقات مفتوحة المصدر والأدوات ومجتمع مطور ضخم.

يتم استخدامه لبناء حلول المؤسسات الأكثر موثوقية من قبل الشركات الكبيرة والصغيرة على حد سواء. يتم تطوير تطبيقات Android بالكامل باستخدام Java ونظامها البيئي.

لمعرفة المزيد حول أساسيات Java ، اقرأ هذا وهذا.

الإصدار

أحدث إصدار هو Java 11 ، والذي تم إصداره في 2018 مع تحسينات مختلفة على الإصدار السابق ، Java 10. ولكن لجميع الأغراض والأغراض ، سنستخدم Java 8 في هذا الويكي لجميع البرامج التعليمية.

تنقسم Java أيضًا إلى عدة "إصدارات":

  • SE - الإصدار القياسي - لتطبيقات سطح المكتب والخوادم المستقلة
  • EE - Enterprise Edition - لتطوير وتنفيذ مكونات Java التي يتم تشغيلها مضمنة في خادم Java
  • ME - Micro Edition - لتطوير وتنفيذ تطبيقات Java على الهواتف المحمولة والأجهزة المدمجة

التثبيت: JDK أو JRE؟

قم بتنزيل أحدث ثنائيات Java من الموقع الرسمي. هنا قد تواجه سؤالاً ، أي واحد يتم تنزيله ، JDK أم JRE؟ JRE تعني Java Runtime Environment ، وهي عبارة عن Java Virtual Machine تعتمد على النظام الأساسي لتشغيل أكواد Java ، و JDK تعني Java Development Kit ، والتي تتكون من معظم أدوات التطوير ، وأهمها المترجم javac، وأيضًا JRE. لذلك ، بالنسبة للمستخدم العادي ، سيكون JRE كافيًا ، ولكن نظرًا لأننا سنطور باستخدام Java ، فسنقوم بتنزيل JDK.

تعليمات التثبيت الخاصة بالمنصة

شبابيك

  • قم بتنزيل ملف msi. ذي الصلة (x86 / i586 لـ 32 بت ، x64 لـ 64 بت)
  • قم بتشغيل ملف msi. إنه ملف قابل للتنفيذ يتم استخراجه ذاتيًا والذي سيقوم بتثبيت Java في نظامك!

لينكس

  • قم بتنزيل ملف tar.gz المناسب لنظامك وقم بتثبيته:

bash $ tar zxvf jdk-8uversion-linux-x64.tar.gz

  • تقوم منصات Linux القائمة على RPM بتنزيل ملف .rpm ذي الصلة وتثبيته:

bash $ rpm -ivh jdk-8uversion-linux-x64.rpm

  • لدى المستخدمين خيار تثبيت إصدار مفتوح المصدر من Java أو OpenJDK أو Oracle JDK. بينما يكون OpenJDK قيد التطوير النشط ومتزامنًا مع Oracle JDK ، إلا أنهما يختلفان فقط في عناصر الترخيص. ومع ذلك ، يشكو القليل من المطورين من استقرار Open JDK.

تعليمات Ubuntu :

افتح تثبيت JDK:

bash sudo apt-get install openjdk-8-jdk

تثبيت Oracle JDK:

bash sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java8-installer

ماك

  • إما أن تقوم بتنزيل Mac OSX .dmg القابل للتنفيذ من Oracle Downloads
  • أو استخدم Homebrew للتثبيت:
brew tap caskroom/cask brew install brew-cask brew cask install java

تحقق من التثبيت

تحقق من تثبيت Java بشكل صحيح في نظامك عن طريق فتح Command Prompt (Windows) / Windows Powershell / Terminal (Mac OS و * Unix) والتحقق من إصدارات Java runtime والمجمع:

$ java -version java version "1.8.0_66" Java(TM) SE Runtime Environment (build 1.8.0_66-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode) $ javac -version javac 1.8.0_66

نصيحة : إذا كنت تحصل على خطأ مثل "القيادة غير موجود" إماjavaأوjavacأو كليهما، والذعر لا، لم يتم تعيين لمجرد PATH النظام بشكل صحيح. بالنسبة لنظام التشغيل Windows ، راجع إجابة StackOverflow أو هذه المقالة حول كيفية القيام بذلك. هناك أيضًا أدلة لـ Ubuntu و Mac أيضًا. إذا كنت لا تزال غير قادر على اكتشاف ذلك ، فلا تقلق ، فقط اسألنا في غرفة Gitter لدينا!

JVM

حسنًا ، نظرًا لأننا انتهينا من عمليات التثبيت ، فلنبدأ أولاً في فهم التفاصيل الدقيقة لنظام Java البيئي. Java هي لغة مفسرة ومترجمة ، وهذا هو الكود الذي نكتبه يتم تجميعه إلى رمز بايت وتفسيره للتشغيل. نكتب الكود في ملفات .java ، وتقوم Java بتجميعها في أكواد ثنائية يتم تشغيلها على Java Virtual Machine أو JVM للتنفيذ. عادةً ما يكون لهذه الرموز البايتية ملحق فئة.

تعد Java لغة آمنة جدًا لأنها لا تسمح بتشغيل برنامجك مباشرة على الجهاز. بدلاً من ذلك ، يتم تشغيل برنامجك على جهاز افتراضي يسمى JVM. يعرض هذا الجهاز الظاهري العديد من واجهات برمجة التطبيقات لتفاعلات الآلة منخفضة المستوى التي يمكنك إجراؤها ، ولكن بخلاف ذلك لا يمكنك اللعب بتعليمات الجهاز بشكل واضح. هذا يضيف مكافأة ضخمة من الأمن.

أيضًا ، بمجرد تجميع الرمز الثانوي الخاص بك ، يمكن تشغيله على أي Java VM. يعتمد هذا الجهاز الظاهري على الجهاز ، أي أن له تطبيقات مختلفة لأنظمة Windows و Linux و Mac. لكن برنامجك مضمون للتشغيل في أي نظام بفضل هذا الجهاز الظاهري. هذه الفلسفة تسمى "اكتب مرة واحدة ، اركض في أي مكان".

مرحبا بالعالم!

لنكتب نموذجًا لتطبيق Hello World. افتح أي محرر / IDE من اختيارك وأنشئ ملفًا HelloWorld.java.

public class HelloWorld { public static void main(String[] args) { // Prints "Hello, World" to the terminal window. System.out.println("Hello, World"); } }

ملاحظة: ضع في اعتبارك أن اسم ملف Java يجب أن يكون هو نفس الاسم الدقيق للفئة العامة حتى يتم تجميعه!

افتح الآن Terminal / Command Prompt. قم بتغيير الدليل الحالي في Terminal / Command Prompt إلى الدليل حيث يوجد ملفك. وقم بتجميع الملف:

$ javac HelloWorld.java

الآن قم بتشغيل الملف باستخدام javaالأمر!

$ java HelloWorld Hello, World

Congrats! Your first Java program has run successfully. Here we are just printing a string passing it to the API System.out.println. We will cover all the concepts in the code, but you are welcome to take a closer look! If you have any doubt or need additional help, feel free to contact us anytime in our Gitter Chatroom!

Documentation

Java is heavily documented, as it supports huge amounts of API’s. If you are using any major IDE such as Eclipse or IntelliJ IDEA, you would find the Java Documentation included within.

Also, here is a list of free IDEs for Java coding:

  • NetBeans
  • Eclipse
  • IntelliJ IDEA
  • Android Studio
  • BlueJ
  • jEdit
  • Oracle JDeveloper

Basic Operations

Java supports the following operations on variables:

  • Arithmetic : Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%),Increment (++),Decrement (--).
  • String concatenation: + can be used for String concatenation, but subtraction - on a String is not a valid operation.
  • Relational: Equal to (==), Not Equal to (!=), Greater than (>), Less than (<), Greater than or equal to (>=), Less than or equal to (<=)
  • Bitwise: Bitwise And (&), Bitwise Or (|), Bitwise XOR (^), Bitwise Compliment (~), Left shift (<<), Right Shift (>>), Zero fill right shift (>>>)
  • Logical: Logical And (&&), Logical Or (||), Logical Not (!)
  • Assignment: =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=
  • Others: Conditional/Ternary(?:), instanceof

While most of the operations are self-explanatory, the Conditional (Ternary) Operator works as follows:

expression that results in boolean output ? return this value if true : return this value if false;

Example: True Condition:

 int x = 10; int y = (x == 10) ? 5 : 9; // y will equal 5 since the expression x == 10 evaluates to true 

False Condition:

 int x = 25; int y = (x == 10) ? 5 : 9; // y will equal 9 since the expression x == 10 evaluates to false

The instance of operator is used for type checking. It can be used to test if an object is an instance of a class, a subclass or an interface. General format- *object instance of class/subclass/interface*

Here is a program to illustrate instanceof operator:

 Person obj1 = new Person(); Person obj2 = new Boy(); // As obj is of type person, it is not an // instance of Boy or interface System.out.println("obj1 instanceof Person: " + (obj1 instanceof Person)); /*it returns true since obj1 is an instance of person */ 

Variable Examples

Variables store values. They are the most basic entity used to store data such as text, numbers, etc. in a program.

In Java, variables are strongly typed, which means you have to define the type for each variable whenever you declare it. Otherwise, the compiler will throw an error at compile time. Therefore, each variable has an associated ’data-type’ of one of the following:

Primitive Type: int, short, char, long, boolean, byte, float, double

Wrapper Type: Integer, Short, Char, Long, Boolean, Byte, Float, Double

Reference Type: String, StringBuilder, Calendar, ArrayList, etc.

You may have noticed that the Wrapper Type consists of types spelled exactly like the Primitive Type, except for the capitalised alphabet in the begining (like the Reference Type). This is because the Wrapper Types are actually a part of the more general Reference Types, but closely linked with their primitive counterparts via autoboxing and unboxing. For now, you just need to know that such a ‘Wrapper Type’ exists.

Typically, you can declare (i.e., create) variables as per the following syntax: ;

// Primitive Data Type int i; // Reference Data Type Float myFloat;

You can assign a value to the variable either simultaneously when you are declaring it (which is called initialisation), or anywhere in the code after you have declared it. The symbol = is used for the same.

// Initialise the variable of Primitive Data Type 'int' to store the value 10 int i = 10; double amount = 10.0; boolean isOpen = false; char c = 'a'; // Note the single quotes //Variables can also be declared in one statement, and assigned values later. int j; j = 10; // initiates an Float object with value 1.0 // variable myFloat now points to the object Float myFloat = new Float(1.0); //Bytes are one of types in Java and can be //represented with this code int byteValue = 0B101; byte anotherByte = (byte)0b00100001;

As evident from the above example, variables of Primitive type behave slightly differently from variables of Reference (& Wrapper) type - while Primitive variables store the actual value, Reference variables refer to an ‘object’ containing the actual value. You can find out more in the sections linked below.

Array

An Array is a collection of values (or objects) of similar datatypes (primitive and reference both form of datatypes are allowed) held in sequencial memory addresses. An Array is used to store a collection of similar data types. Arrays always start with the index of 0 and are instantiated to a set number of indexes. All the variables in the array must be of the same type, declared at instantiation.

Syntax:

dataType[] arrayName; // preferred way

Here, java datatype[] describes that all the variables stated after it will be instantiated as arrays of the specified datatype. So, if we want to instantiate more arrays of the similar datatype, we just have to add them after the specified java arrayName(Don’t forget to separate them through commas only). An example is given below in the next section for reference.

dataType arrayName[]; // works but not preferred way

Here, java datatype describes only that the variables stated after it belong to that datatype. Besides, java [] after the variable name describes that the variable is an array of the specified datatype (not just a value or object of that datatype). So, if we want to instantiate more arrays of the similar datatype, we will add the variables names just after the one already specified, separated by commas and each time we will have to add java [] after the variable name otherwise the variable will be instantiated as an ordinary value-storing variable (not an array). For better understanding an example is given in the next section.

Code snippets of above syntax:

double[] list1, list2; // preferred way

Above code snippet instantiates 2 arrays of double type names list1 and list2.

double list1[], list2; // works but not preferred way

Above code snippet an array of datatype double named list1 and a simple variable of datatype double named list2 (Don’t be confused with the name list2. Variables names have nothing to do with the type of variable).

Note: The style double list[] is not preferred as it comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers. Additionally it’s more readable: you can read that it’s a “double array named list” other than “a double called list that is an array”

Creating Arrays:

dataType[] arrayName = new dataType[arraySize];

Code snippets of the above syntax:

double[] List = new double[10];

Another way to create an Array:

dataType[] arrayName = {value_0, value_1, ..., value_k};

Code snippets of above syntax:

double[] list = {1, 2, 3, 4}; The code above is equivalent to: double[] list = new double[4]; *IMPORTANT NOTE: Please note the difference between the types of brackets that are used to represent arrays in two different ways.

Accessing Arrays:

arrayName[index]; // gives you the value at the specified index

Code snippets of above syntax:

System.out.println(list[1]);

Output:

2.0

Modifying Arrays:

arrayName[index] = value; 

Note: You cannot change the size or type of an array after initialising it. Note: You can however reset the array like so

arrayName = new dataType[] {value1, value2, value3};

Size of Arrays:

It’s possible to find the number of elements in an array using the “length attribute”. It should be noticed here that java length is an attribute of every array i.e. a variable name storing the length of the variable. It must not be confused for a method of array since the name is same as the java length() method corresponding to String classes.

int[] a = {4, 5, 6, 7, 8}; // declare array System.out.println(a.length); //prints 5

Code snippets of above syntax:

list[1] = 3; // now, if you access the array like above, it will output 3 rather than 2

Example of code:

int[] a = {4, 5, 6, 7, 8}; // declare array for (int i = 0; i < a.length; i++){ // loop goes through each index System.out.println(a[i]); // prints the array }

Output:

 4 5 6 7 8

Multi-dimensional Arrays

Two-dimensional arrays (2D arrays) can be thought of as a table with rows and columns. Though this representation is only a way to visualize the array for better problem-solving. The values are actually stored in sequential memory addresses only.

int M = 5; int N = 5; double[][] a = new double [M][N]; //M = rows N = columns for(int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { //Do something here at index } }

This loop will execute M ^ N times and will build this:

0 | 1 | 2 | 3 | 4

0 | 1 | 2 | 3 | 4

[ 0 | 1 | 2 | 3 | 4 ]

Similarly a 3D array can also be made. It can be visualised as a cuboid instead of a rectangle(as above), divided into smaller cubes with each cube storing some value. It can be initialised follows:

int a=2, b=3, c=4; int[][][] a=new int[a][b][c];

In a similar manner, one can an array of as many dimensions as he/she wishes to but visualizing an array of more than 3 dimensions is difficult to visualize in a particular way.

Jagged Arrays

Jagged arrays are multi-dimensional arrays that have a set number of rows but a varying number of columns. Jagged arrays are used to conserve memory use of the array. Here is a code example:

int[][] array = new int[5][]; //initialize a 2D array with 5 rows array[0] = new int[1]; //creates 1 column for first row array[1] = new int[2]; //creates 2 columns for second row array[2] = new int[5]; //creates 5 columns for third row array[3] = new int[5]; //creates 5 columns for fourth row array[4] = new int[5]; //creates 5 columns for fifth row

Output:

0

0 | 1 | 2 | 3 | 4

[ 0 | 1 | 2 | 3 | 4 ]

Control Flow

Control flow statements are exactly what the term means. They are statements that alter execution flow based on decisions, loops and branching so that the program can conditionally execute blocks of code.

Primarily, Java has the following constructs for flow control:

  • if
  • if...else
if(  ){ //code enters this block if the above expression is 'true' }
if(  ){ //execute this block if the expression is 'true' } else{ //execute this block if the expression is 'false' }

switch

Switch is an alternative for the if...else construct when there are multiple values and cases to check against.

switch(  ){ case :  break; case :  break; default:  }

Note: The program flow falls through the next case if the break statement is missing. e.g. Let’s say you say the standard ‘Hello’ to everyone at office, but you are extra nice to the girl who sits next to you and sound grumpy to your boss. The way to represent would be something like:

switch(person){ case 'boss': soundGrumpy(); break; case 'neighbour': soundExtraNice(); break; case 'colleague': soundNormal(); break; default: soundNormal(); }
Note: The `default` case runs when none of the `case` matches. Remember that when a case has no `break` statement, it `falls through` to the next case and will continue to the subsequent `cases` till a `break` is encountered. Because of this, make sure that each case has a `break` statement. The `default` case does not require a `break` statement. 
  • nested statements

Any of the previous control flows can be nested. Which means you can have nested if,if..elseandswitch..casestatements. i.e., you can have any combination of these statements within the other and there is no limitation to the depth ofnesting.

For example, let’s consider the following scenario:

  • If you have less than 25 bucks, you get yourself a cup of coffee.
  • If you have more than 25 bucks but less than 60 bucks, you get yourself a decent meal.
  • If you have more than 60 bucks but less than a 100, you get yourself a decent meal along with a glass of wine.
  • However, when you have more than a 100 bucks, depending on who you are with, you either go for a candle lit dinner (with your wife) or you go to a sports bar (with your friends).

One of the ways to represent this will be:

int cash = 150; String company = "friends"; if( cash < 25 ){ getCoffee(); } else if( cash < 60 ){ getDecentMeal(); } else if( cash < 100 ){ getDecentMeal(); getGlassOfWine(); } else { switch(company){ case "wife": candleLitDinner(); break; case "friends": meetFriendsAtSportsBar(); break; default: getDecentMeal(); } }

In this example, meetFriendsAtSportsBar() will be executed.

Loops

Whenever you need to execute a block of code multiple times, a loop will often come in handy.

Java has 4 types of loops:

While Loop

The while loop repeatedly executes the block of statements until the condition specified within the parentheses evaluates to false. For instance:

while (some_condition_is_true) { // do something }

Each ‘iteration’ (of executing the block of statements) is preceeded by the evaluation of the condition specified within the parentheses - The statements are executed only if the condition evaluates to true. If it evaluates to false, the execution of the program resumes from the the statement just after the whileblock.

Note: For the while loop to start executing, you’d require the condition to be true initially. However, to exit the loop, you must do something within the block of statements to eventually reach an iteration when the condition evaluates to false (as done below). Otherwise the loop will execute forever. (In practice, it will run until the JVM runs out of memory.)

Example

في المثال التالي ، expressionيتم إعطاء الحرف بواسطة iter_While < 10. نحن زيادة iter_Whileبواسطة 1كل مرة يتم تنفيذ الحلقة. و whileفواصل حلقة عندما iter_Whileيصل قيمة 10.

int iter_While = 0; while (iter_While < 10) { System.out.print(iter_While + " "); // Increment the counter // Iterated 10 times, iter_While 0,1,2...9 iter_While++; } System.out.println("iter_While Value: " + iter_While);

انتاج:

1 2 3 4 5 6 7 8 9 iter_While Value: 10