About NewTechnoBuzz
Advertise
Contact Us

Java - Basic DataTypes

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables. Java is a strongly typed language. It means that every variable has a type, every expression has a type, and every type is strictly defined. Second, all assignments, whether explicit or via parameter passing in method calls, are checked for type compatibility. There are no automatic coercions or conversions of conflicting types as in some languages. The Java compiler checks all expressions and parameters to ensure that the types are compatible. Any type mismatches are errors that must be corrected before the compiler will finish compiling the class. There are two data types available in Java:
  • Primitive Data Types
  • Reference/Object Data Types

Primitive Data Types

There are eight primitive data types defined by Java. Primitive data types are predefined by the language and named by a keyword. Let us now look into detail about the eight primitive data types.

bype

  • It is smallest integer type.
  • It has signed 8-bit type that has a range from –128 to 127.
  • Minimum value is -128 (-2^7)
  • Maximum value is 127 (inclusive)(2^7 -1)
  • Default value is 0
  • It is useful when you’re working with a stream of data from a network or file.
  • Syntax - byte b, c;
  • Example - byte a = 200 , byte b = -10

short

  • It is a signed 16-bit type.
  • Minimum value is -32,768 (-2^15)
  • Maximum value is 32,767 (inclusive) (2^15 -1)
  • Default value is 0
  • Syntax - short b, c;
  • Example - short s = 14000, short r = -2000

int

  • It is the most commonly used integer type.
  • It has signed 32-bit type that has a range from –2,147,483,648 to 2,147,483,647.
  • Minimum value is - 2,147,483,648.(-2^31)
  • Maximum value is 2,147,483,647(inclusive).(2^31 -1)
  • Default value is 0
  • Syntax - int b, c;
  • Example - int a = 200 ,int b = -10

long

  • It is signed 64-bit type.
  • Minimum value is -9,223,372,036,854,775,808.(-2^63)
  • Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
  • Default value is 0L
  • It is useful when big, whole numbers are needed where an int type is not large enough to hold the desired value.
  • Syntax - long b, c;
  • Example - long a = 200 , long b = -10

float

  • It uses 32 bits of storage.
  • It is a single-precision 32-bit IEEE 754 floating point.
  • Default value is 0.0f
  • It is useful you need a fractional component, but don’t require a large degree of precision.
  • Syntax - float b, c;
  • Example - float a = 200.13 , float b = -10.23

double

  • It uses 64 bits to store a value.
  • It is a double-precision 64-bit IEEE 754 floating point.
  • Default value is 0.0d
  • Syntax - double b, c;
  • Example - double a = 200.13 , double b = -10.23

char

  • It is a single 16-bit Unicode character.
  • Minimum value is '\u0000' (or 0)
  • Maximum value is '\uffff' (or 65,535 inclusive)
  • It is used to store any character.
  • Syntax - char b, c;
  • Example - char a = 'a' , char b = 'b'

boolean

  • It has two possible values: true and false.
  • It is used for simple flags that track true/false conditions.
  • Default value is false
  • Syntax - boolean b, c;
  • Example - boolean a = false

Reference/Object Data Types

  • Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy etc.
  • Class objects, and various type of array variables come under reference data type.
  • Default value of any reference variable is null.
  • A reference variable can be used to refer to any object of the declared type or any compatible type.
  • Example: Animal animal = new Animal("giraffe");
Below is the example of Java Primitive datatypes example:

Java Literal

First Java Program




Java Userful Resources

A collection of Java Sites, Books and Articles is given at this page.

Java Programming Books

0 comments