OCA Java 8 Preparation : Data Types
This is Blog Post – 2 in this series of OCA Java Refresher .Today, the topic is ‘Java Data Types’ and we’ll be covering all the points that are must for you to know when appearing for your OCA exam .Going through this will help you revise the must-know points very quickly and wait don’t get tricked by the simplicity of the topic name. We have some important points to cover. So, let’s get started.
The data types in Java can be classified broadly into primitive and reference data types.
Primitive Data Types :
- There are 8 primitive types available that you must know by heart – boolean, char, byte, short, int, long, float, double.When I say you need to know them by heart, I really mean so. Take a note that only char and int have a shortened name among all these available types so while appearing for the exam try to first identify if the correct data type names are used. It is easy to be fooled by the use of bool instead of boolean. Just this small trick can make you lose marks or atleast waste your precious time during the exam.
- If you find it difficult to remember all these data types, just think it this way – There are three broad categories available for Java Primitives – boolean , character and number(further classified as integral and floating point). This will also ease the understanding of operations that can be performed on these types which we’ll cover later.
- ‘char’ primitive type is an ‘unsigned numeric data type’. By unsigned , we mean it can only store positive integers. All the primitives of ‘number’ category(refer point 3) are signed i.e. they store both positive and negative numbers.
- Try watching for an assignment of a value to a char type with double quotes(“) as such a code will fail to compile. It is easy to fall for such traps.
- As stated in point 4, Java stores a char data as an unsigned integer value(positive values). So, it is valid to directly assign a positive integer value to a char.You can also assign a unicode character value to it.However, in case you try to assign a negative value to a char type, the code will fail to compile.
char c1 = 'a'; //valid
char c2 = '\u1222'; //valid, use of unicode
char c3 = 122; //valid
char c4 = -122; //invalid, fails to compile
- As far as assigning a negative value to char type is concerned, you can do it forcefully by casting it to char.
/*This code compiles fine,
* explicit casting will trim the value during storage so you can't
* predict the value at the time of printing it
char c5 = (char)-122;
- Exam can test your understanding of being able to identify the right data type for any given data set so be sure to practice that out. As far as,the sizes of these data types are concerned you can find them in the below table. The numbers that a ‘byte’ can store ranges from -128 to 127(inclusive) . The exam won’t test you more than that of ‘byte’ as far as the ranges are concerned.So, don’t worry you don’t need to memorize the ranges for all of these data types.
- Remember boolean type variable can hold either ‘true’ or ‘false’, which are predefined boolean literals. Try to catch out the use of 0 or 1 for boolean values in the exam as such a code will not compile.
- The floating point numbers can be of types – float(4 bytes) and double(8 bytes). The default literal type of floating point numbers is ‘double’. So in case you want it to be considered as float , use suffix ‘f’ or ‘F’ . The suffix for double is ‘d’ or ‘D’ but the usage of it is optional as any floating point number not specified as float is considered to be double by default.
- Similarly, the default literal type for integral values is ‘int‘. Until specified, compiler will assume the integral literal to be of type int. To use byte or short, typecasting needs to be done which we’ll cover later. For long, you can use the suffix ‘l’ or ‘L’.
Table : Java Primitive Data Types
|boolean||true or false||false||1||NA
|char||Unicode characters||\u0000||16||\u0000 - \uFFFF
|byte||Signed Integers||0||8||-128 to 127
|short||Signed Integers||0||16||-32768 to 32767
|int||Signed Integers||0||32||-2147483648 to 2147483647
|long||Signed Integers||0||64||-9.223372e+18 to 9.223372e+18
|float||Floating Point numbers(IEEE 754 )||0.0||32||Float.MIN_VALUE to
|double||Floating Point numbers(IEEE 754 )||0.0||64||Double.MIN_VALUE to
Number Systems :
- Integer literal values can come in the following flavours – binary(base – 2), decimal(base – 10), octal(base – 8), and hexadecimal(base – 16).A base-2 systems uses only two values 0 and 1. A base-8 systems uses digits in the range of 0-7(both inclusive). Similarly, base-10 system uses the numbers in range 0-9(both inclusive). The hexadecimal or base-16 number system uses digits 0-9 and letters A-F(a total of 16 digits & letters) where A stands for 10, B stands for 11 and so on.
- By default the base of integer literals in Java is base-10. To assign a variable with an integer literal in other number systems you need to use a specific prefix. For binary literals , use a prefix ‘0b’ or ‘0B’. Similarly, for octal literals use the prefix ‘0’. For hexadecimal literals, you are expected to use ‘0x’ or ‘0X’ as the prefix.
- Based on the exam objectives , you need not know the base conversions of numbers but it’s good to know them.You can learn more on this here. Exam mostly will test your understanding of being able to identify a right value of integer literal in different number systems.
Use of underscores in Number literals(Java7 Feature) :
Java 7 introduced the use of underscores(‘_’) as part of literal values. Such a grouping doesn’t impacts the meaning or value of the literals but just makes it a little more readable.
int value1 = 112_345;
long value2 = 0b1101_1011;
long value3 = 0x8937_89DF;
float value4 = 110.56_34F;
There are very important points that you must know as far as of use of underscores are considered for literal values.Make sure to get a good hold of these rules :
- Use of underscores are allowed for both – integral and floating-point values(both float and double). Also, it is valid to use them with literal value in any of the four number systems.
- You can’t start or end any literal value with underscore. For instance,
long value5 = 9892_; or
long value6 = _3793; is not valid.
- When using underscores with a value in octal number system, use of an underscore just after the prefix’0′ is valid.For eg:
long value7 = 0_4421; is allowed.
- You can’t use an underscore just after the prefixes 0b, 0B, 0x and 0X which are used to define literal values in binary and hexadecimal number systems.
long value8 = 0B_010101; //invalid
long value9 = 0x_fa1348; //invalid
long value10 = 0_4561; //Valid as it is in octal number system
- Use of an underscore just before the suffixes L, l, f, F, d, and D(used to mark a literal value as long, float and double respectively) is not allowed.
long value11 = 0B_010101_L; //invalid
float value12 = 357.04_f; //invalid
double value13 = 56_6.66_D; //invalid
- You can’t place an underscore adjacent to a decimal point.
double value14 = 424_.45; //invalid use of underscore
float value15 = 234._454; //invalid use of underscore
- You are not allowed to use an underscore in positions where a string of digits is expected.For instance,
int value16 = Integer.parseInt("35_451"); //invalid use of underscore
The above code is an invalid use of underscore as here a ‘string of literal’ is expected.. Here, for this case, the code will compile fine as compiler will treat it as a valid string value.However, there will be a runtime exception when you try executing the above code.
- The assignment
long value17 = 23___34; is valid.
Identifiers are names of packages, classes, interfaces, methods, and variables. You need to be able to identify a valid identifier, make a note of below set of rules.
- An identifier can be of unlimited length, theoretically.
- An identifier name must start with a letter(a-z or A-Z), a currency sign, or an underscore.
- An identifier name can use digits(0-9) but cannot start with it.
- An identifier can use an underscore at any position.
- It is invalid to use a Java keyword as an identifier.
- Use of special characters is not allowed.
- You can use a currency sign at any position in an identifier.
Reference Data Types :
Reference data types need no introduction as you can see them all over in Java.The default value for reference data type is null. Still in case you have any questions around that or you want me to discuss it in more detail, we can surely take it up.
Soon the next topic will be coming up in this series. Meanwhile, make sure you revise and practice these points fairly well.Stay tuned!
Be the First to comment.