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

CoreJava Day 3( Arrays , Static and Non Static )

The document provides an overview of core Java concepts, focusing on arrays, their declaration, initialization, and usage. It explains the differences between static and non-static members, JVM memory management, and reference variables. Additionally, it includes examples and syntax for creating and manipulating arrays and classes in Java.

Uploaded by

sharanvp00
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)
2 views15 pages

CoreJava Day 3( Arrays , Static and Non Static )

The document provides an overview of core Java concepts, focusing on arrays, their declaration, initialization, and usage. It explains the differences between static and non-static members, JVM memory management, and reference variables. Additionally, it includes examples and syntax for creating and manipulating arrays and classes in Java.

Uploaded by

sharanvp00
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
You are on page 1/ 15

Core Java

- HemanthKumar C

Day 3 & Day 4

Day 6 Java Collections Arrays, 2D Arrays, Array List, List


Day 7 Java Collections Collection Framework
ARRAYS
WHAT IS AN ARRAY?
• Array is liner data structure which is used to store homogenous
type of data.
• Array can be created in 2 ways
1.) using new Operator 2.) without using new operators
• Drawbacks:
• In array the size is fixed
• it can store only homogeneous type of data.
EXAMPLE :
• {10,11,12,13}; // integer values
• {10.10, 11.20, 12.50, 13.60 }; // decimal values
• {“Hems”, “Amrutha”, “Pradeep Aldur”, “varsha” }; //String
Values
• {‘A’, ‘B’, ’C’, ‘D’ }; // character Values
Array declaration Store the values into an array
Syntax: array _ name [ index ]= values ;
Syntax: Datatype [ ] array _ name ;
Ex: arr [0] = 10 ;
arr [1] = 20 ;
int [] arr arr [2] = 30 ;
arr [3] = 40 ;

Array size Initialization Array utialization


Syntax: array _ name = new datatype [ size ] ; System.out.println(arr [0]);
System.out.println(arr [2]);
Ex: arr = new int [5] ;
System.out.println(arr [3]);
Array Index Out of Bound Exception  System.out.println(arr [4]);
Array value Re - Initialization
Syntax: array _ name [ index ]= new values ; Ex: arr [3] = 90 ;

Note : If the value is not stored in a particular index and if we try to print , we will get default values.

To find length of an Array

Syntax: array _ name .length ; System.out.println(arr.length);

For(int i =0; i<arr.length ; I++)


{
System.out.println(arr[i]);
}

Array declaration and store the value directly


Syntax: Datatype [ ] array _ name = { v1 , v2 , v3 , v4 } ;
Ex: int abb [ ] = { 10 , 20 , 3 0 , 40 ) ;
abb →

0 1 2 3 4

Copying the value of one Array to another Array


int acc [ ] = abb ; acc →
10 20 30 40 50
0 1 2 3 4

class Array{
public static void main ( String [ ] args ) {
int[] arr = new int[3] ; 2D ARRAY / 2 DIMENSIONAL ARRAY
arr [ 0 ] = 10 ;
arr [ 1 ] = 20 ;
arr [ 2 ] = 30 ;
for ( int i=0; i<= arr . length ; i++ )
{
System.out.println(i+"\t"+ arr[i]);
}}}

class Array {
public static void main ( String [ ] args ){
int[] arr = {10,20,30,40};
System.out.println("index\t values");
for ( int i=0; i<= arr . length ; i++ )
{
System.out.println(i+"\t"+ arr[i]);
}}}
In a class , we have 3 members
• variables
• methods
• constructor
• Variables and methods can be classified into static and
non - static.
• Constructor is always non – static

Class class _ name {


Variable / data members
Method / function members static and non – static
Constructor → non – static
}
STATIC
➢ Any member of the class declared with a keyword static is called as static member of the class.
➢ Static is always associated with class.
➢ Static is one copy.
➢ All the static members will be stored in static pool area.
➢ Whenever we want to access static members from one class to another class then we should use
Class _ name . variable _ name ( ); or Class _ name . method _
name ( );
Note : We can develop multiple classes in a single file.
➢ Whichever class is having main method that class file should be filename.

Ex: B / W the classes with a method as static


class Circle {
static void area() {
final double pi=3.142; int r=5;
double result=pi*r*r;
System.out.println("result is" +result);
}}

class Tester {
public static void main(String[] args) {
Circle. Area();
} }
NON – STATIC
➢ Any member of the class declared without a keyword static is called as Non - static member of
the class.
➢ Non - Static is always associated with object.
➢ Non - Static is multiple copy.
➢ All the Non - static members will be stored in inside the Heap Memory
➢ Whenever we want to access from Non – static to static then we should use
➢ Object . variable _ name or object . method _ name
➢ Reference variable . variable _ name or Reference variable . method _ name

New class _ name ( ) ;


Ex: New Sample ( ) ; → object
Operator constructor

It will create random It will initialize all the


memory space into non– static members into the heap
the heap memory memory heap
JVM MEMORY
➢ Whenever class loader loads the class all the static members will get It has 4 parts
initialized in the static pool area.
1. stack – It is used for execution which follows last in first out [ LIFO ] .
➢ JVM starts executing from main method in the program the first statement is the
object creation , equal operator will work from right to left. 2.Heap memory – It is used to store non – static members of the class. Whenever we create an object at the
➢ New operator will create a random memory space in the heap memory. instance non – static members will be stored in the heap memory.
Constructor will initialize all the non - static members into the heap memory 3.static pool area - It is used to store static members of the class. Whenever class loader loads the class at
while creating the object itself we pass the arguments which will get that instance static members will get initialized in the static pool area.

initialized the constructor and from constructor it will get initialized 4.method area – It is used to store method body or definition . Irrespective of static or non – static all
to the object variable. the method bodies will be stored in method area.
➢ Now in the main method the object address will be stored in the class Circle
references {
variable e1 and through that references variables we point the values. int x=17;
void area(int r)
{
Stack Heap memory final double pi = 3.142;
//int r = 4;
new Circle(); new Circle(); double result = pi * r * r;
System.out.println(result);
}
area() int x=7; int x=7;
Void area() Void area()
}
main() public class Non_Static_method
JVM random memory space {
Class Loader
public static void main(String[] args)
{
// Non – static to static without parameter and return type
main() area() public static void main(String[]args)
new Circle().area(5); // new keyword should be mentioned before writing
class_name.methodName
System.out.println(new Circle().x);
Method area static pool area }
REFERENCE VARIABLE Ex : Tester t1 = new Tester ( ) ;
Reference variable is a special type of variable which is used to Class _ name reference variable operator constructor
store object address. → object
Ex : Class Tester {
Reference variable can hold either null or object address.
Void disp ( ) {
Reference variable declaration
System.out.println("Hii");
Syntax : class _ name reference _ variable ;
}
Ex : int a ;
Tester t1 ;
public static void main(String[ ] args)
{
Tester t1 = new Tester ( );
Reference variable initialization
Syntax : reference _ variable = object ; t1.disp ( ) ;

Ex : t1 = new Tester() ; } }

Reference variable declaration and initialization


Syntax : class _ name reference _ variable = object ;
// homogenous type of object creation
JVM MEMORY
Ex: Non – static to static through reference variable
➢ Whenever class loader loads the class all the static members will get
initialized in the static pool area.
➢ JVM starts executing from main method in the program the first statement is the
object creation , equal operator will work from right to left.
➢ New operator will create a random memory space in the heap memory. public class Non_Static_with_ReferenceVariable
Constructor will initialize all the non - static members into the heap memory {
while creating the object itself we pass the arguments which will get int x=18;
initialized the constructor and from constructor it will get initialized
to the object variable. void area( ) {
➢ Now in the main method the object address will be stored in the final double pi=3.142; int r=4;
references double result=pi*r*r;
variable e1 and through that references variables we point the values. System.out.println(result);
}
Stack Heap memory
public static void main(String[] args)
new Circle(); new Circle();

{
area() int x=18; int x=18; Non_Static_with_ReferenceVariable C1 = new
Void area() Void area()
Non_Static_with_ReferenceVariable();
main()
JVM random memory space C1.area();
Class Loader System.out.println(C1.x);

main() area() public static void main(String[]args) }

Method area static pool area }


Ex : non - static . Demo @ 1bcfc76
Note : multiple objects can be stored in multiple references variable and if any Ref1 address object Ref2
changes made to an objects it will not affect other objects.
Class Demo {
Ref1 address object
Int a=10;
Ref2 address object
public static void main(String[] args) {
Class Demo {
Int a=10; Demo1 d1 = new Demo1( );
public static void main(String[] args) { Demo1 d2 = d1;
Demo1 d1 = new Demo1( ); System.out.println(d1);
System.out.println(d1); System.out.println(d2);
Demo1 d2 = new Demo1( ); }}
System.out.println(d2); Output Demo1 @ 15db9742 ,,
}}
Demo2 @ 15db9742
Output Demo1 @ 15db9742 ,, Demo2 @ 06d69c

Note : Whenever we print the reference variable it print address i.e. fully qualified
path.
Fully qualified path means package name.class_name @ hexadecimal no

You might also like