Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

3 Questions

 
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...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
 
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
 
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 ]
 
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.
 
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:
 
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
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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
 
Dinner will be steamed monkey heads with a side of tiny ads.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic