aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 3 Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

3 Questions

Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

#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?


===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

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

Joined: Nov 09, 2008
Posts: 598

And why am I a ranch hand?What's that all about..Thought I was a greenhorn...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

See Saloon Titles.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi Punit ,
could you explain how comma flag works in your example?

Thanks
Preetha
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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


SCJP 6
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

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

Joined: Nov 09, 2008
Posts: 598

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

Joined: Oct 16, 2008
Posts: 952
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

Joined: Dec 22, 2008
Posts: 6
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

Joined: Nov 09, 2008
Posts: 598

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

Joined: Nov 09, 2008
Posts: 598


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

Joined: Oct 16, 2008
Posts: 952
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

Joined: Nov 09, 2008
Posts: 598

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

Joined: Oct 16, 2008
Posts: 952
Ya I am also feeling, you have got it.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032

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.


All code in my posts, unless a source is explicitly mentioned, is my own.
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
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

Joined: Aug 29, 2008
Posts: 580

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.


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
I do not know Arun
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
James, Arun is asking why float value is getting changed in the output?
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

float f=123456789123456f;
System.out.println(f);


hope this will help
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
 
subject: 3 Questions