0% found this document useful (0 votes)
9 views6 pages

#6 NonPrimitive Variables

Uploaded by

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

#6 NonPrimitive Variables

Uploaded by

achieve2025ssc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA VARIABLES

(Non Primitive Data


Types In Depth)

Data Types (Non-Primitive Date Types


=>
Reference
-
data
X
t I here are
mainly "
type of enference types :

Class

-string
Interface
-

Array

enference ?
* What is

understand with
Let's an
example of class

So Let's create a class

of
to create class
Now an
object Employee
new
keyword allocates a
memory
block
objecta the variable's

holds to actual
reference memory
a

.
name

Heap

Memory
holds Actual
emp Object reference
Variable Object

In Java value So with


*
,
everything
is
pass by .
the
help of
enference variables we're
achieving the
functionality of pointers in

CPP ,

*
String
immutable in Java
strings are
-

St contains
String literal
-

Constant Pool
Inside heap is
fixed memory String
there Kla
,
a
Space
holds
So the
String variable a
reference of corresponding string
literal in Pool.
String
Constant

"hello"
Eg -String
: si =

"hello"
S
tring S2 =
Heap

holds
S reference "hello"

String
Sz Constant Pool
Since immutable holds
strings are same
strings ,
I
reference
to

Constant Pool
same
string
literal in
String
So "hello" .
both BS2 holds
s
,
reference
to
string same literal

This
appliesfor normal strings
. If
we create
using new
keyword
,
it'll be stored normal
as
object heap
in
.

Eg ("hello"
:
String B = new
String
-

*
Interface
To understand let's create it
better
,
an
interface simplement .

let's create
Now a
few objects in a class : -

Public class Student &


public static void main
(String args[J)
Person softwar Engineer = new
Engineer 1) ;
Person teacher = new Teacher 11;

Teacher teacher new Teacher 11


;
=

Engineer Software Engineers = new


Engineer 1);
Software Engineer reference Engineer Heap
(Type : Person)

teaches Teacher
reference
(Type : Person)

So here the variables &teacher


Software Engineer holds a
reference of
Engineer & Teacher
type objects in the
heap memory
.
So store the child in
objects
of parent
we can a a one or we can

Store the
objects in same class
itself but we cannot create an

object of interface .

ic Person Person /) ;
person = new
-
-
I his is
wrong
.

=>
Awway
I

Sequence
of memory storing
same
datatype
-

-
~

-int[Jarr (5];
For
eg
: = new int Cap acity of array
or int arr () = new int [5]; Heap

arz
reference 18 6 10 40 20

O 12 3 Y

arr [0) = 10 arr <] = 6 ar r (2) = 10


,
ar r [3] = 40 arr (4] = 20
, , ,
*
Arrays can be
assigned in
multiple ways I can be
of multiple
types like ID
,
&D exc
.

=> Wrapper Class

Autoboxing Unboxing
For each
of
the
primitive data
types ,
we have
corresponding
reference types that are known as
Wrapper classes

-
int
Integer
Chay Character

Short Short

byte Byte
long Long
float Float

double Double

boolean Boolean

need
for
*
Why the
wrapper classes ?
We
advantages ofpassing by reference For example
> the
get
-

we've declared
if
a
wrapper type
of
int i
Integer type
. e
,
we can

it later it'll in well because


change change
as
memory
on

wrapper we're storing reference


in .

stored
In primitive this won't be
possible as
they're in stack

k not
heap
.
The collections objects only data
reference types
works i
-

on .
c on
,

So we need class to use collections


.
wrapper

Autoboxing
*

To primitive data
convert a
type to its
wrapper
-
.

Eg : - inta = 10 ;

Integer al a

- primitive to its
wrapper

*
Unboxing : -

To convert a class to
primitive
wrapper
-

Eg :
Integer v = 20
;

intx1 = x
;
- to
wrapper primitive

=> Constant Variable


We cannot the value constant . This is
variable
change of
-

using final keyword


created .
usually
agi-Static final VAR 10 ; =

Y
only of
It means It means the value

one
copy
exist VAR can't be .
changed

You might also like