Dose java have the concept of scalar context as perl?

Discussion in 'Java' started by Jack Dowson, May 1, 2007.

  1. Jack Dowson

    Jack Dowson Guest

    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!
     
    Jack Dowson, May 1, 2007
    #1
    1. Advertising

  2. Jack Dowson

    Chris Dollin Guest

    Jack Dowson wrote:

    > 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?


    `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.

    --
    "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
     
    Chris Dollin, May 1, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    327
  2. Clint Olsen
    Replies:
    6
    Views:
    407
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  3. dutone
    Replies:
    8
    Views:
    119
    dutone
    Jul 2, 2004
  4. Stuart Kendrick

    how to force scalar context

    Stuart Kendrick, Jul 12, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    188
    Stuart Kendrick
    Jul 13, 2004
  5. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    193
    Arndt Jonasson
    Jan 27, 2005
Loading...

Share This Page