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

3 Questions

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
#1 Please confirm:If I have a serializable class and a serializable superclass,then during deserialization the superclass's constructor will NOT run and inherited variables will retain their correct state.

#2 Please illustrate how the comma Flag works with the printf()/format()
methods.

#3 In what context can I use the Scanner class's useDelimiter method?
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Oh thanks for the prompt reply.
So the comma flag seperates ints into 100thousands,thousands,hundreds and for floats it formats the number into a (platform?)specific precision?
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
And why am I a ranch hand?What's that all about..Thought I was a greenhorn...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi Punit ,
could you explain how comma flag works in your example?

Thanks
Preetha
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Report post to moderator
This is very good question , I never thought that way but I got the reason for this.

"," flag is inserted by scanning the integer part of the string from least significant to most significant digits and inserting a comma at intervals defined by the locale's grouping size.

By default this grouping size is 3.
so if you print:

System.out.printf("%,f",123456789123456f);
output: 123,456,788,103,168.000000

System.out.printf("%,d",123456789);
output: 123,456,789
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
OK I don't understand..was the number:123456789 rounded off?(The text is very small on my screen but it looks like you maybe prefixed the number with a dot?As in 0.123456789 and if so why didn't it format the float to 6 decimal points like it did with your earlier examples?

I have one last question:When is it legal to perform a downcast?
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Oh I wasn't reading properly it did..
In any case could you please explain when downcasting is legal to me??
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Report post to moderator
When is it legal to perform a downcast?


When you superclass reference contains object of subclass then.


[ December 23, 2008: Message edited by: punit singh ]
 
Dheeraj Bansal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Report post to moderator
Casting is possible if both the types falls in a hierarchy
so one type of casting is done by JVM which is called implicit casting
in which there is no loss of precision whereas other one has to be done by
us explicitly which can cause lose of precision. But in both of the above mentioned cases they shlould follow "is-a" relationship. ie one has to be
super class of other.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Yes so implicit casting would be upcasting..
because a sub-class IS-A instance of the superclass.
But downcasting would be explicit..how do I know when this legal?

I think I need to find an example...maybe I'm not confused about downcasting..It might be reference variable casting that I'm confused about.Let me go find the source of that bingo-bango-bongo question
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator

Which, inserted at "//insert code", will NOT compile?(Choose all that apply.)

A.Bongo b4=b2
B.Bongo b5=(Bongo)b2
C.Bango b6=b3
D.Bango b7=(Bango)b3
E.Bingo b8=b3
F.Bingo b9=(Bingo)b3
G.Bango b10=(Bongo)b3

Answer: A and E will not compile.

So I read that at compile time the reference types are checked so that means that in A:
At compile time:The references Bongo b4 and Bango b2 must pass the IS-A test?And the object's type is disregarded completely until runtime?
ie. It will only compile if Bango b4 reference variable is of the type Bongo or subclass of Bongo?

And why is it that G will give an error at runtime?I understand that it's reference types match so there will be no compile-time error...but why is it that a Bingo object cannot be upcast to a Bongo object when Bingo IS-A Bongo?
[ December 23, 2008: Message edited by: Duran Harris ]
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Report post to moderator
ie. It will only compile if Bango b4 reference variable is of the type Bongo or subclass of Bongo?


This line is misleading Duran, elaborate more.

So I read that at compile time the reference types are checked so that means that in A:
At compile time:The references Bongo b4 and Bango b2 must pass the IS-A test?And the object's type is disregarded completely until runtime?


This is true. And also Parent type reference could be assigned child type reference not vice versa.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
ie. It will only compile if Bango b4 reference variable is of the type Bongo or subclass of Bongo?



This line is misleading Duran, elaborate more.


I mean if Bongo is a parent and Bango is a child.

OK then I think I've got it.
[ December 23, 2008: Message edited by: Duran Harris ]
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Report post to moderator
Ya I am also feeling, you have got it.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Report post to moderator

And why is it that G will give an error at runtime?I understand that it's reference types match so there will be no compile-time error...but why is it that a Bingo object cannot be upcast to a Bongo object when Bingo IS-A Bongo?

Duncan, G doesn't give an error at runtime. You are correct in your explanation as to why the statement runs with no problem.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi Punit,

i am really confused with this output,

System.out.printf("%,f",123456789123456f);
output: 123,456,788,103,168.000000
could you explain me the output?

Thanks
preetha :roll:
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Report post to moderator
Originally posted by punit singh:


"," flag is inserted by scanning the integer part of the string from least significant to most significant digits and inserting a comma at intervals defined by the locale's grouping size.

By default this grouping size is 3.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Report post to moderator
I do not know Arun
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Report post to moderator
James, Arun is asking why float value is getting changed in the output?
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Report post to moderator
float f=123456789123456f;
System.out.println(f);


hope this will help
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
The value is getting changed as the value cannot fit in the size of float. Float values are stored in exponential form. So if you store a too big value into them, the value will get truncated you can say. The answer would be in the IEEE standards but I have no plans of reading that
 
    Bookmark Topic Watch Topic
  • New Topic