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

Java Data Types And Operators

By Author: siyaram ray
Total Articles: 281

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: 34Word Count: 728See All articles From Author

Computer Programming Articles

1. Explain The New Features In Java 7
Author: Individual

2. Why Should You Learn Selenium Ide For Test Automation
Author: Siyaram Ray

3. What Is Builder Design Pattern In Java?
Author: Individual

4. Advantages To Having A Warehouse Management System
Author: brilliant info

5. Web Development Patterns Of 2018 And 2019
Author: Siyaram Ray

6. 5 Factors Why Java Professionals Should Know Hadoop.
Author: Individual

7. Brief Introduction Of Selenium Automation Framework
Author: Siyaram Ray

8. Seo Tip And Techniques 2018
Author: Ammy

9. Advantages Of Learning Web Development The Right Way
Author: Siyaram Ray

10. The Computer Repair Service Waukesha Is Your One Stop For All Hardware And Software Issues
Author: adronseton

11. All About The Phases Of Software Development Procedure
Author: Henry Smith

12. Java Vs Python: Which Programming Language To Master?
Author: Individual

13. 9 Myths Regarding Seo That You Simply Ought To Stop Basic Cognitive Process Nowadays
Author: emly

14. Selenium Testing – Common And Difficult Interview Questions
Author: Siyaram Ray

15. Concrete Scanning
Author: concrete scanning

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