در روش ها، می توانید از هر 5 نوع داده اولیه یا ترکیبی موجود در جاوا و کتابخانه های جاوا استفاده کنید. بسیاری از روشهای داخلی Application Builder از انواع دادههای اولیه یا ترکیبی استفاده میکنند. به عنوان مثال، متد ()timeStamp یک عدد صحیح طولانی را به عنوان خروجی ارائه می دهد.
ثابت های 5 ، 5.0 و true حرفی هستند. جاوا بین حروف 5 و 5.0 تمایز قائل می شود که در آن 5 یک عدد صحیح و 5.0 یک دوتایی (یا شناور) است.
شما می توانید محاسبات و عملیات را با استفاده از انواع داده های اولیه درست مانند بسیاری از زبان های برنامه نویسی دیگر انجام دهید. جدول زیر برخی از رایج ترین عملگرهای یونری و باینری مورد استفاده در کد جاوا را توضیح می دهد.
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:
However, the opposite will not work automatically (you will get a compilation error). Instead you can use explicit type casting as follows:
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:
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.
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):
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.
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.
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:
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.
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.
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”;
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.
If you choose not to use the Declarations node to declare an array, then you can use the following syntax in a method:
When a double vector has been declared in this way, the value of each element in the array will be zero.
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.