About Question enthuware.ocajp.i.v7.2.943 :
Moderator: admin
- 
				ETS User
 
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!
- 
				admin
 - Site Admin
 - Posts: 10443
 - 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.
			
			
									
									
						- 
				pvijver
 - 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.
- 
				vchhang
 - 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?
- 
				admin
 - Site Admin
 - Posts: 10443
 - 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.
- 
				vchhang
 - 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.
			
			
									
									
						- 
				abhishek2204
 - 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.
- 
				admin
 - Site Admin
 - Posts: 10443
 - 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.
			
			
									
									
						- 
				abhishek2204
 - 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??
			
			
									
									
						- 
				admin
 - Site Admin
 - Posts: 10443
 - 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??
- 
				abhishek2204
 - 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 37 guests