Professional Documents
Culture Documents
Chapter Two
String اٌخٍط اٌشِضي
• A string is collection of characters grouped together. For example, "hello" is a
string consisting of the characters 'h', 'e', 'l', 'l', and 'o'.
'h', صِْٛ ِٓ اٌشٛ" خٍط سِضي ِخىhello" ص ِجخّؼٗ ِؼب ػٍى عبًٍ اٌّثبيِٛػٗ ِٓ اٌشّٛػببسٖ ػٓ ِج
'o''وe', 'l', 'l',
• In most programming languages, strings are generally understood as a data
type, they are built in as part of the language and they take one of two basic
representations as illustrated in following figures:
ٌخُ بٕبؤ٘ب وجضء ِٓ اٌٍغت، ع بٍبٔبثٕٛط اٌشِضٌٗ بشىً ػبَ وٍُٛ اٌخٙ ٌخُ ف، فً ِؼظُ ٌغبث اٌبشِجت
:ضح فً األشىبي اٌخبٌٍتِٛ ٛ٘ احذًا ِٓ حّثٍٍٍٓ أعبعٍٍٓ وّبٚ ٌْٚأخزٚ
.$ ً بـ فبطً خبي خبصٙص اٌخً حٕخِٛفٗ ِٓ اٌشٛط اٌشِضٌٗ وّظفٌٍٛتم تمثٌل اٌخ. فً هذه,ًتمثٌل منته
فً هذا التمثٌل ٌتم تمثٌل الخٌوط الرمزٌه كـ (مؤشر إلى) مصفوفه من الرموز مع، الطول/ تمثٌل المؤشر
على سبٌل. الطول أكثر فعالٌة لبعض العملٌات/ تمثٌل المؤشر. عدد صحٌح ٌخزن طول الخٌوط الرمزٌه
حٌث إنه مخزوّ ن، ٌستغرق تحدٌد طول الخٌوط الرمزٌه وق ًتا ثاب ًتا، الطول/ فً تمثٌل المؤشر، المثال
.بالفعل
1
BY NsM [DATA STRUCTURES IN ARABIC]
• Java Strings can't change the characters, but string variables can point to
different strings:
ٌٗط سِضٌٍٛىٓ ٌّىٓ ٌّخغٍشاث اٌخٍط اٌشِضي أْ حشٍش إٌى خٚ ، صِٛ حغٍٍش اٌشJava Strings ال ٌّىٓ ٌـ
:ِخخٍفت
String s;
s = "java language";
s = "java is oop";
2
BY NsM [DATA STRUCTURES IN ARABIC]
returned. If string doesn't exist in the pool, a new string instance is created and
placed in the pool. For example:
إذا كان الخٌط. من تجمع الخٌط الثابت أوالJVM ٌتحقق،ًفً كل مرة تقوم فٌها بإنشاء خٌط رمزي حرف
، فً حالة عدم وجود خٌط فً التجمع. فً إشارة إلى مثٌل المجمع ٌتم إرجاعه، موجود بالفعل فً التجمع
: فمثال.ٌتم إنشاء مثٌل خٌط جدٌد ووضعه فً التجمع
.String s1="Welcome;"
String s2="Welcome";//will not create new instanceلن ٌخلق مثٌل جدٌد
• In the above example only one object will be created. Firstly JVM will not find
any string object with the value "Welcome" in string constant pool, so it will
create a new object. After that it will find the string with the value "Welcome" in
the pool, it will not create new object but will return the reference to the same
instance.
لٍّتٚ ػٍى أي وبئٓ خٍط سِضي رJVM ٌٓ ٌؼثش، ًالٚ أ.احذ فمطٚ ٓ عٍخُ إٔشبء وبئ، ٖ•فً اٌّثبي أػال
ف حجذ اٌخٍط راث اٌمٍّتٛ بؼذ رٌه ع. ٌزٌه عٍٕشئ وبئًٕب جذٌذًا، " فً حجّغ ثببج اٌخٍطWelcome"
.ٗب عخشجغ اٌّشجغ إٌى اٌّثًٍ ٔفغٌٕٙىٚ ٌٓ حٕشئ وبئًٕب جذٌذًاٚ ، " فً اٌخجّغWelcome"
• Note: String objects are stored in a special memory area known as string
constant pool.
ط فً ِٕطمت راوشة خبطت حؼشف ببعُ حجّغ اٌخٍط اٌثببجٍٛ ٌخُ حخضٌٓ وبئٕبث اٌخ:ِالحظت
Q: Why java uses concept of string literal ً لماذا تستخدم جافا مفهوم الخٌط الحرف:س
Ans.: To make Java more memory efficient (because no new objects are created
if it exists already in string constant pool).
دةٛجِٛ أوثش وفبءة فً اٌزاوشة (ألٔٗ ال ٌخُ إٔشبء وبئٕبث جذٌذة إرا وبٔجJava ً ٌجؼ:اإلجببت
..)طٍٛببٌفؼً فً حجّغ ثببج اٌخ
3
BY NsM [DATA STRUCTURES IN ARABIC]
Each time a new declaration a compiler would create different object in memory
فً وً ِشة إػالْ جذٌذ اٌّؼبٌج عٍخٍك وبئٓ ِخخٍف فً اٌزاوشة
Example: مثال
String str1 = new String("Hello");
String str2 = new String("Hello");
In this case compiler would create two different object in memory having the
same text.
ف ٌٕشئ اٌّؼبٌج وبئٍٕٓ ِخخٍفٍٓ فً اٌزاوشة ٔفظ إٌضٛفً ٘زٖ اٌحبٌت ع
4
BY NsM [DATA STRUCTURES IN ARABIC]
While primitive types are value types :بٌنما األنواع البدائٌة هً أنواع قٌم
int x = 5; int y = x;
String x = "a"
String y = x;
بالتوفيق