JAVA Object oriented programing language
Platform independent ( can run on any os)
RAM - Temporary Memory
JVM ( Java Virtual Machine ) - RAM has special area to execute java program
JVM has - > Heap Memory : Place where objects are created
- > Stack Memory : Reference to the object which is stored. Variables are also stored in it.
Object - It is an Instance of class ( Real implementation of class)
Object is created in the heap memory in jvm of ram
Object reference is created in stack memory of jvm of RAM
State of Object - At a particular time what data is holded by object
Behavior of object - What all it can do, the functions of object
Create an object :
Trainer t = new Trainer (nage, 1000, hyderabad ) - syntax
{class} {constructor}
Constructor :Constructor will go to class and check the number of variables, data type and how much
memory is required and give information to new operator
Initialize variables when creating objects. { to assign value to variables }
New : new operator will allocate memory for objects in heap .
t : reference variable { reference to object } . To invoke functionality of objects.
Stored in stack memory.
Dot operator - t.allskills();
To invoke functionalities of objects
Object have State and Behavior
,State : At a time what data object is holding
Behavior : Functionalities of object { what all it do }
output
Sample.java class file is created . And 1st phase is compilation phase, command used is javac
Sample.java. Os search for javac files.
And it will generate class file (Sample.class) - in form of byte code {low level language}
This compiled Sample.class file we can execute in any OS (windows,linux,mac…)- Platform
independent
And next is executional phase , byte code line by line will be interpreted (line by line execution
of code to convert code to machine code )
Data Types
Variable : Name to reserved memory location at which some value is stored.
Based on datatype, it will be decided :
● How much memory will be allocated
● Which type of value can be store
Data Types : define the kind of value the variables can hold. Based on data type memory will be
allocated
Primitive Types :
–classes
-data types
Signed Number : can store both positive and negative number and 0
2^8 = 256 -> [ -128 to 127]
Unsigned Number : can store only non negative number
Eg : 0, 62, 827
,2^8 = 256 -> [ 0 to 255]
Memory Occupy by data types :
Int - 32 bit, char - 16 bit, double - 64, long - 64 , short - 16, byte - 8, float - 8 , boolean - 1 bit
Coding
First program
package firstprogram;
public class firstprogram {
public static void main(String args[]) {
System.out.println("Welcome to java training");
}
}
2 class in same package
If classes are in same package :
package firstprogram;
public class secondprogram {
int i = 10;
public void test() { //create test method
System.out.println(i);
}
public static void main(String args[]) {
secondprogram s = new secondprogram(); //call method by create instance
s.test();
}
}
, Different Package
If Custom class is in different package :
Test :
package package1;
import package2.Custom; // import custom class from package2
public class Test {
public static void main(String args[]) {
Sample s = new Sample(); //call method by create instance
s.test(); 1.
Custom c = new Custom(); 3.
c.verify();
}
}
Sample :
package package1;
public class Sample{
int i = 10;
public void test() { 2.
System.out.println(i);
}
}
Custom :
package package2;
public class Custom {
public void verify() { 4.
System.out.println("this is verify method");
}
}
Global Variables
Global Variables - Variables which are declared inside class but outside method
Local Variables - Variables which are declared inside method
Global variables may or may not be initialized
Local variables must be initialized. Else error.
package com.thirddatatypes;
public class sample{
int i = 10;
int j; // not assigning any value to j, but printing value default 0 { global variable }
public void test() { //method
Platform independent ( can run on any os)
RAM - Temporary Memory
JVM ( Java Virtual Machine ) - RAM has special area to execute java program
JVM has - > Heap Memory : Place where objects are created
- > Stack Memory : Reference to the object which is stored. Variables are also stored in it.
Object - It is an Instance of class ( Real implementation of class)
Object is created in the heap memory in jvm of ram
Object reference is created in stack memory of jvm of RAM
State of Object - At a particular time what data is holded by object
Behavior of object - What all it can do, the functions of object
Create an object :
Trainer t = new Trainer (nage, 1000, hyderabad ) - syntax
{class} {constructor}
Constructor :Constructor will go to class and check the number of variables, data type and how much
memory is required and give information to new operator
Initialize variables when creating objects. { to assign value to variables }
New : new operator will allocate memory for objects in heap .
t : reference variable { reference to object } . To invoke functionality of objects.
Stored in stack memory.
Dot operator - t.allskills();
To invoke functionalities of objects
Object have State and Behavior
,State : At a time what data object is holding
Behavior : Functionalities of object { what all it do }
output
Sample.java class file is created . And 1st phase is compilation phase, command used is javac
Sample.java. Os search for javac files.
And it will generate class file (Sample.class) - in form of byte code {low level language}
This compiled Sample.class file we can execute in any OS (windows,linux,mac…)- Platform
independent
And next is executional phase , byte code line by line will be interpreted (line by line execution
of code to convert code to machine code )
Data Types
Variable : Name to reserved memory location at which some value is stored.
Based on datatype, it will be decided :
● How much memory will be allocated
● Which type of value can be store
Data Types : define the kind of value the variables can hold. Based on data type memory will be
allocated
Primitive Types :
–classes
-data types
Signed Number : can store both positive and negative number and 0
2^8 = 256 -> [ -128 to 127]
Unsigned Number : can store only non negative number
Eg : 0, 62, 827
,2^8 = 256 -> [ 0 to 255]
Memory Occupy by data types :
Int - 32 bit, char - 16 bit, double - 64, long - 64 , short - 16, byte - 8, float - 8 , boolean - 1 bit
Coding
First program
package firstprogram;
public class firstprogram {
public static void main(String args[]) {
System.out.println("Welcome to java training");
}
}
2 class in same package
If classes are in same package :
package firstprogram;
public class secondprogram {
int i = 10;
public void test() { //create test method
System.out.println(i);
}
public static void main(String args[]) {
secondprogram s = new secondprogram(); //call method by create instance
s.test();
}
}
, Different Package
If Custom class is in different package :
Test :
package package1;
import package2.Custom; // import custom class from package2
public class Test {
public static void main(String args[]) {
Sample s = new Sample(); //call method by create instance
s.test(); 1.
Custom c = new Custom(); 3.
c.verify();
}
}
Sample :
package package1;
public class Sample{
int i = 10;
public void test() { 2.
System.out.println(i);
}
}
Custom :
package package2;
public class Custom {
public void verify() { 4.
System.out.println("this is verify method");
}
}
Global Variables
Global Variables - Variables which are declared inside class but outside method
Local Variables - Variables which are declared inside method
Global variables may or may not be initialized
Local variables must be initialized. Else error.
package com.thirddatatypes;
public class sample{
int i = 10;
int j; // not assigning any value to j, but printing value default 0 { global variable }
public void test() { //method