OCJP(Oracle Certified Java Program)

Totel:29 Click: 1 2 3 4 5 6
scjp1.5 Interview Questions And Answers

Page 1

When we passed to the primitive variable is passed to a method , we can see in this code : 

class ReferenceTest {
public static void main (String [] args) {
int a = 1;
ReferenceTest rt = new ReferenceTest();
System.out.println("Before modify() a = " + a);
System.out.println("After modify() a = " + a);
void modify(int number) {
number = number + 1;
System.out.println("number = " + number);

In this folowing code , we can see thet the variable a is passed to a method called modify(),
which increments the variable by 1.and now we know that whats the output of the givn code :
C:Java ProjectsReference>java ReferenceTest
Before modify() a = 1
number = 2
After modify() a = 1

if java passes objects by passing the reference variable instead its means the java uses pass-by-reference for objects, but its not a exactly.Java is actually pass-by-value for all variables running within a single VM. Pass-by-value means pass-by-variable-value. And that means, pass-by-copy-of-the-variable !

We are always passing a copy of the bits in the variable. So for a primitive variable, we are passing a copy of the bits representing the value.The bottom line on pass-by-value: the called method can't change the caller's
variable, although for object reference  variables, the called method can change the object the variable referred to.

For example, in the following code :

void bar() {
Foo f = new Foo();
void doStuff(Foo g) {
g = new Foo();

When we pass an object variable in to a method , We must know that we're passing the object reference, and not the actual object itself.

We must remember that we are not even passing the actual reference variable, but rather a copy of the reference variable.

The copy variable means we get the copy of the bits in that variable, so when we pass a reference variable,

We can also say like this : both the caller and the called method will now have identical copies of the reference, and thus both will refer to the same exact (not a copy) object on the heap.

For this example, we will use the Dimension class from the java.awt package:

1. import java.awt.Dimension;
2. class ReferenceTest {
3. public static void main (String [] args) {
4. Dimension d = new Dimension(5,10);
5. ReferenceTest rt = new ReferenceTest();
6. System.out.println("Before modify() d.height = " + d.height);
7. rt.modify(d);
8. System.out.println("After modify() d.height = " + d.height);
9. }
10. void modify(Dimension dim) {
11. dim.height = dim.height + 1;
12. System.out.println("dim = " + dim.height);
13. }
14. }

When we run this class, we can see that the modify() method was indeed able to modify the original (and only) Dimension object created on line 4.

C:Java ProjectsReference>java ReferenceTest
Before modify() d.height = 10
dim = 11
After modify() d.height = 11

Its mainly use for the determine the effect upon objects and primitive values of passing variables into methods and performing assignments or other modifying operations in that method.That method basically use for the declared to take primitives and/or object references. Here we also know that by this method that how to caller's variable can be affected by the called method.

When passed into methods, is huge and important, Its the main imp diff of object reference and primitive variables.

The bitwise operator (& and |)is also use for the logical operators and expressions also .But because they aren't the short-circuit operators, they evaluate both sides of the expression,

The short-circuit operators (&& and ||) can be used only in logical (not bitwise) expressions. The bitwise operators (& and |) can be used in both logical and bitwise expressions, but are rarely used in logical expressions because they are not efficient.

Goto Page:

1 2 3 4 5 6