Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More more code concepts!

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)Is the following declaration(as a member variable) legal??
static final transient int maxElements = 100;

2)What is the result of
System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0));
Should it be False?? The answer is True, which i cannot understand??
and also what is the output for
3) System.out.println(-1 * Double.NEGATIVE_INFINITY);
Thanx in advance!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Qstn I:
The answer is false. But I'm not sure why ?? I tried compiling but I get a compile error. But if I remove the transient, then it compiles just fine. So, others.. please lend me a hand
Qstn II:
Refer to <html> http://www.javaranch.com/ubb/Forum24/HTML/001428.html </html> . Same discussion.
Qstn III:
It's just Infinity. ( -ve * -ve = +ve). But if you change the statement as System.out.println(1 * Double.NEGATIVE_INFINITY); then it gives a -ve Infinity.
Prabhu.

[This message has been edited by Prabhu (edited May 17, 2000).]
 
bongadi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhu,
It compiles for me.
could you post the compiler error you are getting.
Also please post the platform and JDK version you are using.
Originally posted by Prabhu:
Qstn I:
The answer is false. But I'm not sure why ?? I tried compiling but I get a compile error. But if I remove the transient, then it compiles just fine. So, others.. please lend me a hand
Qstn II:
Refer to <html> http://www.javaranch.com/ubb/Forum24/HTML/001428.html </html> . Same discussion.
Qstn III:
It's just Infinity. ( -ve * -ve = +ve). But if you change the statement as System.out.println(1 * Double.NEGATIVE_INFINITY); then it gives a -ve Infinity.
Prabhu.

[This message has been edited by Prabhu (edited May 17, 2000).]

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using win98 and jdk1.3 and the 1st is not compiling.
with 2 the answer is true but i think it should be false
and with 3 i too had thought positive infinity.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static final transient int maxElements = 100;
is perfectly legal. Note that there are no mutually exclusive or contradicting modifiers here. Each modifier serves a purpose.
I can go over and explain in detail if you need more clarification. Let me know
Ajith
 
Suresh
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
faizaharis,
Transient variables cannot be static or final.
Let's see the definition of transient first.

Transient variables are used for securing the value of an instance variable (which is to be maintained secure, eg. Access code). If an object is serialized, the transient variables are not written to the output stream.

Since, the transient concept itself pertains to an object, it can't be static.
But, I am not sure why it can't be final. Somebody please!!
Regards,
Suresh.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
though the answer is vague i propose this
class x{
static final transient int maxElements = 100;
}
if x is in an object then i think that if this object is to be serialized and deserialized then the variable maxElements state is not saved leading to problems after deserialization since this variable is a class variable. hope there is some logic and others may throw more light.

[This message has been edited by rahul_mkar (edited May 18, 2000).]
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh,
I don't agree with you. Transient simply means, "dont serialize me". I don't see any point why it can't be static or final.
static variables can still be accessed through an object instance. I don't know if any special rules apply for serializing static entities, but I don't believe being transient has anything to do with being static.
I am using JDK 1.2.2 and it compiles fine for me
Ajith
 
Suresh
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Sorry, a transient variable can be declared static final. (But, RHE says a transient variable CANNOT be declared static final). Just now I tried a program and it works!!
Thanks Ajith.
But, still I don't see any use of a transient variable being static.
Adding to rahul's question, will a static variable be serialized to an ObjectOutputStream?
Thanks and Regards,
Suresh.
 
Tom P
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static variables are not serialized.
http://java.sun.com/docs/books/tutorial/javabeans/persistence/index.html
I think it may have been true that "transient", "final", and "static" were mutually exclusive in Java 1.0.x. At that time "transient" wasn't defined. I have a document from Java 1.0.x that includes the following: "It is a compile-time error if a transient variable is also declared final or static."
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a shot at Question 2:
I think the reason is how they implement the numbering system.
In binary usually the left most bit indicate the negative,
but the way they calculate the negative number is backword.
!!!WARNING!!! my examples are in 8 bytes numbering only,
so the real Java number will be different (I just don't want
to type in long binaries)
For example:
1 is 00000001
2 is 00000010
BUT
-1 is not 10000001, instead it is 11111111
-2 is not 10000010, instead it is 11111110
And for 0 it will always be 00000000, doesn't matter positive
or negative.
and for 1000000 in actuality will be -128.
That's why if you notice in Java the ranges for:
Byte(8 bit): -2^7 ... (2^7)-1
Short(16 bit): -2^15 ... (2^15)-1
Int(32 bit): -2^31 ... (2^31)-1
So there is always 1 extra negative numbers than the positive
because of that.
I am sorry if it's kinda confusing, it's more of the lower
level implementation stuff.
 
gunjan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the errata for the RHE. There are lots of modifications mentioned for the book. Every one using RHE should get the errata. They modify the statement transient variables cannot be static and final.
Remember transient variables can be static and final.
Only thing that is important from serialization point of view is that transient and static variables are not serialized.
Regards
Gunjan
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wirianto,
I didnt understand how your reply is related to the 2nd question
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where can i get the errata?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
faizahari,
Basically in binary presentation either it's -0.0 or 0.0 or +0.0
it's all 0000000, so when you compare them they are always equal.
Sorry for the long winded explanation.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
thanks tom u r URL was great. i found the answer here and for everybody i put it down here
Controlling Serialization
You can control the level of serialization that your Beans undergo:
Automatic: implement Serializable. Everything gets serialized by the Java serialization software.
Selectively exclude fields you do not want serialized by marking with the transient (or static) modifier.
Writing Beans to a specific file format: implement Externalizable, and its two methods.

[This message has been edited by rahul_mkar (edited May 21, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic