The Java programming language offers a blend of traditional programming features and innovative concepts. The primary characteristics and syntax of Java follows the features of C and C++ to a certain extent. This is because before the invention of Java, C and C++ were popular programming languages that programmers around the world were familiar with.
Features of Java
Let’s now examine some important features of the Java language in detail and then find out how Java differs from its ancestors C and C++.
Primitive Data Types
Java is a strongly-typed language because every variable in a Java program has a type. Primitive data types are simple types of data that store values. Java does not implement primitive data types as objects. Instead, it follows the style of traditional programming languages like C and C++ to ensure high performance.
Note: Java’s primitive data types have the same characteristics as the primitive data types in C and C++ with just a few exceptions.
There are eight primitive data types in Java that are classified into three groups, namely, Numeric, Character, and Boolean data types.
Numeric Data Types
The Numeric Data Types group is further classified into two groups, namely, Integer numeric types and Real numeric types.
Integer Numeric Types:
Include the 8–bit byte, 16-bit short, 32-bit int, and 64-bit long types.
Support signed, positive, and negative integers.
Do not support unsigned integers.
Real Numeric Types:
Include the 32-bit float and 64-bit double types.
Support floating-point numbers and arithmetic operations on these numbers as defined by the IEEE 754 specification.
Consider double precision values as double type even if they are stored in a float variable. To assign double precision values to a float variable, you need to explicitly cast the variable to float.
Character Data Type
Includes the 16-bit Unicode character type, char. The Unicode character type defines characters by using unsigned 16-bit values ranging from 0 to 65,535.
Assigns a Unicode value to a variable of type char.
Provides global compatibility for character data.
Boolean Data Type
Uses the boolean type to store a boolean value which is either true or false.
Does not support conversion of a boolean variable to a numeric type like the C programming language.
Java uses the same set of operators available in C and C++. These operators include:
+, – , *, /, and %: Perform the basic arithmetic operations. Also, the + and – operators can be used as unary operators to denote positive and negative numbers.
+=, -=, *=, /=, and %=: Combine an arithmetic operation with an assignment operation. These operators are called compound assignment operators.
++ and – – : Increment or decrement a number by one.
Bitwise Operators: Perform operations on the individual bits in an integer. Some of the bitwise operators are &, +, ~, ^, >>, >>>, and <<.
Note: The >>> operator is a new operator in Java that performs an unsigned right shift.
Relational Operators: Compare one operand with another. Some of the relational operators are ==, !=, >, <, >=, and <=.
Boolean Logical Operators: Perform operations on boolean operands.
Java implements arrays as objects. Some of the key features of arrays in Java are:
Arrays can be declared for any data type.
Multi-dimensional arrays can be created by declaring arrays of arrays.
At the time of declaration, only a reference handle is allocated to an array.
Example: int myNum;
The required number of references is allocated when you specify the amount of storage needed for an array.
Example: myNum = new int;
Array elements can be accessed through their indexes. Array indexing starts at 0.
Every access to an array index is checked to make sure that the index is within the array’s range.
In Java, strings are objects that can be instantiated with a set of characters enclosed within double quotes. Some of the key features of strings are:
Read-only strings are created using the String class and strings that need modifications are created using the StringBuffer class.
Strings are not implemented as arrays in Java.
Strings can be concatenated using the + operator.
The length() method can be used to obtain the number of characters in a string.
The memory management and garbage collection features of Java take care of memory allocation, memory usage tracking, and release of memory. These features eliminate the need to explicitly manage memory in Java programs.
The key features of Java’s memory management are:
There are no pointers and pointer arithmetic operations in Java.
All objects have references that act as handles to the objects. The memory manager keeps track of object references.
Objects are allocated memory using the new operator. Java does not support C-style malloc and free functions to manage memory explicitly.
When an object does not have any references to it, the automatic garbage collection mechanism of the Java run-time system reclaims the memory used by the object.
The Java run-time system makes use of idle times in a user interactive session to run the garbage collector on a low priority thread.
Integrated Thread Synchronization
The Java run-time system supports multi-threading. Developers can access multi-threading features at the syntax level and through thread objects.
Java’s approach to multi-threading provides a powerful way for developers to create thread-safe multi threaded classes.
Features Not in Java
Java excludes many of the features available in C and C++ to eliminate redundancy and facilitate simple code that is easy to understand. Some of the features that are not available in Java are:
#define, preprocessor, typedef, and header files
Structures and unions
Functions and procedures
Though Java does not support goto statements, you can include multi-level breaks by using labels within a loop or switch statement. You can use the break statement to break out of a block or use the continue statement to continue execution from a specific point.