Tuesday, 12 December 2017

Java Data Types and Operators



Java is a strongly typed language, that means all the variables must first be declared before we can use it. Declaring a variable in java includes type and name with optional value assignment. If no value is assigned, variable holds the default value. For primitive types, there are different default values but it’s always NULL for Object data types.
Java Data Types – Primitives and Binary Literals

Java Primitive Data Types

Java programming language contains eight primitive data types. Four primitive data types are for integer values – byte, short, int and long. Two primitive data types are for floating type decimal values – float and double. One is for characters – char and one is for condition – boolean. Java programming language also comes with Wrapper classes for all these primitive data types.

Autoboxing in Java

Autoboxing in java was introduced in Java 1.5. Autoboxing and unboxing is a convenient way to auto transform primitive data type to it’s corresponding java wrapper classes and vice versa.

Autoboxing in Java

Converting a primitive data type into an object of the corresponding wrapper class is called autoboxing. For example, converting int to Integer or converting long to Long object.
Java compiler applies autoboxing when a primitive value is:

1.     Passed as a parameter to a method that expects an object of the corresponding wrapper class. For example a method with Integer argument can be called by passing int, java compiler will do the conversion of int to Integer.
2.     Assigned to a variable of the corresponding wrapper class. For example, assigning a Long object to long variable.

Unboxing in Java

Converting an object of a wrapper type to its corresponding primitive data type is called unboxing.
Java compiler applies unboxing when an object of a wrapper class is:
1.     Passed as a parameter to a method that expects a value of the corresponding primitive type.
2.     Assigned to a variable of the corresponding primitive type.

Java Autoboxing Example

Here is a small java program showing examples of autoboxing and unboxing in java.
Note: It’s not a good idea to rely on autoboxing always, sometimes it can cause compiler error that method is ambiguous when a method is overloaded.

Wrapper class in Java

Wrapper class in java are the Object representation of eight primitive types in java. All the wrapper classes in java are immutable and final. Java 5 autoboxing and unboxing allows easy conversion between primitive types and their corresponding wrapper classes in java programs.

Why do we need wrapper classes?

I think it was a smart decision to keep primitive types and Wrapper classes separate to keep things simple. We need wrapper classes when we need a type that will fit in the Object world programming like Collection classes. We use primitive types when we want things to be simple.
Primitive types can’t be null but wrapper classes can be null.
Wrapper classes can be used to achieve polymorphism.
Here is a simple program showing different aspects of wrapper classes in java.

Java Ternary Operator

Java ternary operator is the only conditional operator that takes three operands. Java ternary operator is a one liner replacement for if-then-else statement and used a lot in java programming. We can use ternary operator to replace switch also as shown in below example.

Java Ternary Operator


The first operand in java ternary operator should be a boolean or a statement with boolean result. If the first operand is true then java ternary operator returns second operand else it returns third operand.

The ternary operator is a form of syntactic sugar for if-then-else statements. It is also known as the conditional operator, which is perhaps a more meaningful name because it evaluates conditions like if does. Provided that the operator is used with care, it can make code more concise without sacrificing readability.

Ternary Operator

The ternary operator evaluates a condition and chooses one of two branches to execute. Like in many other programming languages, ? and : symbols are used to form it. Other common names you might encounter that refer to the same concept are inline if, ternary if and the conditional operator.
The name ternary refers to the fact that the operator takes three operands.
condition ? exprTrue : exprFalse
The condition is a boolean expression that evaluates to either true or false. Both, exprTrue and exprFalse are also expressions but they can evaluate to anything you want them . If the condition is true, the ternary operator evaluates exprTrue. Otherwise exprFalse is evaluated.
The ternary operator is an expression (like price + 20 for example), which means that once executed, it has a value. And that value needs to be further used, for example by assigning it to a variable or returning from a method, or the expression will not compile.
It’s worth mentioning that the operator is lazy in the sense that only the used expression is evaluated: The ternary operator will not evaluate the unused branch.






No comments:

Post a Comment