no

Learn Java Programming: Lesson 5 - Variables and Data Types

Go Back to Course Outline I. Introduction Java programming language is statically-type, which means all variables must be declared f...

Go Back to Course Outline

I. Introduction

Java programming language is statically-type, which means all variables must be declared first before they can be used. There are two types of data in Java:
  1. Primitive
    1. This type of variable is stored directly on the stack, which means faster access.
    2. It is defined by its size and type and represents raw value, which cannot be null.
    3. Stores values.
    4. There are 8 primitive data types and each has an equivalent wrapper class.
  2. Non-primitive or Reference Type because they refer to Object
    1. It describes a concrete type and can have methods that can be called to perform operations.
    2. The default value is null.
    3. A good example is a String which is available from Java. Classes and interface are an example too.
    4. Stores the handle or address to object (not reference).
    5. Stored in heap memory.

I.1 How Data Types are Stored in Memory

II. Primitive Data Types

 
Type 
Size
Range or Value
Wrapper
Example
Default
byte
1 byte
-128 - 127 
Byte
byte b = 1;
0
short
2 bytes
-32,768 - 32,767
Short
short s = 2;
0
int
4 bytes
-231 - 231-1
Integer
int i = 3;
0
long
8 bytes
-263 - 263-1
Long
long l = 4;
0l
float
4 bytes
Precision max of 7 decimal digits
Float
float f = 5.0f;
0f
double
8 bytes
Precision max of 16 decimal digits
Double
double d = 6.25;
0d
boolean
1 bit
true or false
Boolean
boolean b = true;
false
char
2 bytes
16-bit Unicode character from 0 - 65,535
Character
char c = ‘e’;
\u0000 or null

Basically primitive number data types are divided into two groups.

  1. Integer - it can store a negative or positive value.
    1. byte, short, int, long
    2. If we want to save memory we should use the appropriate number type for our requirement. In case, we don’t know then it’s always safer to use the largest which is decimal at the cost of memory.
  2. Decimal - a number that can store the fractional parts.
    1. float, double
    2. By adding e to a float number, we multiply that value to the power of ten. For example, 1e3f is equal to 1,000.0.

II.2 Booleans

It’s the simplest type of primitive data as it can only hold either true or false value.

II.3 Char

  • It is used to store a single character. The character must be surrounded by a single quote. For example ‘c’.
  • Alternatively, we can use the ASCII decimal value to display a certain character. For example, a is 97. See this link https://en.wikipedia.org/wiki/List_of_Unicode_characters.

III. Reference Data Type

Data types that refer to objects. Note that the 8 primitive data types each have their own equivalent reference type.

III.1 Autoboxing

The ability to automatically convert a primitive data type into a reference data type. For example:
Integer x = 10;

IV. References

  • https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html
  • https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html


              Post a Comment Default Comments

              Outsourcing

              Are you looking for Java Developers in the Philippines? Get in touch.

              Donations

              If you like what I do, you can support this channel by buying me a coffee. I would be grateful for your contribution!

              item