Primitive Data Types

68 1 0
                                    

Primitive data types are predefined types that are used when declaring a variable or literals.

8 Types
================================
1. byte
    - it has range from -128 to 127
    - default value = 0

2. short
    - it has a range from -32768 to 32767
    - default value = 0

3. int
    - it has a range of -2e31 to 2e31 - 1
    - this is a whole number.
    - default value = 0
    - usually, we use base 10 numbering system. This is our normal counting. Binary, octal and hexadecimal is also allowed in case you might need it.

4. long
    - it has a range of -2e63 to 2e63 - 1
    - default value = 0L

5. float
    - it has higher range than int and you use this in decimal values but you cannot use this in currency.
    - default value = 0.0f
    - you can use e for exponents

6. double
    - it has higher range than float and can use with decimal values but you cannot use this in currency.
    - for currency, you need to use the java.math.BigDecimal
    - default value = 0.0

7. boolean
    - it has two possible values which is true or false
    - default  value = false

8. char
    - 16 bit unicorn character
    - it has a range from '\u0000' or 0 to '\uffff' or 65535
    - default value = '\u0000'
    - you can use this \u or sometimes  called the unicode escape
    - you need to use single quotation for the char value apart from String which is double quotation.
    - char letter = 'z'

java.lang.String
    - this is an array of characters but does not belong to the 8 primitive data  types.
    - default  value = null

Java.math.BigDecimal
    - this is used for precise calculation of numbers.

Literals
    - this is the opposite of variables
    - the value is fixed.

Special escape sequences for character and strings
==============================
\b - backspace
\t - tab

- line feed
\f - form feed
\r - carriage return
\" - double quote
\' - single quote
\\ - backslash

You can use underscore to separate numbers for readability.

Example:
==≈====≈======================

public static void main(String[] args){
    System.out.println("H

ELLO");
}

===================≈===========
Result:
H
ELLO

Java ProgrammingWhere stories live. Discover now