Win a copy of Spring Boot in Practice this week in the Spring forum!

# Lam Thai

Ranch Hand
since Apr 02, 2001
Cows and Likes
Cows
0
In last 30 days
0
Total given
0
Likes
1
0
Total given
0
Given in last 30 days
0
Scavenger Hunt Ranch Hand Scavenger Hunt Greenhorn Scavenger Hunt

## Recent posts by Lam Thai

Originally posted by Greg Georges:
Can someone please explain the logic of this question, cannot seem to grasp the idea
What will be the result of attempting to compile and run the following program?
class TestClass
{
public static void main(String args[])
{
boolean b = false;
int i = 1;
do
{
i++ ;
} while (b = !b);
System.out.println( i );
}
}
The answer is that it will print 3.
Guess that (b = !b) evaluates to (false = true), so therefore we go through another iteration because the condition is true, then I suppose it becomes (true = false) and exit the loop. Is this the correct reasoning?

Hi Greg,
http://www.javaranch.com/ubb/Forum24/HTML/009781.html
Regards,
Lam
21 years ago

Originally posted by James Du:
I think in a arithmetic sense, it's imposible for a type of 64bits to distinguish more than 2^64 distinct numbers, they have just 2^64 different combinations! how does it accomplish that?

Hi James,
The position of the decimal point floats!
The answer lies in the formulea above where N is the precision that represents the mantisa and K represents the exponential component.
According to the IEEE standard, out of 64 bits, they use 11 bits to store the component, and the remanining 53 bits to store the mantisa. That is the reason why they give up the precision because with 53 bits, they can only have upto 17 digit precision (2^N, where N is 53,is equal 9007199254740992).
Now do you see why the same 64 bit can be used differently? In short, long has 64-bit precision but no exponent component. Double has only 53-bit precision, but it has 11-bit component! The exponential component is what floats the decimal point and hence helps extend the range of the double.
Now for the same 64 bits, you can even extend the range bigger by giving up a bit more precision and increasing the number of bit for the exponent component, let's say 49-bit precision and 15-bit exponent... And that how we got the extended-double number with the same 64 bit.
- Lam -

[This message has been edited by Lam Thai (edited May 17, 2001).]
21 years ago

Originally posted by James Du:
Hi,
We know that type long has 64bits, so the the amount of the distinct numbers it can hold is 2 to the power of 64.
My puzzle is: since type double has the same number of bits, why it could hold the number greater than long both in precision and in magnitude? How many numbers could it distinguish?

Hi James,
You can find the answer in JLS or IEEE 754 standard. But here is a short answer:
The finite nonzero values of any floating-point value set can all be expressed in the form s * m * Math.pow(2,e-N-1) where
s is +1 or -1, m is a positive integer less than Math.pow(2,N), and
e, the exponential component, is an integer between
Emin = -(Math.pow(2,K-1) - 2) and
Emax = (Math.pow(2,K-1) - 1), inclusive.
For float N = 24 and K = 8. Emin = -126, Emax = 127
For double N = 53 and K = 11. Emin = -1022. Emax = 1023.
So as you can see, the exponential component for a double is quite large compared to a long.

- Lam -

[This message has been edited by Lam Thai (edited May 16, 2001).]
21 years ago

Originally posted by devesh singh:
#) lass wrapper
{
public static void main(String [] str)
{
static int l; // 1
Integer I = new Integer(6);

System.out.println(I);
System.out.println(l);
}
}

what is wrong withh this code if i removw line 1 then its all OK

Hi Devesh,
int l is a local variable to main(). Local variable cannot have any access modifier. If you remove 'static' declaration from int l, you will be okay. BTW, Don't forget to initialize l.
- Lam -

[This message has been edited by Lam Thai (edited May 14, 2001).]
21 years ago

Originally posted by Shah Chunky:
Dear Lam...
Throwable is A Class & Not an Interface
So what do u think the answer would be ?
Thanks

Hi Shah,
You are definitely right! What caused me to come up with such idea that Throwable is an interface? Shame is on me.
Now I have just gone through the inheritance tree and this is what I found:
Object <---Throwable <-----Error <--- { Exceptions you should not catch }
Object <---Throwable <----- Exception <--- { exceptions you can catch }
Based on that, I would say b) Throwable. If we choose either Exception or Error, we would miss half of the pie.
- Lam -
21 years ago

Originally posted by Shah Chunky:
Dear Friends.
My Question Still remains ?
What i want to know from u guys is that what will be the answer for both the Questions ?
For Q.1) should i choose (b) i.e. Throwable
(Remember Exception is super class of all Exceptions)
Q.2) Should i choose False.
Thanks

Hi Shah,
#1. If you think All Exceptions include Exeception itself then Throwable is the choice. However since Throwable is an interface not a class; So from the spirit of the question accompanied by the keyword superclass, I would choose 'a' as my final answer.
#2. Definitely FALSE! You cannot override 'static' method.
- Lam -
[This message has been edited by Lam Thai (edited May 14, 2001).]
21 years ago
James,
Now we don't have to keep 'transient' and throw out 'static' and 'final', do we? Regarding your statement: 'static' variables can't be 'transient', I have this question for you:
I need a counter to count how many objects that have been instantiated. I also need to a fault tolerance system to take care of any unscheduled shutdown. What would be the easiest way that I can write/do in my program to find out how many objects were created before the shutdown when the system is restored?
Regarding similar statement of yours: 'final' variables can't be 'transient', again I have another question for you:
Each cellphone has an unchangeable unique ID. In order to get on the airwave legally, they need to identify themselves. What do they need to send to the authentication server?
Have a nice day,
- Lam -
21 years ago

Originally posted by jeena jose:
can some one expain to me:
we can't declare a trasient variabe inside a final or static method.why???

Inside any method, local variables have no visibility. It has nothing to do with 'transient', 'static' or 'final'. Plug this code in a program
public foo() {
transient int i = 0;
}
Now replace the keyword transient with other modifiers. What did you find? The local variable int i is really fussy, isn't it? Take care,
Lam
21 years ago

Originally posted by Hima Mangal:
hi all..
pls have a look at the following code..
What will this program print out ?
class Base{
int value = 0;
Base(){
}
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
}
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
1. 10
2. 20
3. 30
4. 40
the correct answer is 40.. how is this so.. shouldn't the method in the base class constructor call its own addValue() method??
also, if the methods are declared static, the output is 30.. aren't static methods resolved by the type or reference and not by the type of object??

Now let's walk through the execution of the code and see if that would help answer your question.
1 - In main(), Derived object is instantiated via 'new' op
2 - This in turn will invoke Derived's constructor
3 - Derived's constructor will implicitlyinvoke super() (i.e. invoking Base's constructor)
4 - Since Base is top-level class, its field will be intialized next
5 - int value will be assigned with 0
6 - call addValue() in Base's constructor (note: due to overriding - obj is Derived, this method is actually Derived's)
7 - field value goes from 0 to 20 via execution of value += 20
8 - Done with addValue() and back in Base's constructor
8 - Return from Base's constructor back to Derived's constructor
9 - call addValue() in Derived's constructor
10 - field value goes from 20 to 40 via execution of value += 20
11 - Done wirth addValue() and back in Derived's constructor
13 - Assign the created object reference to type Base named b
14 - call System.out.println(), which call getValue() first
15 -getValue() returns field value, which was set to be 40
16 - System.out.println() outputs the value fo 40
If both addValue() is declared with static access, then whichever class where addValue() is called, the method belongs to that class unless it is qualified by another name. So at step #6, Base's addValue() is called and the field value will go from 0 to 10 via value += 10; All the remaining execution steps remain and at step #10 field value will go from 10 to 30...
Hope that helps,
- Lam -
21 years ago

Originally posted by Cindy Glass:
Here is my password. Make sure that is can not be serialized. Make sure that it can not be changed.
transient final

Hello Cindy,
Don't you ever want to change your password? That is a bit dangerous, don't you agree? (Just kidding!) Have a nice day,
- Lam -
21 years ago

Originally posted by James Du:
I think static instances and final instances were implicitly transient, you needn't specify them at all.
serialization is for object, not for class, so what's the meaning of serialize the static instances of a object? how do you deserialize them? restore them to the Class object?
serializatioin is for transfering object data. final instances means instances that can not be changed, then what's the use to serialize them and deserialize them since 2 sides of the transfer both knows what's the very value of the instance?
Regards,
James

Hi James,
First, it could be due to my ignorance! but I am not sure if there is such a thing as static instance. If you can kindly give me an example of one, I would appreciate very much.
Next let's assume for now that you refer static instance to be static or class member. In that case, I don't believe static implicitly carries transient connotation since that would mean one of them is extraneous in the language. Couldn't you think a case where one would serialize the value of a static memeber? What about MY PASSWORD example given in the previous threat? Don't you think that was good?
Now let's visit the 'final' and 'transient' pair. If final implicitly means transient, then again one is extraneous in the language!
Maybe we should keep 'transient' and throw away both 'static' and 'final'. But that is not what we want, do we?

Well, what do I know?
Regards,
- Lam -

[This message has been edited by Lam Thai (edited May 14, 2001).]
21 years ago

Originally posted by Ravindra Mohan:
Hi Folks,
Lam has correcly understood the point made by Cindy..
Ravindra Mohan.
[This message has been edited by Ravindra Mohan (edited May 13, 2001).]

Ravindra,
I only know what I know. I would not claim that I understood the point that Cindy made; Doing so is exagerated, conceded and simply myopic - Importantly that also shows my ignorance.
- Lam -
[This message has been edited by Lam Thai (edited May 13, 2001).]
21 years ago

Originally posted by Ravindra Mohan:
Hi Lam,
You have correctly answered the question.
Good explanation.
Ravindra Mohan

Ravindra,
Thank for being a good cheer leader!
- Lam -
21 years ago

Originally posted by Yuki Cho:
The following code prints out 3. I thought it will go in an infinite loop since it is while (true), but it is not. Please explain... thanx!!!
-----------
What will be the result of attempting to compile and run the following program?
class TestClass
{
public static void main(String args[])
{
boolean b = false;
int i = 1;
do
{
i++ ;
} while (b = !b);
System.out.println( i );
}
}

Hi Yuki,
Pay attention to the while statement:
while (b = !b)
The first time, when b = false, read that statement as
while (b = !false) or while (b = true) // b is now assigned a value 'true'
Next time around, b is true, read that statement to be
while (b = !true) or while (b = false) // b is now assigned a value 'false' -> hence your loop is broken.
Take care,
- Lam -
21 years ago

Originally posted by Yuki Cho:
hi there,
can you please explain to me why the following code doesn't compile? i thought class B is able to access class A since B extends A even though they are in different package.
--------
//in file A.java
package p1;
public class A
{
protected int i =10;
public int getI() {return i;}
}
//in file B.java
package p2;
import p1.*;
public class B extends p1.A
{
public void process(A a)
{
a.i = a.i*2;
}
public static void main(String args[])
{
A a = new B();
B b = new B();
b.process(a);
System.out.println(a.getI());
}
}

thanks,
yuki

Hello Yuki,
The above code violates the following JLS rule:

Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:
If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.