Java Data Types Integer, floating point, char, string, Boolean
Java data types:
Java Data Types: Of course, it is not enough for us to display simple messages on the screen (We don’t need a programming language for this). We want to use Java can perform calculations to achieve certain effects on web pages Clocking, automating processes or problems in electronic commerce (Beginning with the purely visual effects on websites via the secure transmission of data over the Internet and the automatic generation of forms up to intelligent software agents for handling various Tasks) to solve, i.e. generally to implement algorithms and process data. For this reason, let us first understand how you can deal with simple data (numbers, letters, etc.) in Java. To we shall first look at the value ranges of the simple Java data types.
When defining a data type, the value range, i. H. the possible Values of this type and the basic operations permitted for these values are specified. In the following we want to give a first introduction to the simple Java data types of Java type. These are called simply because there is next to them as well there are more complicated Java data types that are displayed in a way that has yet to be determined assemble simple Java data types.
Java data types Integer:
How does a computer handle whole numbers? He saves them as a sequence of binary characters (i.e. 0 or 1) that represent integer powers of the number 2. The number 23 can roughly be represented by the sum
1 x 16 + 0 x 8 + 1 x 4 + 1 x 2 + 1 x 1 = 1 x 2^{4} + 0 x 2^{3} + 1 x 2^{2} + 1 x 2^{1} + 1 x 2^{0}
which can then be expressed in the memory of a computer by the binary sequence 10111 (i.e. with 5 digits) can be coded. Negative numbers let through code different methods, which we will not go into in detail here will. In any case, however, one needs another place for the sign. Let us assume that we have 1 byte – these are 8 bits, i.e. 8 binary digits – for the Available to represent a number. We need the first bit for the sign. The largest number that we represent with the remaining 7 digits is thus
1 x 64 + 1 x 32 + 1 x 16 + 1 x 8 + 1 x 4 + 1 x 2 + 1 x 1 = 127.
Type name  greatest value  smallest value  Long 
Byte  127  128  8 Bits 
Short  32767  32768  16 Bits 
int  2147483647  2147483648  32 Bits 
Long  9223372036854775807  9223372036854775808  64 Bits

Due to the computerinternal representation of negative numbers (the socalled Two’s complement representation), the smallest negative number is in terms of absolute value by 1 larger, i.e. H. in this case −128. So with 8 bits we can get 127 + 128 + 1 = 256 represent different numbers. If we had more jobs available, would our stock of numbers are also growing.
The byte data type represents exactly this representation in Java. A number from Type byte is 8 bits long and ranges from −128 to +127. In general this number range is much too small to be able to work with it sensibly. For this reason, Java has other kinds of integer Java data types, they are two, four, or eight bytes long. Table1 summarizes these Java data types. there are literal constants for the integer Java data types simply from a sequence of digits (0 to 9). For such constants, it is assumed by default that it is a 32 Bit number (i.e. an int constant). Let’s work with 64 bits instead (i.e. with a number in long format), we must add the ending to the number Append L. We want to illustrate this with an example. The commands
System.out.println
and
System.out.print
are also able to print simple Java data types such as integers. We are now trying to output the number 9223372036854775807 on the screen. For this we write the following program:
1 2 3 4 5 
public class Test { public static void main (String [] args) { System.out.println (9223372036854775807); } } 
If we call the compiler with the command javac Test.java, see above we get the following error message:
So in line 3 of the Test.java file, we used a number that leads to is great. Too large because it is accepted as an int value by default becomes, but according to the above Table1 it is significantly larger than 2147483647, and therefore not more can be represented with 32 bits. Such a number must be explicitly called We change the line as follows:
System.out.println (9223372036854775807L);
By adding the ending L, the number is treated as a long number and thus encoded with 64 bits (which, according to the table1, just fits into). Of The corresponding data type is called long in Java.
Attention: In addition to the purely decimal notation of integer values literal constants in Java can also be used as octal numbers (numbers in the eighth System) or as hexadecimal numbers (numbers in the sixteenth system) will. Octal numbers must and may begin with a leading 0 only contain digits in the range 0 to 7. Hexadecimal numbers must start with 0x begin and can contain digits in the range 0 to 9 and A to F. The three int constants 27, 033, and 0x1B are therefore alternative notations for the integer decimal value 27.
1 2 3 4 5 
public class Test { public static void main (String [] args) { System.out.println (9223372036854775807L); } } 
When I add the “L” at the end of 9223372036854775807 the compiler compile program without any error as you can see in the below figure
Floating point Java data types:
We want to do a simple calculation. The following program is supposed to do that Output the result of 1/10 using the division operator in Java:
1 2 3 4 5 
public class Test { public static void main (String [] args) { System.out.println (1/10); } } 
We translate the program and run it. Received to our surprise we get a supposedly wrong result namely the zero!
What is happen?
To understand what actually happened, we have to be clear about one thing: we worked with integer Java data types. The division operator is in However, Java defines it in such a way that the division of two whole numbers turns into an integer (namely the integer part of the quotient). We remember us to the primary school time – here 1/10 would also have resulted in 0 – with the remainder 1. We can determine this remainder in Java with the% symbol. We change our Program according to:
1 2 3 4 5 6 7 8 
public class Test { public static void main (String [] args) { System.out.print ("1/10 is "); System.out.print (1/10); // whole number part System.out.print (" with remainder"); System.out.print (1% 10); // rest } } 
Typname  largest positive value  smallest positive value  Long 
float  3.4028234663852886E + 038  1.4012984643248171E045  32 Bits 
double  1.7976931348623157E+308  4.9406564584124654E324  64 Bits 
The above program outputs are:
Now it is generally not undesirable to work with whole numbers only. Suppose we want some amount of money in another currency convert. Should the penny amounts then be dropped? Java provides for this Another reason is the possibility of using socalled floating point numbers floating point numbers). These are built up internally from 32 or 64 bits, however, the bit patterns are interpreted differently than the integer ones Data types. The floating point types float and double available in Java have the range of figures given in Table2.
Literal constants for the floating point Java data types can be taken from various optional Constructed components. In addition to a decimal point, there can be sequences of digits (before and after the decimal point) and an exponent (consisting of an e or an E, followed by a possibly signed one Digit sequence) and an ending (f, F, d or D) can be used. Such However, floating point constant must (to differentiate between integer constants) at least one decimal point or one exponent or one Ending exist. If a decimal point occurs, it must be preceded or followed by a Sequence of digits.
As already mentioned, the E followed by the number X stands for the multiplication with 10^{X}, i.e. H. the number 1.2E3 stands for 1.2 · 103, i.e. the value 1200, the number 1.2E3 for 1.2 · 10^{−3}, i.e. the value 0.0012. Be negative numbers by putting a minus sign in front of the corresponding number.
A floating point constant is always without or with the ending d or D of type double. If we use the ending f or F, it is of type float. Of course, with 32 bits or 64 bits, not every number between + 3.4028235E38 and 3.4028235E38 are shown exactly. The computer works again with powers of 2, so even numbers as simple as 0.1 cannot be coded exactly in the computer. It is therefore rounded internally carried out, so that we can expect rounding errors in some cases have to. Exercise 4.8 will deal with this problem again. For At the moment we shouldn’t care, because we want our program now Switch to calculating with floating point numbers. For this we only replace the integer values by floating point numbers:
1 2 3 4 5 6 
public class Test { public static void main (String[] args) { System.out.print ("1/10 is "); System.out.print(1.0/10.0); } } 
If we run the above program, the result is:
Java data types char for characters:
Sometimes it turns out to be necessary, not with numerical values, but with single letters or characters to work. These characters are in Java defined by the data type char (abbreviation for character). Literal constants this data type, individual characters from the available character set shown in single quotes, the characters a and ? Had in Java the representation ’a’ and ’?’.
Data of the char type are represented internally with 16 bits (i.e. 2 bytes). Each Character corresponds internally to a certain number or number (this 16bit binary number), the socalled Unicode. For example, the letter a corresponds to the number 97. You can therefore also use char values as take integer values and convert them accordingly to integer values. For example, the instruction int i = ’a’ gives i the value 97 assigned.
The Unicode character set also contains characters that may be in our Editor cannot be displayed. To work with these signs anyway Java uses the Unicode notation (\ u followed by four hexadecimal Digits) with which all Unicode characters (\ u0000 to \ uffff) can represent. For example, the letter a corresponds to the value \ u0061 (the hexadecimal value 61 precisely corresponds to the decimal Value 97), i.e. H. the literal constant ’a’ can be written as ’\ u0061’. For the simplified representation of some “invisible” characters and symbols with a predefined meaning as a command or separator also exists the notation with socalled escape sequences. If you want, for example note a horizontal tab as a character constant, so you can instead of ’\ u0009’, write short ’\ t’. A line feed can be used as Note ’\ n’. The symbols ’and”, which are used to represent characters or String constants are required, can only be in the form ’\’ ’and ’\” ’Can be generated as a character constant. As a consequence for the special The meaning of the \ symbol must be the corresponding constant in the form ’\\’ be noted.
Java data types Strings
Several characters of the data type char can be combined to form a string be summarized. Such strings are not used in Java as values of a special simple data type, but as objects of a special one Class called String, We want to but not go into further here. The only important thing for us is those literal constants data type, a sequence of characters from the available Character set, shown in doublequotes. the string abcd would have the representation “abcd” in Java.
The boolean java data types for truth values
It will often be necessary to compare two values with one another. Is about the value 23 greater, less than or equal to another value? The given for this Comparison operators provide an answer that ultimately leads to yes or no, true or false, d. H. to one of the truth values true or false runs out. In order to be able to work with such truth values, exists in Java the data type boolean. This type has only two possible values:
true and false.
These are the only literal constants that can be noted as boolean values. The evaluation of logical expressions Checking (for example, comparisons) returns values of the type boolean, which can be linked with logical operators, for example later an important role in the flow control of our programs will play.
Implicit and explicit casts java data types
Sometimes it happens that we need a certain data type at one point, However, we have a different one. We want the addition, for example, 9223372036854775000L + 807 a 64bit number and a 32bit number. However, the plus operator is only defined for values of the same type. So what to do In our case, the answer is simple: nothing. The Java compiler recognizes that the left number has a range of numbers that encompasses that of the right number. The So the system can easily convert the 807 to a long number (which it does). This transformation, which happens unnoticed by us in the background, will be referred to as implicit typecast. Implicit type conversions always occur when a smaller range of numbers is mapped into a larger one. from byte to short, from short after int, and from int to long. Integer Java data types can also be implicit convert to floating point types, although there may be rounding errors may occur. In addition, of course, a float value can be automatically added double. Implicit conversion from char to int, long, float, or double is also possible. But sometimes it also happens that we turn a larger one into a smaller one Have to convert number range. For example, we have the result of a Floating point calculation (let’s say 3.14) and we are only interested in the part before the comma. So we want to turn a double number into an int value. Such a type of conversion may lose information – the Compilers won’t do this easily. He rather gives us when translating an error message of the form
Incompatible type for declaration.
Explicit cast needed to convert double to int.
out. The reason for this is that such conversions are often due to programming errors are based, i.e. are not actually intended at all. Of the Compiler assumes and reports an error. So we have to make it clear to the translator that we know exactly what we are to do! This procedure is called explicit type conversion (eng. Explicit typecast) is designated and performed by specifying the intended target data type write in round brackets in front of the corresponding number. In our example above would be about
(int) 3.14
mean. The compiler recognizes that the conversion is really wanted, and cuts off the decimal places. The result of the conversion is 3. A conversion from boolean to another elementary data type or the other way round is not possible – neither explicitly nor implicitly.
Warning: In addition to the situations described so far in this section, there are other program contexts in which the compiler does automatic type conversions can make. Particularly noteworthy is the fact that for assignments to variables of the type byte, short or char, the value on the right can also be converted automatically from the allocation symbol if it is a constant value of the type and this is in the value range of the variable. For example, we can by the instruction
shorts = 1234;
of the short variable s the integer value 1234 (i.e. actually a 32bit Assign a number of type int).
Summary
We got to know simple Java data types with which we can enter whole numbers, Represent floating point numbers, single characters, and truth values in Java can. We learned how these Java data types relate to each other and how to transform them into each other.
Related Article:
Java Identifiers with examples