انواع داده ها

انواع داده ها

انواع داده ها
انواع داده های اولیه
جاوا شامل هشت نوع داده اولیه است که در جدول زیر فهرست شده اند.
 
نوع داده
شرح
تعداد بایت ها
مثال
byte
عدد صحیح بین -127 و 128
1
byte b=33;
char
کاراکتر یونیکد؛ عدد صحیح بین 0 و 65535 (0 و 2 16 -1)
2
char c=’a’;
char c=97;
short
عدد صحیح بین -32768 و 32767 (-2 15 -1 و 2 15 -1)
2
short s=-1025;
int
عدد صحیح بین -2 31 و 2 31 -1
4
int i=15;
طولانی
عدد صحیح بین -2 63 و 2 63 -1
8
long l=15;
float
عدد ممیز شناور 32 بیتی
4
float f =4.67f;
double
عدد ممیز شناور 64 بیتی
8
double d=4.67;
boolean
بولی با مقادیر false یا true
N/A
boolean b=true;
سایر انواع داده ها مانند رشته ها کلاس هایی هستند که به آنها انواع داده های ترکیبی نیز گفته می شود.
در روش ها، می توانید از هر 5 نوع داده اولیه یا ترکیبی موجود در جاوا و کتابخانه های جاوا استفاده کنید. بسیاری از روش‌های داخلی Application Builder از انواع داده‌های اولیه یا ترکیبی استفاده می‌کنند. به عنوان مثال، متد ()timeStamp یک عدد صحیح طولانی را به عنوان خروجی ارائه می دهد.
تکالیف و لفظ
چند نمونه از استفاده از لفظ در تکالیف عبارتند از:
int i=5; // i را مقداردهی اولیه کنید و مقدار 5 را اختصاص دهید
دو برابر d=5.0; // d را مقداردهی اولیه کنید و مقدار 5.0 را اختصاص دهید
بولی b=true; // b را مقداردهی اولیه کنید و مقدار true را تعیین کنید
ثابت های 5 ، 5.0 و true حرفی هستند. جاوا بین حروف 5 و 5.0 تمایز قائل می شود که در آن 5 یک عدد صحیح و 5.0 یک دوتایی (یا شناور) است.
عملگرهای UNARY و BINARY در متدها (SYNTAX جاوا)
شما می توانید محاسبات و عملیات را با استفاده از انواع داده های اولیه درست مانند بسیاری از زبان های برنامه نویسی دیگر انجام دهید. جدول زیر برخی از رایج ترین عملگرهای یونری و باینری مورد استفاده در کد جاوا را توضیح می دهد.
 
سطح تقدم
نماد
شرح
1
++ —
unary: پسوند جمع و تفریق
2
++ — + – !
unary: جمع، تفریق، علامت مثبت، علامت منفی، منطقی نیست
3
* / %
باینری: ضرب، تقسیم، مدول
4
+ –
باینری: جمع، تفریق
5
!
منطقی نه
6
<= > >=
comparisons: less than, less than or equal, greater than, greater than or equal
7
== !=
comparisons: equal, not equal
8
&&
binary: logical AND
9
||
binary: logical OR
10
?:
conditional ternary
11
= += -= *= /= %= >>= <<= &= ^= |=
assignments
12
,
element separator in lists
TYPE CONVERSIONS AND TYPE CASTING
When programming in Java, conversion between data types is automatic in many cases. For example, the following lines convert from an integer to a double:
int i; // initialize i
double d; //initialize d
i=41;
d=i; // the integer i is assigned to the double d and d is 41.0
However, the opposite will not work automatically (you will get a compilation error). Instead you can use explicit type casting as follows:
int i; // initialize i
double d; //initialize d
d=41.0;
i=(int) d; // the double d is assigned to the integer i and i is 41
You can convert between integers and doubles within arithmetic statements in various ways, however you will need to keep track of when the automatic type conversions are made. For example:
int i; // initialize i
double d; //initialize d
i=41;
d=14/i; // d is 0
In the last line, 14 is seen as an integer literal and the automatic conversion to a double is happening after the integer division 14/41, which results in 0.
Compare with:
int i; // initialize i
double d; //initialize d
i=41;
d=14.0/i; // d is 0.3414…
In the last line, 14.0 is seen as a double literal and the automatic conversion to a double is happening before the division and is equivalent to 14.0/41.0.
You can take charge over the type conversions with explicit casting by using the syntax (int) or (double):
int i; // initialize i
double d,e; //initialize d and e
i=41;
d=((int) 14.0)/i; // d is 0
e=14/((double) i); // e is 0.3414…
STRINGS AND JAVA OBJECTS
The String data type is a Java object. This is an example of how to declare a string variable:
String a=”string A”;
When declaring a string variable, the first letter of the data type is capitalized. This is a convention for composite data types (or object-oriented classes).
After you have declared a string variable, a number of methods are automatically made available that can operate on the string in various ways. Two such methods are concat and equals as described below, but there are many more methods available in the String class. See the online Java documentation for more information.
Concatenating Strings
To concatenate strings, you can use the method concat as follows:
String a = “string A”;
String b = ” and string B”;
a.concat(b);
The resulting string a is “string A and string B”. From an object-oriented perspective, the variable a is an instance of an object of the class String. The method concat is defined in the String class and available using the a.concat() syntax.
Alternatively, you can use the + operator as follows:
a = a + b;
which is equivalent to:
a = “string A” + ” and string B”;
and equivalent to:
a = “string A” + ” ” + “and string B”;
where the middle string is a string with a single whitespace character.
Comparing Strings
Comparing string values in Java is done with the equals method and not with the == operator. This is due to the fact that the == operator compares whether the strings are the same when viewed as class objects and does not consider their values. The code below demonstrates string comparisons:
boolean streq = false;
String a = “string A”;
String b = “string B”;
streq = a.equals(b);
// In this case streq == false
streq = (a == b);
// In this case streq == false
b = “string A”;
streq = a.equals(b);
// In this case streq == true
Special Characters
If you would like to store, for example, a double quotation mark or a new line character in a string you need to use special character syntax preceded by a backslash (\). The table below summarizes some of the most important special characters.
 
Special Character
Description
\’
Single quotation mark
\”
Double quotation mark
\\
Backslash
\t
Tab
\b
Backspace
\r
Carriage return
\f
Form feed
\n
Newline
Note that in Windows the new line character is the composite \r\n whereas in Linux and macOS \n is used.
The example below shows how to create a string in Windows that you later on intend to write to file and that consists of several lines.
String contents = “# Created by me\r\n”
  +”# Version 1.0 of this file format \r\n”
  +”# Body follows\r\n”
  +”0 1 \r\n”
  +”2 3\r\n”
  +”4 5\r\n”;
The string is here broken up into several lines in the code for readability. However, the above is equivalent to the following:
String contents = “# Created by me\r\n# Version 1.0 of this file format \r\n# Body follows\r\n0 1 \r\n2 3\r\n4 5\r\n”;
which is clearly less readable.
ARRAYS
In the application tree, the Declarations node directly supports 1D and 2D arrays of type string (String), integer (int), Boolean (boolean), or double (double). A 1D array may be referred to as a vector and a 2D array referred to as a matrix, provided that the array is rectangular. A nonrectangular array is called jagged or ragged. In methods, you can define higher-dimensional arrays as well as arrays of data types other than string, integer, Boolean, or double.
1D Arrays
If you choose not to use the Declarations node to declare an array, then you can use the following syntax in a method:
double dv[] = new double[12];
This declares a double array of length 12.
The previous line is equivalent to the following two lines:
double dv[];
dv = new double[12];
When a double vector has been declared in this way, the value of each element in the array will be zero.
To access elements in an array you use the following syntax:
double e;
e = dv[3]; // e is 0.0
Arrays are indexed starting from 0. This means that dv[0] is the first element of the array in the examples above, and dv[11] is the last element.
You can simultaneously declare and initialize the values of an array by using curly braces:
double dv[] = {4.1, 3.2, 2.93, 1.3, 1.52};
In a similar way you can create an array of strings as follows:
String sv[] = {“Alice”, “Bob”, “Charles”, “David”, “Emma”};
2D Arrays
2D rectangular arrays can be declared as follows:
double dm[][] = new double[2][3];
This corresponds to a matrix of doubles with 2 rows and 3 columns. The row index comes first.
You can simultaneously declare and initialize a 2D array as follows:
double dm[][] = {{1.32, 2.11, 3.43},{4.14, 5.16, 6.12}};
where the value of, for example, dm[1][0] is 4.14. This array is a matrix since it is rectangular (it has same number of columns for each row). You can declare a ragged array as follows:
double dm[][] = {{1.32, 2.11}, {4.14, 5.16, 6.12, 3.43}};
where the value of, for example, dm[1][3] is 3.43.
Copying Arrays
For copying arrays, the following code:
for (int i1 = 0; i1 <= 11; i1++) {
  for (int i2 = 0; i2 <= 2; i2++) {
    input_array[i1][i2] = init_input_array[i1][i2];
  }
}
is not equivalent to the line:
input_array = init_input_array;
since the last line will only copy by reference.
Instead, you can use the copy method as follows:
input_table = copy(init_input_table);
which allocates a new array and then copies the values.