File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assignment doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assignment doubt" Watch "Assignment doubt" New topic
Author

Assignment doubt

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
In below program we have assigned float f=10;
that should give us compiler error . but this program compiles fine and gives output 10.
what am I missing . please let me know.


SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18837
    
  40

In below program we have assigned float f=10;
that should give us compiler error . but this program compiles fine and gives output 10.
what am I missing . please let me know.


You forgot about the implicit casts. An int will implicitedly cast to a float. Hence, it is possible to (look like) assign an int literal to a float (but it is really a cast).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Got it now
Thanks a lot
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

But, if you try to assign a *floating point value* to the float variable without using "f", it will give you the compilation error as you expected.



The output is :



Does that help?

Thanks for the post Divya!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Yogvinder Singh Ghotra
Ranch Hand

Joined: Sep 10, 2007
Posts: 38
10 is a int literal
10.0 is a double literal

Can I answer the last post by this??
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Yogvinder Singh Ghotra:
....Can I answer the last post by this??


Yes, very much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assignment doubt