Article

Java Data Types and Operators

Topic: Coach Training and CertificationPublished December 13, 2017
No ratings yet766 viewsSign in to rate
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.rnJava Data Types – Primitives and Binary LiteralsrnJava Primitive Data TypesrnJava 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.rnAutoboxing in JavarnAutoboxing 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.rnAutoboxing in JavarnConverting 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.rnJava 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.rn2. Assigned to a variable of the corresponding wrapper class. For example, assigning a Long object to long variable.rnUnboxing in JavarnConverting an object of a wrapper type to its corresponding primitive data type is called unboxing.rnJava compiler applies unboxing when an object of a wrapper class is:rn1. Passed as a parameter to a method that expects a value of the corresponding primitive type.rn2. Assigned to a variable of the corresponding primitive type.rnJava Autoboxing ExamplernHere is a small java program showing examples of autoboxing and unboxing in java.rnNote: 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.rnWrapper class in JavarnWrapper 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.rnWhy do we need wrapper classes?rnI 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.rnPrimitive types can’t be null but wrapper classes can be null.rnWrapper classes can be used to achieve polymorphism.rnHere is a simple program showing different aspects of wrapper classes in java.rnJava Ternary OperatorrnJava 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.rnJava 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.rnThe 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.rnTernary OperatorrnThe 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.rnThe name ternary refers to the fact that the operator takes three operands.rncondition ? exprTrue : exprFalsernThe 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.rnThe 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.rnIt’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.

Further reading

Further Reading

4 total

Article

How Coaching Turns Knowledge Into Change Walk into any bookstore or scroll through your favorite podcast feed, and you’ll see the same thing: endless tips, strategies, and “life hacks” promising transformation.rnWe devour them. We highlight paragraphs. We even try to practice what we’ve learned. But somehow, the big shifts never stick. Weeks later, the old patterns creep back in.rnSo why does it happen? Why do so many smart, motivated people keep getting stuck — eve

October 6, 2025

Article

grinding and cutting a variety of materials across numerous industries. Understanding their construction, types and safety precautions is mandatory for anyone working with these powerful tools. This article will explore the basics of abrasive wheels, offering insights into their components, maintenance and legal requirements to ensure both effective and safe usage. Exploring the Basics of Abrasive Wheels Abrasive wheels are critical tools in various industrial applications, f

March 6, 2025

Article

The internet we use daily, known as the surface web, represents only a fraction of the entire digital landscape. Beneath this visible layer lies the deep web and the dark web, where anonymity, privacy, and unrestricted information exchange thrive. Among the most well-known directories for accessing dark web content is the Uncensored Hidden Wiki . This article delves into the history, significance, risks, and access methods of the Uncensored Hidden Wiki, providing a comprehens

January 31, 2025

Article

If you're searching for exceptional piano classes near you, there are several excellent options in Toronto to consider. For those eager to learn the piano, finding a school or instructor that offers expert guidance, comprehensive lesson plans, and a supportive environment is key. Catering to both beginners and advanced players, many schools in the Toronto area provide tailored lessons to help students achieve their musical aspirations. What to Look for in Piano Lessons When

January 1, 2025