Results 1 to 4 of 4
  1. #1
    Jomar808 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Question Java's Defualt Literal Types (Whole number literals defaults to int?)

    Hello guys, its my first Post here, just want to ask

    is java whole number literals defaults to int?

    check these codes

    code 1:
    Java Code:
    1
    byte b = 10;
    code 1 works

    code 2:

    Java Code:
    1
    2
    3
    4
    5
    6
    void someMethod(byte a,byte b)
    System.out.println(a+b);

    then I use it

    someMethod(10,5);
    code 2 doesn't work, it yields an error

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method sum(byte, byte) in the type Test is not applicable for the arguments (int, int)

    now these are my questions

    1. does java whole number literals defaults to int? (I think so, basing on the code)
    2. if question no. 1 is true, then why code 1 worked? it shouldn't right, coz its a downward cast (from int to byte).
    3. what really happens under the hood?

    by the way, the jdk version I used is 1.7.0

    Thank you guys in advance

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,447
    Rep Power
    25

    Default Re: Java's Defualt Literal Types (Whole number literals defaults to int?)

    You'll probably find your answers (and a lot more) here:
    The Java Language Specification, Third Edition - TOC

  3. #3
    Jomar808 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Java's Defualt Literal Types (Whole number literals defaults to int?)

    Thanks a lot Norm

    Yep confirmed, java traits whole number literals as Integer, now my question is about the code above,

    what happened to code1? why java did not yeild an error, when in code 1 is very clear its an implicit downward cast (assigning int to byte)
    but on code 2, it did act as expected

    what happened under the hood?

    hope you can help guys, thanks a again Norm

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Java's Defualt Literal Types (Whole number literals defaults to int?)

    Don't double post. Please go through the Forum rules.

    Also, please don't post any beginner questions in the Advanced Java section.

    Your other thread has been removed.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. variable number of fields with different types
    By Sir G. in forum Advanced Java
    Replies: 11
    Last Post: 03-29-2010, 12:40 AM
  2. Replies: 3
    Last Post: 03-26-2010, 07:09 AM
  3. I can only print string literals
    By leeavital in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 01:20 AM
  4. Content assist defaults
    By tenmiles in forum Eclipse
    Replies: 0
    Last Post: 03-24-2009, 06:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •