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

CoreJava Day2

Uploaded by

kewatkuldeep558
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)
9 views

CoreJava Day2

Uploaded by

kewatkuldeep558
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/ 5

Core Java – Day 2

Group of classes – All classes with in same package or by


importing a package (Same Location) are acsessable to each
other ie.That can be initialized from each other.

Dependent class are class which does not have main method
and are initialized from another class example Shop class is
initialized from Login class.

Independent class are class with main method and can be


called directly (They can also be initialized from another
class) like Register class can be initialized from main or from
Login class when Login fails.

Note – For Better Understanding watch Eclipse example


Login.java, Register.java and Shop.java.

Inner and Outer class – Inner class is defined inside Outer


class.
1. Program is saved by name of Outer class.
2. Main() remains in Outer class.
3. Inner class can be static
4. We cannot create Constructor of inner class it is
initialized from Outer class i.e. always dependent on
Outer class (Inner class creates memory inside Outer
class)
Outer o = new Outer();
Inner i = new Inner(); X wrong

Outer.Inner i = new Outer().new Inner();

Note – Please look at Demo Example for better


understanding.

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