Data Types

The data type of a variable represents the type of that variable. It decides the range and memory required by that variable.

The type of variables and expressions is known to the compiler and the assignments are checked by the compiler for type compatibility. Hence, java is a statically typed language.

There are two types of variables in java:

  • Primitive-type
  • Reference-type

Primitive Data Types:

These are the basic types. There are total eight primitive data types:

Data Type Size(bytes) Default Value
byte 1 0
short 2 0
int 4 0
long 8 0
float 4 0.0
double 8 0.0
char 2 ‘ ‘(space)
boolean Decided by jvm False

data type

Some important points:

  • Old languages like C/C++ are ASCII code based and the the no. of different allowed ASCII code characters are less than or equal to 256. Hence, to represent these 256 characters 8-bits are enough (2^8=256). Hence, the size of char in old languages is 1 byte.
  • But java is Unicode based and the number of different Unicode characters are more than 256 but less than or equal to 65536. Hence, it requires 2 bytes or 16 bits.
  • Char array is preferred over String because String is immutable and the object is destroyed only when garbage collector clears it from the String pool otherwise it remains in the pool whereas in case of char array we can explicitly wipe the data after we are done with it or even overwrite the data.

4 thoughts on “Data Types

Add yours

  1. You really make it appear really easy with your presentation however I to find this matter to be really one thing that I feel I might by no means understand. It kind of feels too complex and very vast for me. I’m taking a look ahead for your next submit, I will attempt to get the grasp of it!

    Liked by 1 person

  2. I am now not certain where you are getting your info, but great topic. I must spend some time studying more or working out more. Thank you for wonderful information I used to be on the lookout for this information for my mission.

    Like

  3. Nice blog right here! Additionally your web site rather a lot up very fast! What host are you using? Can I am getting your associate hyperlink in your host? I want my web site loaded up as quickly as yours lol

    Like

Leave a comment

Start a Blog at WordPress.com.

Up ↑