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

CoreJava Variable

Variable in JAVA

Uploaded by

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

CoreJava Variable

Variable in JAVA

Uploaded by

roliguide
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Core Java – Day 2

Variable – As already discussed variable store data. Data is


of Primitive or Non Primitive Type.

int i = 10; // i is a variable which store data 10 which is of int


type , size 4 byte.

Class -Type reference-variable = new->create classobject data


Test t = new Test(); // t is a variable which store class data
which is of Test type.

What is Primitive data - Java datatype byte,short,int,long


float,double, float, boolean are Primitive Data which we can
understand How much memory , what range of values..

What is Non Primitive data – All class type of Data is Non


Primitive which we do not understant How much memory it
takes or what range.. String is a class hence String is Non
Primitive.
Data Type casting - All Primitive Type of Data can be
converted to each other either Expicitly or Implicitly.
Similarly All Non Primitive Data can be converted to each
other but We cannot Convert Primitive and Non Primitive
Data to each other.

Implicit Type cast – Lower range of data with Less Memory


can be implicitly be converted to Higher range of Data with
more Memory. This is also called Upcasting of Data.

Example-
byte b = 10; // 10 is of byte type
int i = b; // assign byte to int
System.out.println(i); // now 10 is of int type
i+=240;
System.out.println(i); // 250 ie more than byte range..

Explicit Type Casting – In case of Explicit Type casting


Higer range of Data with more memory is Downcasted to
Lower Range of Data with Less Memory.
Note – It can result in Data Loss
A) No Data Loss
int i = 10; // 10 is of int type 4 bytes
Convert int to byte
byte b = i; X wrong
byte b = (byte) i ; // downcast
System.out.println(b); // 10 is of byte type 1 byte no data loss

B) Data Loss
int i = 128;
Convert int to byte
byte b = (byte) i; // downcast but 128 is out of Range
System.out.println(b); // -127 as it is out of range it starts from
negative i.e data loss.

Note : Look at Examples for More Understanding.

You might also like