Page 1 of 1
About Question enthuware.ocajp.i.v7.2.943 :
Posted: Thu Dec 13, 2012 9:39 am
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!
Re: About Question enthuware.ocajp.i.v7.2.943 :
Posted: Thu Dec 13, 2012 10:08 am
by admin
Yes, the import statement is correct although it is redundant because there is no class in java package.
Re: About Question enthuware.ocajp.i.v7.2.943 :
Posted: Tue Mar 26, 2013 4:23 am
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.
Re: About Question enthuware.ocajp.i.v7.2.943 :
Posted: Mon Jun 02, 2014 8:51 am
by vchhang
Line 3: float density = 20.0;
I thought when you are assigning a constant, there is automatic casting?
Re: About Question enthuware.ocajp.i.v7.2.943 :
Posted: Mon Jun 02, 2014 8:08 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.943 :
Posted: Tue Jun 03, 2014 7:52 am
by vchhang
It appears that I have to read the JLS because no books have all of these information.
Re: About Question enthuware.ocajp.i.v7.2.943 :
Posted: Tue Jan 13, 2015 11:14 am
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.
Re: About Question enthuware.ocajp.i.v7.2.943 :
Posted: Tue Jan 13, 2015 8:30 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.943 :
Posted: Tue Jan 13, 2015 9:47 pm
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??
Re: About Question enthuware.ocajp.i.v7.2.943 :
Posted: Tue Jan 13, 2015 10:00 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.943 :
Posted: Tue Jan 13, 2015 10:19 pm
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...