Types in Java

There are mainly two categories of types in Java

  • primitive types and
  • reference types

So, we can have two kinds of variables holding values of these two types.

Primitive types

Primitive types are boolean and numeric types such as  byte, short, int, long, char, float, double and these are predefined in the Java language.

  • memory location stores the actual value represented by primitive type
  • When assigned to another variable of same type, its value is copied. both variable representing different copy
  • When primitives are passed as argument to any method, its copy of value is passed.Thus, called method will be accessing copy of value.
  • When you declare a variable of some primitive type, it always holds value of same primitive type

Reference types

Reference types can be any of following : class, interface, array, enums, annotations types

  • We can have unlimited number of reference types as opposed to primitives, as they are defined by user itself
  • memory location stores value of reference (kind of address) to data in memory. of above types.
  • Value of these variables represents some kind of bit-pattern, which Java run-time uses to access the object. in other words,  we can say, its the way Java run-time accesses object in memory using this value. This value is not accessible and is irrelevant to programmer / developer.
  • When assigned to another variable of same type, its reference value copy is made and now both variable will point/ refer to same object
  • When variable of this type is passed to method, then reference value is copied to called method  parameter and method can access/change the object using reference, but not the reference/address of object

One thought on “Types in Java

Add yours

Leave a comment

Create a free website or blog at WordPress.com.

Up ↑