About Question enthuware.ocajp.i.v7.2.943 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.943 :
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!
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!
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.943 :
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.
-
- Posts: 1
- Joined: Tue Mar 26, 2013 4:20 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.943 :
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.
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.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.943 :
Line 3: float density = 20.0;
I thought when you are assigning a constant, there is automatic casting?
I thought when you are assigning a constant, there is automatic casting?
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.943 :
But not with float, double, or long.vchhang wrote:Line 3: float density = 20.0;
I thought when you are assigning a constant, there is automatic casting?
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.943 :
It appears that I have to read the JLS because no books have all of these information.
-
- Posts: 3
- Joined: Tue Jan 13, 2015 11:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.943 :
Hi,
Can you please clear my doubt as to why float density = 20.0; is not acceptable by compiler??
I am completely lost here.
Can you please clear my doubt as to why float density = 20.0; is not acceptable by compiler??
I am completely lost here.
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.943 :
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.
-
- Posts: 3
- Joined: Tue Jan 13, 2015 11:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.943 :
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??
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.943 :
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.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??
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Tue Jan 13, 2015 11:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.943 :
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...
Who is online
Users browsing this forum: No registered users and 41 guests