Java Data Types: Spotle - Ai/learn

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Spotle.

ai Study Material
Spotle.ai/Learn

Java Data Types


Variables In Java

//declaring a variable x of type


//integer and assigned value
A variable is a named field containing
information that your program uses. You //30
can also think of a variable as a name of int x = 30;
memory location where the variable value
is stored. //declaring a variable c of type
In Java, a variable has to be declared //character and assigned value
before it is used. //‘r’

Char b = ‘r’;

Spotle.ai Study Material


Spotle.ai/Learn 2
Data Types

Data Types refer to the type of data you will store in a variable. For
example a variable can be of type number, string or an object of a
custom class.

Spotle.ai Study Material


Spotle.ai/Learn 3
Primitive And Non-Primitive Types

A primitive data type is pre-built or defined by the programming


language, in this case Java. The size and type of variable values are
fixed and you do not have to do anything special to use them.

Non-primitive, or reference data types, don't store the value, but


store a reference to that value. Reference data types can be String ,
array, class or interface

Spotle.ai Study Material


Spotle.ai/Learn 4
Data Types In Java
Data Types

Primitive Data Non-Primitive


Types Data Types

Numeric Character Object Array

byte short

int long

float double

boolean

Spotle.ai Study Material


Spotle.ai/Learn 5
Primitive Data Types
Primitive
Data Types

Floating
Integer
Point

byte short float double


( 1 byte) ( 2 bytes) (4 bytes) (8 bytes)

int long
(4 bytes) (8 bytes)

Character Boolean

char boolean
(2 bytes) (1 byte)

Spotle.ai Study Material


Spotle.ai/Learn 6
Integers

There are 4 types of integer data types in Java – byte, short, int, long.

//A byte type variable takes 1 byte of storage and can store values from -128 to 127
byte a = 2;

//The short data type is a 16-bit. It has a minimum value of -32,768 and a maximum value of
32,767
short b = 156;

//The int data type is a 32-bit signed two’s complement integer, taking values from -2^31 to
2^31-1
int c = 32769;

//The long data type is a 64 bit’two’s complement integer taking values from -2^63 to 2^63-
long l = 24678822;

Spotle.ai Study Material


Spotle.ai/Learn 7
Floating Points

There are 2 types of floating point data types in Java – double and
float.

Single-precision 32-bit IEEE 754 floating point. Its value range is


unlimited.
float f = 56.8929929;

//double data type is a double-precision 64-bit IEEE 754 floating point. Its
value range is unlimited.
double d = 4.54877;

Spotle.ai Study Material


Spotle.ai/Learn 8
Double Vs Float In Java
Float Double
Precision A float gives you A double gives you
approx. 6-7 decimal approx. 15-16
digits precision. decimal digits
precision.
Storage Needs 4 bytes of Needs 8 bytes of
storage. storage.
Default Use Not Applicable By default, Java
designates a floating
point literal as double.
Applications Use where storage is Use applications
a constraint. which need more
precision.

Spotle.ai Study Material


Spotle.ai/Learn 9
The Boolean Datatype

A boolean type is declared with the boolean keyword and only takes
the values true or false:

boolean isSaladHealthy = true;


boolean isSaladTasty = false;
System.out.println(isSaladHealthy); // Outputs true
System.out.println(isSaladTasty); // Outputs false

Spotle.ai Study Material


Spotle.ai/Learn 10
The Char Datatype

The char data type is a single 16-bit Unicode character. A char is a


single character:

char grade = ‘A’;


System.out.println(grade); // Outputs A

Spotle.ai Study Material


Spotle.ai/Learn 11
The Non-Primitive Datatype

Non-Primitive
Datatypes

Array String Class Interface

Spotle.ai Study Material


Spotle.ai/Learn 12
Class – Defining custom data types

A class is a data type in Java that


defines a template or blueprint for
an object. In other words a class
refers to the category or type of
objects. A class allows you to
declare custom data types.

For example you can define a class


cat or car or savings accounts to
store objects of specific types. A
class has variables defining
properties and methods defining
behavior of its objects.

Spotle.ai Study Material


Spotle.ai/Learn 13
Class Syntax

Spotle.ai Study Material


Spotle.ai/Learn 14
Strings In Java

A String in Java, representing a collection of characters, is actually an


object of type java.lang.String. The class contains methods that can
perform certain operations on strings.

String s = “hello”;
System.out.println(“The length of s is ” + s.length());//outputs The length
of s is 5

Spotle.ai Study Material


Spotle.ai/Learn 15
Arrays In Java

Arrays are used to store multiple homogeneous values in a single


variable. Note, in Java array indexes start from zero.

String fruits[] = {“apple”,“banana”,”mango”};


System.out.println(“The third fruit is” + fruits[2]); //outputs The third fruit
is mango

Spotle.ai Study Material


Spotle.ai/Learn 16
Interface

An interface in Java is used to specify the contract or capabilities that


implementing classes must implement. An interface has abstract
methods, with only signatures, which are then implemented by some
classes. For example, every corporate department may implement the
Security Interface.

interface Security
{
boolean accessCheck(String employeeToken);
}

Spotle.ai Study Material


Spotle.ai/Learn 17
Next Lecture:
Deep Dive
Into Java
Strings

Spotle.ai Study Material


18
Spotle.ai/Learn

You might also like