Wednesday 13 November 2013

Primitive datatypes

There are 8 primitive datatypes in Java:

  1. int
  2. double
  3. boolean
  4. char
  5. long
  6. short
  7. byte
  8. float

In this post I'll cover first 4 primitives. They are also more commonly used than the others. The actual usage will come later

1. int (Integer value)

These are the whole values, i.e. without any fractional part. Examples are: 1, 2, 3, -50, 900, 0, -340981, and so on. However, it's worth nothing that there are boundaries to these values. The maximum value an integer variable can hold is 2147483647 and the minimum is -2147483648. So the proper definition of an int in Java is a variable that can hold whole values in the range shown above.

2. double (fractional value)

Can hold both integers and fractions. Example: -5.0, 23.67, 123.90, 6.8, etc. Note that if you assign an integer value to a double variable, it'll automatically be changed to a fractional value with 0 in its decimal place (3.0, 4.0, 0.0).

3. boolean

A boolean variable can only hold one of the two values, true or false. That's it pretty much. Usually used to test conditions

4. char

You can think of char as a letter, single digit or some character. Examples: '1', 'a', 'b', '0', '-'. Char values can easily be identified by the single quotes around them.

There's no need to cover the rest 4 datatypes at this stage. If you're interested you can always read about them on the Oracle's website. Here's the link. Next post will contain some info on how to use the primitives we've covered.

No comments:

Post a Comment