keith barron

Greenhorn
+ Follow
since Apr 04, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by keith barron

I could not figure it out so i changed the code and now i understand what is happening.
i changed to the code to look like this:
public class Test
{
public static void main(String[] args)
{
double d = 1.0/0;
int i = (int)d;
System.out.println(i); // prints 2147483647
}
}
I don't know why "2147483647" is the result, but i do know that if you cast this large a number into a byte field which only holds 8 bits, then the first bit is not equal to zero. Any primitive where the first bit is not equal to zero is a negative number.
Maybe somebody can explain why "2147483647" is the result?
When you say (b = true) here is what happens:
1) "b" is assigned "true".
2) "b" is evaluated. it is now equal true
Conversly, if you say (b = false), "b" would first be assigned "false" and would evaluate to false.
i added some print statements to the method change_i like this:
public static void change_i(String i[])
{
String j[] = {"Method"};
i = j;
System.out.println("i.toString() = " + i.toString() );
System.out.println("j.toString() = " + j.toString() );
System.out.println("method: j[0] = " + j[0]);
System.out.println("method: i[0] = " + i[0]);

}
here is the result:
i.toString() = [Ljava.lang.String;@7e74a5e3
j.toString() = [Ljava.lang.String;@7e74a5e3
method: j[0] = Method
method: i[0] = Method
class: i[0] = hi original
so based on these results here is what i see going on:
- method "change_i" gets a copy of the reference that is contained in "i"
- "i" is then assigned the reference to j, but only in the method "change_i".
- the original reference that "i" contains in "main" is unchanged

[This message has been edited by keith barron (edited April 06, 2001).]
[This message has been edited by keith barron (edited April 06, 2001).]
thank you for your clear explanation Manfred!
the example you referred to different in that the result does not change when the access modifiers for Parent::hello from private to public. that is really my question. i would like if you would compile and run the code, make the change to the access modifier in Superclass::methodB and see the result change.
why does the result change when the access modifier for Superclass::methodB changes from private to public?
public class Superclass {
public static void main(String[] args) {
System.out.println(new Subclass().methodA());
}
Superclass() {
System.out.println("SuperClass Constructor Executed");
}

private int methodB() {
System.out.println("methodB in Superclass");
return 9;
}

int methodA() {
System.out.println("methodA in Superclass");
return methodB();
}
}

class Subclass extends Superclass {
Subclass() {
System.out.println("SubClass Constructor Executed");
}

public int methodB() {
System.out.println("methodB in Subclass");
return 1;
}
}