Basic points and Data Type in Java

Data Type

TypeExplanation
intA 32-bit (4-byte) integer value
shortA 16-bit (2-byte) integer value
longA 64-bit (8-byte) integer value
byteAn 8-bit (1-byte) integer value
floatA 32-bit (4-byte) floating-point value
doubleA 64-bit (8-byte) floating-point value
charA 16-bit character using the Unicode encoding scheme
booleanA true or false value

 

Implicit and explicit casting : Primitive Type Casting 

An implicit cast means you don’t have to write code for the cast

An implicit cast happens when you’re doing a widening conversion.

An explicit casts looks like this:

Impossible Conversions

  • Any primitive type to any reference type
  • The null value to any primitive type
  • Any primitive to boolean
  • A boolean to any primitive

 

The Remainder or Modulus Operator in Java

Java has one important arithmetical operator you may not be familiar with, %, also known as the modulus or remainder operator. The % operator returns the remainder of two numbers. For instance 10 % 3 is 1 because 10 divided by 3 leaves a remainder of 1. You can use % just as you might use any other more common operator like + or -.

Here’s the output:

Perhaps surprisingly the remainder operator can be used with floating point values as well. It’s surprising because you don’t normally think of real number division as producing remainders. However there are rare times when it’s useful to ask exactly how many times does 1.5 go into 5.5 and what’s left over? The answer is that 1.5 goes into 5.5 three times with one left over, and it’s that one which is the result of 5.5 % 1.5 in Java.

Leave a Reply

Your email address will not be published. Required fields are marked *