Results 1 to 15 of 15
Like Tree2Likes

Thread: Casting

  1. #1
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Casting

    When does an internal cast actually happen? I am aware that compound assignment operator do an internal cast. Does it happen in Assignment 1?Assignment 2?Assignment 3?Assignment 4?

    Java Code:
    public class Parser{
    	public static void main( String[] args){
    		byte b=1;
    		short s=1;
    		int i=1;
    		s=b;//Assignment 1
    		s<<=b;//Assignment 2
    		b<<=s;//Assignment 3
    		s +=i;//Assignment 4
    	}
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: Casting

    I'm having a hard time finding a valid language definition of "internal cast". The only terms I know are "implicit cast" and "explicit cast".
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Casting

    Quote Originally Posted by gimbal2 View Post
    I'm having a hard time finding a valid language definition of "internal cast". The only terms I know are "implicit cast" and "explicit cast".
    Don't forget 'widening' cast and 'narrowing cast'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: Casting

    Well I did forget those, only I know them as upcasting and downcasting.

    Edit: thinking about it, I guess for primitive types "widening" and "narrowing" are a better fit.
    Last edited by gimbal2; 11-03-2014 at 11:58 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Casting

    I guess the author meant "implicit cast"

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Casting

    Quote Originally Posted by jmohandos304 View Post
    I guess the author meant "implicit cast"
    All the casts in the assignments in your original post are implicit casts (i.e. the compiler generates them for you where ever allowed).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,916
    Rep Power
    5

    Default Re: Casting

    You may also want to look at the compiled class files to see the generated byte code. That could provide some insight as to how the compiler handled the different types. You can do it with javap -c somefile.class. Eclipse has a built in class file viewer for this purpose.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: Casting

    Insert coin into brain : why is assignment 4 allowed again as it might lead to data truncation? If you translate it to this:

    Java Code:
    s = s + i;
    you get a proper compilation failure.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,916
    Rep Power
    5

    Default Re: Casting

    The JLS says: Chapter*15.*Expressions

    A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: Casting

    So it actually performs an invisible explicit cast.

    Well I guess that makes sense given that the OP= expressions basically exist to not have to spend any more energy than you absolutely have to.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Casting

    A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

    So an implicit cast occurs in the case of compound assignment even if there is a chance of loss of precision. Am I right?

    Even i checked what gimbal2 said. s+=i is fine to compiler but not its equivalent s=s+i

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Casting

    Quote Originally Posted by jmohandos304 View Post
    A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

    So an implicit cast occurs in the case of compound assignment even if there is a chance of loss of precision. Am I right?
    No you're not; if there is a loss of precision, as in 'b+= i' where b is a byte and i is an int, you need to cast i explicitly to convince the compiler that you know what you're doing. The compiler never drops bits behind your back.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Casting

    The following compiles fine:

    Java Code:
    public class Parser{
    	public static void main( String[] args){
    		byte b=1;
    		int i=1;
    		b+=i;
    	}
    }
    This does NOT compile:
    Java Code:
    public class Parser{
    	public static void main( String[] args){
    		byte b=1;
    		int i=1;
    		b=b+i;
    	}
    }

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Casting

    Whoops, you're right: the compiler generates the cast implicitly; I thought that the compiler were more prudent ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: Casting

    I think it still is; these operators basically exist to automate a couple of tasks and one of them is to save you from having to do an explicit cast. Didn't realize that this was the case and now that I know, I like it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. casting an int
    By droidus in forum New To Java
    Replies: 1
    Last Post: 09-07-2011, 07:38 PM
  2. Casting
    By zzpprk in forum Advanced Java
    Replies: 13
    Last Post: 08-13-2009, 08:59 PM
  3. What does casting mean?
    By sev51 in forum New To Java
    Replies: 3
    Last Post: 01-27-2009, 05:31 PM
  4. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 12:07 AM
  5. Casting
    By leebee in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 01:24 PM

Posting Permissions

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