Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Dose java have the concept of scalar context as perl?

Thread view: 
Jack Dowson - 01 May 2007 08:52 GMT
Hello Everyone:
    I'm new to java.I'm now confused by the output of the following two
examples:

       The first example:
import java.util.*;
class UtilCalender{
    public static void main(String[] args){
        Calendar c = Calendar.getInstance();
        System.out.println("current date: ");
        System.out.println("Year: " +c.get(c.YEAR));
        System.out.println("Month: " + (c.get(c.MONTH)+1));
        System.out.println("Day: " + c.get(c.DAY_OF_MONTH));
            }
        }
    And the result is:
current date:
Year: 2007
Month: 5
Day: 1

    The second example:
import java.util.*;
class UtilCalender1{
    public static void main(String[] args){
        Calendar c = Calendar.getInstance();
        System.out.println("current date: ");
        System.out.println(c.get(c.YEAR) + (c.get(c.MONTH)+1)
+c.get(c.DAY_OF_MONTH));   
            }
        }
    And the result is:
current date:
2013

    What leads to the different output?
    It can be easily interpreted in perl as different scalar context.Then
what's the reason in java?

    Any reply will greatly be appreciated!
Chris Dollin - 01 May 2007 09:21 GMT
> Hello Everyone:
>       I'm new to java.I'm now confused by the output of the following two
[quoted text clipped - 32 lines]
>
>       What leads to the different output?

`c.get()` is returning integers, which your second example adds together
before printing. There are multiple `println`s, distinguished by the
compile-time type of their argument; the one with an integer argument
prints integers in the usual way. In your first example, the `+` is
adding strings to things by converting the things to strings (someone
please write the tune for this); `+` has a somewhat horrible overloading
to allow this.

Signature

"It was the first really clever thing the King had said that day."
/Alice in Wonderland/

Hewlett-Packard Limited     Cain Road, Bracknell,                registered no:
registered office:          Berks RG12 1HN                       690597 England



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.