java for abap programmers 3
TRANSCRIPT
-
7/28/2019 Java for ABAP Programmers 3
1/5
Alistair C. Rooney 2002 All rights reserved
JavaTM for ABAP programmers Lesson 3
The Primitive Data types in Java.
Everything in Java is an Object. Youll hear this refrain often enough on any Java course.
The exception to this is, of course, the primitive data types. Ill go through them quickly
in this lesson, since you, as an ABAP programmer, will already have a very goodunderstanding of data types.
Why do we have the data types we do in Java? Well here is an answer from JamesGosling (Java Architect) in response to that and other questions:
When James started out writing the Java language he likened it to moving apartment. He
put everything (from C/C++, the oldapartment) into boxes and then moved to the newapartment. He didnt unpack anything. He left everything in a box until he needed it and
then he pulled it out. After a year, everything that was left in the boxes was thrown out.Its not a completely accurate analogy but it is very close.
Boolean.
Named after George Boole, (1815-1864) the English mathematician who devisedBoolean algebra.
V.I.P. (Very Important Point): The Boolean declaration starts with a small b as do allour primitive data types. Using a capital B refers to the wrapper class and not to the
data type (More on this later).
The boolean can only have a value of true (not True) or false. It does not have a value of1 or 0 and cannot be treated as such.
Therefore you would test it like this:
boolean a=false;
.
.ifa = = true
{some code in here.
}
Notice something else here. Because of the structure of Java syntax, you are not obligedto leave a space between the operator and the operand. In ABAP you must leave a space.
Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.
-
7/28/2019 Java for ABAP Programmers 3
2/5
Alistair C. Rooney 2002 All rights reserved
The double equals tests a condition. The single equals (as in ABAP) acts as an
assignment operator.
The default for boolean is always false.
Byte.
In Java a byte is a signed, twos-complement quantity. For those of you who need a
lesson on what on earth a twos complement value is, please send an email and I willhappily send you an explanation. Essentially its an efficient way of storing negative
numbers.
A byte is 8 bits long and therefore can hold any value between 128 and 127 (28).
Beware of doing arithmetic using bytes. For instance this code:
byte a=10, b=20;
byte c=a+b;
will generate an error.Java will always perform mathematical calculations at 32 bits
precision.
You would need to cast the 32 bit answer into a byte to store the answer in c. We do this
by placing the data type in brackets, like so:
byte c = (byte) (a+b);
DANGER Will Robinson!
There is, of course, always a potential for data loss when casting from bigger to smaller
data types. The Most Significant Bits will be dropped.
Readers who have never seen an episode of the old 60s classic Lost in Space will not
get my reference at the top. Its OK. Ask your nearest old geek.
Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.
-
7/28/2019 Java for ABAP Programmers 3
3/5
Alistair C. Rooney 2002 All rights reserved
Integer.
An integer as you all know, is a whole number. It is declared in Java by using the intkeyword.
An integer is also signed and twos complement and is 32 bits wide, allowing it to holdanything from 231 to (231 -1).
Nothing more to see here! Move along.
Long.
As the name suggests the Long gives you more space to hold you big integers. It behaves
like an integer in every way, except for two things. It can take a number between 263 and(263 1). Secondly the declaration of a long value requires an l or an L after the number.
Convention dictates that you use the capital L to avoid confusion with the number one.
For example:
long longVar = 12345L;
The default values for integers and longs is zero.
Short.
There are instances when memory is at a premium. For instance, writing Java code foryour COMPAQ IPAQTM. (Remember Java is essentially write once run anywhere).
In these situations you can use the short, which shares all the attributes of an integer but
only takes up 16 bits. So from this we can deduce its number range to be from 215
to (215 1).
The Short does not require a suffix.
Float.
The first primitive data type that can hold decimals is the float. As the name suggests it
uses a floating point precision and can accommodate up to 32 bits. This gives it a number
range of between 3.4E38 to 3.4E38 and run about 5 to 6 digits of accuracy.
Floats must have an f or an F as a suffix. For example:
float f = 12.3456f;
Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.
-
7/28/2019 Java for ABAP Programmers 3
4/5
Alistair C. Rooney 2002 All rights reserved
Double.
This is similar to the float in many ways. The most important distinction is that it isdouble (the pun is intentional) the size. That means that the double will hold a stunning
64 bits. This means we can go from 1.7E308 to 1.7E308. You may append a D or a
d to the end of the number. I say may because its optional. Leaving it out will makethe assumption of a double.
Although currency calculations would rarely use the full amount of a double, it iscommon practice to use doubles for currency calculations.
Large scientific or engineering calculations make good use of the double data type.
Char.
The char is the only unsignedprimitive data type (apart from Boolean!).
HEADS UP! In C a char is a mere 8 bits wide (255 characters). In Java it is 16 bits wide
allowing for a much greater range of characters. It conforms to Unicode and not to ASCII(Dont panic, the Unicode and ASCII character sets are identical for the first 255 chars).
This enables us to use Japanese, Chinese, Sanskrit and a bunch of truly multilingual
character sets. (visit http://charts.unicode.org/).
The char in Java is defined within single quotes. For example:
chara = A;
You can also embed escape sequences within a character definition. The following table
shows a summary of those.
\n Linefeed \r Carriage return
\f Form feed \\ Backslash
\t Tab \ Double quote
\b Backspace \ Single quote
Hey and thats the lot folks. Lets take a while (get a coffee) to summarise what wevelearnt about primitive data types.
Ive put these into a little table again for your convenience:
Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.
http://charts.unicode.org/http://charts.unicode.org/http://charts.unicode.org/ -
7/28/2019 Java for ABAP Programmers 3
5/5
Alistair C. Rooney 2002 All rights reserved
boolean true or false
byte 8 bits
short 16 bits
int 32 bits
long 64 bits
float 32 bitsdouble 64 bits
char 16 bits (unsigned)
Next Lesson well cover some commenting standards in Java and have a quick look at the
JavaDoc utility, which is provided free with the SDK.
Well also have a look at naming conventions and naming standards.
Java for Abapers is soon to be available in book form!
Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.