Data Types

1. PDT (Primitive Data Type)

A type that contains the data itself

  • Primitive data types => Only 8!

  • The data is directly in the memory space

  • The . (dot) operator cannot be used => why? Because it's not an address!

1. Boolean type

  • boolean

    : 1bit

    (true/false) -> JAVA does not allow 0 or 1

2. Character type

  • char

    : 2byte

    ex) 'a' -> only a single character is possible

    => Uses single quotation('') -> stored as ASCII code value

+

byte == 8bit (1 byte) == -128 ~127

3. Integer type

  • Short

    : 16 bit (2 byte)

  • Int

: 32bit (4 byte) => The default for integer types is Int

  • Long

    : 64bit (8 byte)

4. Floating-point type

  • float

    : 32bit (4 byte)

  • Double

    : 64bit (8 byte) => The default for floating-point types is Double

-> Everything except the 8 types above is a Reference Type!!!!!

2. Reference Type

A type that contains an address

: Reference data type

  • The memory space contains an address, and you must follow the address to find the actual data

  • The . (dot) operator can be used!! ex) address.

  • In Reference types, you must use new for everything except String

  • String can be used like a primitive type or with new!!

    • New is placed in the Heap area

    • Using it like a primitive type places it in a different area than new => this consumes less data!

      ex) String address = new String("Bitcamp"); -> when using new

      ​ String address = "Bitcamp"; -> when used like a primitive type

String is an immutable object!

-> So to change String data, you need to use StringBuilder or StringBuffer

Buffer

= A temporary memory space

null value

= A value that explicitly indicates the absence of an address in reference types

ex) now = null;

Java tries to match types based on the operator!

ex 1)

-> It matches the type to a floating-point type (1 becomes 1.0)

​ => Since there is no data loss when converting from integer to floating-point, type promotion occurs automatically

ex 2)

-> Since the result is a string, the type is declared as String

​ => The type changes depending on what the result value is!

char type & int type

char type automatically undergoes promotion to int type!

=> but, to convert int to char, type casting must be done

-> why? Because there is data loss!

​ : char 2 byte < int 4 byte

To go from a larger size to a smaller size, type casting is required because there is data loss!

(Summary)

Larger size -> Smaller size => Data loss exists => Type Casting

Smaller size -> Larger size => No data loss => Promotion

(Type Promotion in JAVA)

img

Last updated