About Question enthuware.ocajp.i.v7.2.943 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.943 :

Post by ETS User »

import java.*; //1
public abstract class InternalLogic //2
{    
float density = 20.0; //3    
public class Doer //4   
  {       
void do() //5       
{          //lot of valid code.      
  }   
  }
}
I just wanted to know whether the import statement on line 1 is correct since it has just classname?

Thanks in advance!

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.943 :

Post by admin »

Yes, the import statement is correct although it is redundant because there is no class in java package.
If you like our products and services, please help us by posting your review here.

pvijver
Posts: 1
Joined: Tue Mar 26, 2013 4:20 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.943 :

Post by pvijver »

I thought you could only have one public class in a file. Are abstract classes an exception to that rule?

If so, is the only allowed filename for this java file then 'Doer.java' ??



/// nevermind: code experimentation led me to the following: there can only be one public class in a file, but a class can have as much public INNER classes as desired.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.943 :

Post by vchhang »

Line 3: float density = 20.0;

I thought when you are assigning a constant, there is automatic casting?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.943 :

Post by admin »

vchhang wrote:Line 3: float density = 20.0;

I thought when you are assigning a constant, there is automatic casting?
But not with float, double, or long.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.943 :

Post by vchhang »

It appears that I have to read the JLS because no books have all of these information.

abhishek2204
Posts: 3
Joined: Tue Jan 13, 2015 11:11 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.943 :

Post by abhishek2204 »

Hi,

Can you please clear my doubt as to why float density = 20.0; is not acceptable by compiler??

I am completely lost here.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.943 :

Post by admin »

It is invalid because 20.0 is a double. You can't assign a double to a float variable without a cast because double is a bigger data type than float. Even though the number 20.0 can fit into a float, java language designers have disallowed assignment of double to float or long to int without an explicit cast.
If you like our products and services, please help us by posting your review here.

abhishek2204
Posts: 3
Joined: Tue Jan 13, 2015 11:11 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.943 :

Post by abhishek2204 »

I guess now i get it..the default data type for integers is double and not float and so if i have to declare a float I need to declare it with F or f at end...correct??

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.943 :

Post by admin »

abhishek2204 wrote:the default data type for integers is double and not float and so if i have to declare a float I need to declare it with F or f at end...correct??
No, default data type for integers is int. 20.0 is not an integer. 20 is an integer. 20.0 is a floating point number and it is considered a double in java. 20.0f or 20.0F is float.
If you like our products and services, please help us by posting your review here.

abhishek2204
Posts: 3
Joined: Tue Jan 13, 2015 11:11 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.943 :

Post by abhishek2204 »

Sorry I misquoted... I meant that default type for decimals is double and not float and to declare a float you will need to postfix f or F...

Post Reply

Who is online

Users browsing this forum: No registered users and 45 guests