0% found this document useful (0 votes)
2 views4 pages

Java Data Types

Java data types are categorized into primitive and non-primitive types. Primitive data types include byte, short, int, long, float, double, boolean, and char, each with specific value ranges and uses. Non-primitive data types, created by programmers, include classes, interfaces, and arrays, and they store references to objects rather than actual data.

Uploaded by

nayeem.gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Java Data Types

Java data types are categorized into primitive and non-primitive types. Primitive data types include byte, short, int, long, float, double, boolean, and char, each with specific value ranges and uses. Non-primitive data types, created by programmers, include classes, interfaces, and arrays, and they store references to objects rather than actual data.

Uploaded by

nayeem.gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Java data types categorize the kind of values a variable can hold and how those values are

stored and
manipulated. Java categorizes data types into two main groups: primitive data types and non-primitive (or
reference) data types.
1. Primitive Data Types:
A primitive data type specifies the type of a variable and the kind of values it can hold. There are eight
primitive data types in Java.
Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid
types are byte, short, int and long. Which type you should use, depends on the numeric value.

 byte: 8-bit signed integer, ranging from -128 to 127.


Example:
byte myNum = 100;
System.out.println(myNum);

 short: 16-bit signed integer, ranging from -32,768 to 32,767.


Example:
short myNum = 5000;
System.out.println(myNum);

 int: 32-bit signed integer, the most commonly used integer type. Stores whole numbers from
-2,147,483,648 to 2,147,483,647.
Example:
int myNum = 100000;
System.out.println(myNum);

 long: 64-bit signed integer, used for very large integer values. Stores whole numbers from
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Example:
long myNum = 15000000000L;
System.out.println(myNum);

Floating point types represents numbers with a fractional part, containing one or more decimals. There
are two types: float and double.

 float: 32-bit single-precision floating-point number (Stores fractional numbers.), used for decimal
values. Sufficient for storing 6 to 7 decimal digits. A floating point number can also be a scientific
number with an "e" to indicate the power of 10:
Example:
float myNum = 5.75f;
System.out.println(myNum);
 double: 64-bit double-precision floating-point number, the default type for decimal numbers and
offers higher precision than float. Sufficient for storing 15 to 16 decimal digits.
Example:
double myNum = 19.99d;
System.out.println(myNum);

Example:
float f1 = 35e3f;
double d1 = 12E4d;
System.out.println(f1);
System.out.println(d1);
O/P: 35000.0
120000.0

 boolean: Represents a logical value, either true or false.


Example:
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false

 char: 16-bit Unicode character, used to store a single character/letter or ASCII values.
Exmaple:
char myGrade = 'B';
System.out.println(myGrade);

char myVar1 = 65, myVar2 = 66, myVar3 = 67;


System.out.println(myVar1);
System.out.println(myVar2);
System.out.println(myVar3);
o/p : A
B
C

2. Non-Primitive (Reference) Data Types:


These are more complex and are not predefined by the language; they are created by the programmer or
are part of the Java API. Non-primitive data types are called reference types because they refer to
objects. They store references (memory addresses) to objects rather than the actual data.
 Classes: User-defined blueprints for creating objects, encapsulating data and methods. Examples
include String, Scanner, and custom classes.
 Interfaces: Blueprints of a class, defining a set of methods that a class must implement.
 Arrays: Used to store multiple values of the same data type in a single variable.
Example:

// Primitive data types


int age = 30;
double price = 19.99;
boolean isActive = true;
char initial = 'J';

// Non-primitive data types


String name = "John Doe";
int[] numbers = {1, 2, 3};

The main differences between primitive and non-primitive data types are:

 Primitive types in Java are predefined and built into the language, while non-primitive types are
created by the programmer (except for String).
 Non-primitive types can be used to call methods to perform certain operations, whereas primitive
types cannot.
 Primitive types start with a lowercase letter (like int), while non-primitive types typically starts
with an uppercase letter (like String).
 Primitive types always hold a value, whereas non-primitive types can be null.

You might also like