123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computer-Programming >> View Article

Java Data Types And Operators

By Author: siyaram ray
Total Articles: 339

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.

Total Views: 45Word Count: 728See All articles From Author

Computer Programming Articles

1. Machine Learning In Finance: Why, What & How
Author: Unknown Member

2. Automation Testing – Selenium Testing Tool Components
Author: Siyaram Ray

3. How To Pair Your Airpods With Ipad, Iphone, Apple Watch, Mac, And Apple Tv
Author: Joey Williams

4. How To Download & Install Ios 12.1 Beta 2 In Your Iphone
Author: Ethan Grey

5. How To Fix Err_connection_reset Error For Macos
Author: Joey Williams

6. Coherent Aid For Android Application Development
Author: Daniel Carl

7. How To Fix Windows Update Error 0x80240034
Author: Ethan Grey

8. Widening Selenium With Image Recognition
Author: Siyaram Ray

9. How To Fix Banking Error 9995 In Quickbooks Online
Author: kaira thompson

10. Key Features Of Successful Mobile Apps
Author: Chris Kambala

11. Selenium Webdriver Is Being Way Out - Selenium Versus Cypress
Author: Siyaram Ray

12. Web Design Company In Moradabad
Author: sachin mishra

13. Gst Software Company In Moradabad
Author: sachin mishra

14. Multi Level Marketing Software
Author: sachin mishra

15. Mlm Level Plan Software
Author: sachin mishra

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: