0% found this document useful (0 votes)
12 views

Data Type in Java

Uploaded by

Prasuk Goyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Data Type in Java

Uploaded by

Prasuk Goyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Type In Java

Java is statically typed and also a strongly typed language because, in


Java, each type of data (such as integer, character, hexadecimal, packed
decimal, and so forth) is predefined as part of the programming
language and all constants or variables defined for a given program must
be described with one of the Java data types.
Data Types in Java
Data types in Java are of different sizes and values that can be stored in
the variable that is made as per convenience and circumstances to cover
up all test cases. Java has two categories in which data types are
segregated
1. Primitive Data Type: such as boolean, char, int, short, byte, long,
float, and double. The Boolean with uppercase B is a wrapper class
for the primitive data type boolean in Java.
2. Non-Primitive Data Type or Object Data type: such as String,
Array, etc.

Data Type Size Description

byte 1 byte Stores whole numbers from -128 to 127

short 2 bytes Stores whole numbers from -32,768 to 32,767

int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647

long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to


9,223,372,036,854,775,807

float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal


digits

double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits

boolean 1 bit Stores true or false values

char 2 bytes Stores a single character/letter or ASCII values


Primitive Data Type:

Boolean Types
Very often in programming, you will need a data type that can only have one of two values,
like:

 YES / NO
 ON / OFF
 TRUE / FALSE

For this, Java has a boolean data type, which can only take the values true or false:

Example

Get your own Java Server

boolean isJavaFun = true;

boolean isFishTasty = false;

System.out.println(isJavaFun); // Outputs true

System.out.println(isFishTasty); // Outputs false

Characters
The char data type is used to store a single character. The character
must be surrounded by single quotes, like 'A' or 'c':

Example

Get your own Java Server


char myGrade = 'B';

System.out.println(myGrade);

Byte
The byte data type can store whole numbers from -128 to 127. This
can be used instead of int or other integer types to save memory
when you are certain that the value will be within -128 and 127:

Example

Get your own Java Server

byte myNum = 100;

System.out.println(myNum);

Short
The short data type can store whole numbers from -32768 to 32767:

Example

short myNum = 5000;

System.out.println(myNum);

Int
The int data type can store whole numbers from -2147483648 to
2147483647. In general, and in our tutorial, the int data type is
the preferred data type when we create variables with a numeric
value.
Example

int myNum = 100000;

System.out.println(myNum);

Long

The long data type can store whole numbers from -


9223372036854775808 to 9223372036854775807. This is used when int
is not large enough to store the value. Note that you should end the value
with an "L":

Example

long myNum = 15000000000L;

System.out.println(myNum);

Floating Point Types


You should use a floating point type whenever you need a number with a
decimal, such as 9.99 or 3.14515.

The float and double data types can store fractional numbers. Note that
you should end the value with an "f" for floats and "d" for doubles:

Float Example

float myNum = 5.75f;

System.out.println(myNum);

Double Example
double myNum = 19.99d;

System.out.println(myNum);

Use float or double?

The precision of a floating point value indicates how many digits the
value can have after the decimal point. The precision of float is only six
or seven decimal digits, while double variables have a precision of about
15 digits. Therefore it is safer to use double for most calculations.

Non-Primitive Data Types


Non-primitive data types are called reference types because they refer
to objects.

The main difference between primitive and non-primitive data types


are:

 Primitive types are predefined (already defined) in Java. Non-


primitive types are created by the programmer and is not defined
by Java (except for String).
 Non-primitive types can be used to call methods to perform certain
operations, while primitive types cannot.
 A primitive type always has a value, while non-primitive types can
be null.
 A primitive type starts with a lowercase letter, while non-primitive
types starts with an uppercase letter.

1. Strings
Strings are defined as an array of characters. The difference between a character
array and a string in Java is, that the string is designed to hold a sequence of
characters in a single variable whereas, a character array is a collection of separate
char-type entities.
Syntax: Declaring a string
<String_Type> <string_variable> = “<sequence_of_string>”;
Example:
// Declare String without using new operator
String s = "Hello Java";
// Declare String using new operator
String s1 = new String("Hello Java");
2. Arrays
Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.

To declare an array, define the variable type with square brackets:

String[] cars;

We have now declared a variable that holds an array of strings. To insert


values to it, you can place the values in a comma-separated list, inside
curly braces:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

You might also like