Prove that this variable stores current object reference

1
Prove that this variable stores current object reference.

/* Prove that this variable stores current object reference. */

class A
{
int x,y;

A(){
x=5;
y=6;
}

A(int x,int y){
x=7;
y=8;
}

void m1(){
System.out.println("In m1");
System.out.println(x+"..."+y);

m2();
System.out.println("In m1 after m2 call");
System.out.println(x+"..."+y);
}

void m2(){
x=14;
y=15;

new B().m3(this);
}
}

class B
{
int p=5;
int q=6;

B(){
p=p+2;
q=q+3;
}

void m3(A a){
a.x=p+a.x;
a.y=q+a.y;

new C(this,a);
}
}

class C{
C(B b,A a){
b.p=a.x+2;
b.q=a.y+3;

a.x=b.p;
a.y=b.q=a.x+5;
}
}

class TVSCOReference  //This Variable Stores Current Object Reference.
{
public static void main(String[] args)
{
A a1=new A();
A a2=new A(1,2);

System.out.println("a1,a2 values before m1 call");
System.out.println(a1.x+"..."+a1.y);
System.out.println(a2.x+"..."+a2.y);

a1.m1();
System.out.println("a1,a2 values after m1 call");
System.out.println(a1.x+"..."+a1.y);
System.out.println(a2.x+"..."+a2.y);
}
}

Compilation:
G:\netlojava>javac TVSCOReference.java

Execution:
G:\netlojava>java TVSCOReference

Out Put:
a1,a2 values before m1 call
5...6
0...0
In m1 5...6 In m1 after m2 call
23...28
a1,a2 values after m1 call
23...28
0...0

1 comments:

Can we share object as argument object to any method

0

/* Can we share object as argument object to any method.
Ans: yes, we must defined the method with parameter with that object class type. For example A class object we can share as a current to only A class methods. If can be shared as argument objects to either A class or B class */


class A
{
void m1()
{
System.out.println("class A and method m1");
}
void m2(A a)
{
System.out.println("class A and method m2");
}
}
class B
{
void m3()
{
System.out.println("class B and method m3");
}
void m4(A a)
{
System.out.println("class B and method m4");
}
}

class Test
{
public static void main(String[] args)
{
A a1=new A();
A a2=new A();

a1.m1();
a1.m2(a2);

// a1.m3(); //error: cannot find symbol a1.m3();
// a1.m4(a2);  //error: cannot find symbol a1.m4(a2);

B b1=new B();

b1.m3();
b1.m4(a2);
}
}

Compilation:
G:\netlojava>javac Test.java

Execution:
G:\netlojava>java Test

Out Put:
Continue reading →

0 comments:

Procedure in Creating Object.

0

Procedure in Creating Object:
In an object creation there are two phases
1) Object memory creation.
2) Object memory initialization.

The above two phases has four stemps.
example: Student s1=new Student
step-1: Referenced variable memory creation.
step-2: New keyword execution.
   2.1: As part of this step object memory is created with an non-static variables by assigning default values and then assigned value.
step-3: Invoking constrantes
new keyword invokes constracts by passing current object reference.
   3.1: As part of this step control is send to constrants body and reference is store by creating this keyword memory forther constraints logic is executed for initializing this object non-static variables.
step-4: After control return from constracter, new keyword return current object reference and store the reference is 's1' variable then 's1' variable start pointing to this object.

// Prog: NetloJavaInstitute.java

//Student.java

class Student {
 int rollNo;
 String name;
 String institute;

 Student(int rn,String nm,String ins){
  rollNo=rn;
  name=nm;
  institute=ins;
 }
}

//NetloJavaInstitute.java

 class NetloJavaInstitute{
  public static void main(String args[]){

   Student s1=new Student(1,"james","netlojava");
   Student s2=new Student(2,"gosling","netlojava");

   System.out.println("Student 1 Object Data");
   System.out.println("Roll No:"+s1.rollNo);
   System.out.println("Name:"+s1.name);
   System.out.println("Institute:"+s1.institute);

   System.out.println("\nStudent 2 Object Data");
   System.out.println("Roll NO:"+s2.rollNo);
   System.out.println("Name:"+s2.name);
   System.out.println("Institute:"+s2.institute);
  }
 }

compilation:
G:\netlojava>javac NetloJavaInstitute.java

Execution:
G:\netlojava>java NetloJavaInstitute

Out Put:

Continue reading →

0 comments:

Provide that 'this' variable stores current object reference.

0


/* Provide that 'this' variable stores current object reference.
print this variable in constructor and non-static method. In both placess same output is printed. i.e., classname@hashcode   */

// prog: ThisNSM.java

class A
{
A(){
System.out.println("In Constructor this:"+this);
}
void m1()
{
System.out.println("In NSM this:"+this);
}
}


class ThisNSM
{
public static void main(String[] args)
{
A a=new A();
System.out.println("In main a:"+a);
a.m1();
}
}

Compilation:
G:\netlojava> javac ThisNSM.java

Execution:
G:\netlojava> java ThisNSM

Out Put:

Continue reading →

0 comments:

6 uses of 'this' keyword

0

6 uses of 'this' keyword:

1. Stroring current object reference.
2. Accessing non-static variables and methods.
3. Accessing other constructor of same class without creating new object.
4. Differentiating non-static variables memory from one object to other object of some class in constructor, non-static block and non-static method.
5. Differentiating non-static variables from a parameter or from a local variable if both have same name.
6. Sending current object as argument and return type to other methods of same or another class.

0 comments:

real world object

0

//Real World Object creation. Reduce the redundancy.

class  Employee
{
int empid;
String name;
String job;
Employee(int eid,String en,String ej)
{
empid=eid;
name=en;
job=ej;
}
void Display()
{
System.out.println("\n Employee Id "+empid+" Details");
System.out.println("Employee Id:"+empid);
System.out.println("Employee Name:"+name);
System.out.println("Employee JOb:"+job);
}
}


class CEmployee
{
public static void main(String[] args)
{
Employee e1=new Employee(1,"Smith","Java Developer");
Employee e2=new Employee(2,"John","Java Developer");
Employee e3=new Employee(3,"Scott","java developer");

e1.Display();
e2.Display();
e3.Display();
}
}


OutPut:


 Employee Id 1 Details
Employee Id:1
Employee Name:Smith
Employee JOb:Java Developer

 Employee Id 2 Details
Employee Id:2
Employee Name:John
Employee JOb:Java Developer

 Employee Id 3 Details
Employee Id:3
Employee Name:Scott
Employee JOb:java developer



0 comments:

Purpose of Return statement.

0

Purpose of Return statement:
/*Basically return statement is used to terminate method execution and for sending control back to calling method.
-> "return;" sends control back to calling method without value.
-> "return <value>;" send's control back to calling method with value. */

//What is the Output from the below program.
//PORStatement.java

class PORStatement
{
static void method1(int a)
{
System.out.println("Before if");
if(a==12)
{
System.out.println("In if");
return;
}
System.out.println("After if");
System.out.println("Hi");
}
static int method2(int a)
{
System.out.println("Before if");
if(a==12)
{
System.out.println("In if");
return a+10;
}
System.out.println("After if");
System.out.println("Hi");
return 30;
}
public static void main(String[] args)
{
method1(12);
method1(24);

method2(12);
method2(24);
}
}

compilation:
G:\netlojava>javac PORStatement.java

Execution:
G:\netlojava>java PORStatement

Out Put:

Before if
In if
Before if
After if
Hi
Before if
In if
Before if
After if
Hi

0 comments:

program to create real world object

0

program to create real world object

/* Write a program to create real world object called student in Java. Create to real world student object from the class with data.
rollno =1
name =james
institute =netlojava

rollno =2
name =gosling
institute =netlojava */

//Student.java

class Student {
int rollNo;
String name;
String institute;

Student(int rn,String nm,String ins){
rollNo=rn;
name=nm;
institute=ins;
}
}

//NetloJavaInstitute.java

class NetloJavaInstitute{
public static void main(String args[]){

Student s1=new Student(1,"james","netlojava");
Student s2=new Student(2,"gosling","netlojava");

System.out.println("Student 1 Object Data");
System.out.println("Roll No:"+s1.rollNo);
System.out.println("Name:"+s1.name);
System.out.println("Institute:"+s1.institute);

System.out.println("\nStudent 2 Object Data");
System.out.println("Roll NO:"+s2.rollNo);
System.out.println("Name:"+s2.name);
System.out.println("Institute:"+s2.institute);
}
}

compilation:
G:\netlojava>javac NetloJavaInstitute.java

Execution:
G:\netlojava>java NetloJavaInstitute

Out Put:
Student 1 Object Data
Roll No:1
Name:james
Institute:netlojava

Student 2 Object Data
Roll NO:2
Name:gosling
Institute:netlojava



Note: In student class constraintes replace parameter names with non-static variable names as shown below.
Student(int rollNo,String name,String institute) { rollNo=rollNum; name=name; institute=institute; } then compile and execute NetloJavaInstitute.java

OutPut:
Student 1 Object Data Roll No:0 Name:null Institute:null Student 2 Object Data Roll NO:0 Name:null Institute:null


//Real World Object creation.Reduce and redundancy.

class  Employee
{
int empid;
String name;
String job;
Employee(int eid,String en,String ej)
{
empid=eid;
name=en;
job=ej;
}
void Display()
{
System.out.println("\n Employee Id "+empid+" Details");
System.out.println("Employee Id:"+empid);
System.out.println("Employee Name:"+name);
System.out.println("Employee JOb:"+job);
}
}


class CEmployee
{
public static void main(String[] args)
{
Employee e1=new Employee(1,"Smith","Java Developer");
Employee e2=new Employee(2,"John","Java Developer");
Employee e3=new Employee(3,"Scott","java developer");

e1.Display();
e2.Display();
e3.Display();
}
}


OutPut:


 Employee Id 1 Details
Employee Id:1
Employee Name:Smith
Employee JOb:Java Developer

 Employee Id 2 Details
Employee Id:2
Employee Name:John
Employee JOb:Java Developer

 Employee Id 3 Details
Employee Id:3
Employee Name:Scott
Employee JOb:java developer


0 comments:

How can a variable be created.

0

How can a variable be created.
Variable is a named memory location used to store data temporarily. During program execution we can modify that data. A Variable can be created by using Datatype. As we have two types of datatypes we can create two types of variables
1. Primitive Variables:- These variables are created by using primitive datatypes.
2. Referenced Variables:- These variables are created bt using referenced datatypes.

The Difference between primitive and referenced variables is "primitive variables stores data directly, where as referenced variables stores reference of the object, not direct values".
Note: As per compiler and JVM we do not have a  separate name called "referenced variable". It means, the variables created by using referenced datatype are also considered as like normal variables only.

Below program shows creating primitive and referenced variables
// Example.java

class Example
{
int x=10;
int y=10;
}

//Test.java

class Test{
public static void main(String args[]){
//primitive variables
int p=50;
int q=m1();
//referenced variables
String s1="a";
String s2=new String("a");

Example e=new Example();

//Referenced variables are initialized with object reference that is created and returned by "new" keyword, as shown in the abobe program.

}
static int m1(){
return 60;
}
        }

Limitation of variable:
It can only store single value at a time. If we assign new value, old value is replaced with new value. It means, always it returns latest modified value.
  • If we modify primitive value previous value is replaced with new value.
  • If we modify referenced variable previous object's reference is replaced with new object's reference and now this referenced variable is referencing to this new object.

Show memory structure from the below program and also output.

class Sample{
public static void main(String args[]){
int a=50;
System.out.println("a: "+a);
a=70;
System.out.println("a: "+a);
Example e1=new Example();
System.out.println("e1: "+e1);

e1=new Example();
System.out.println("e1: "+e1);
}
           }


Output:
a: 50
a: 70
e1: Example@f4a24a
e1: Example@cac268

Conclusion:
The value of primitive variable is mathematical data based on its data type.
The value of referenced variable is its class object's reference.

0 comments:

Java Comments

0

/*
Java Comments:
A description about a basic programming element is called comment. Comments are meant for developer to understand the purpose.
Types of comments:
Java supports 3 types of comments
1. Single Line comment (//).
2. Multiline comment (/*    */).
3. Document comment (/**   */).

The statements placed inside these special characters are ignored by compiler while compling the class. It means, compiler will not generate bytecodes for those comments. So comments are not appeared in .class file.
Below program shows usage of all above 3 comments.
*/

//JavaComments.java

/**
* This program shows adding two integer numbers.
* @author: Netlojava
* @company: Netlojava.blogspot.com
* @date: 29-08-2013
* @Version: 1.0
*/

public class JavaComments
{
/*
  This method takes two integer numbers and return their addition result.
*/

           public static void main(String args[])
{
int a,b,c;   // a,b,c are integer varibules
a=5;   //a varibule 
b=6; //b varibule
c=a+b;
//addition of two numbers.
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}


Compilation:
G:\> javac JavaComments.java

Execution:
G:\> java Java Comments

Out Put:

5
6
11


0 comments:

Recent Posts