Results 1 to 10 of 10
Like Tree5Likes
  • 2 Post By DarrylBurke
  • 3 Post By JosAH

Thread: Implicit and Explicit casting

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

    Default Implicit and Explicit casting

    From the certification exam book:


    The following code snippet will print 'true'.
    short s = Short.MAX_VALUE;
    char c = s;
    System.out.println( c == Short.MAX_VALUE);

    Correct Option is : B

    A. True
    B. False

    Explanation:
    This will not compile because a short VARIABLE can NEVER be assigned to a char
    without explicit casting. A short CONSTANT can be assigned to a char only if the
    value fits into a char.
    short s = 1; byte b = s; => this will also not compile because although value is small
    enough to be held by a byte but the Right Hand Side i.e. s is a variable and not a
    constant.
    final short s = 1; byte b = s; => This is fine because s is a constant and the value fits
    into a byte.
    final short s = 200; byte b = s; => This is invalid because although s is a constant but
    the value does not fit into a byte.
    Implicit narrowing occurs only for byte, char, short, and int. Remember that it does not
    occur for long, float, or double. So, this will not compile: int i = 129L;





    I dont understand the part in bold.

    The below code compiles fine and contradicts what is said in bold. So what does the bold statement mean then?

    Java Code:
    class BreakTest{
    	public static void main(String args[])
    	{
    		float f=1.0f;
    		double d=f;
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Implicit and Explicit casting

    That's not narrowing.
    That's widening.
    A double is bigger than a float.
    Try it the other way around.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Implicit and Explicit casting

    Or try this:

    Java Code:
    float ingcloud = 5.0;
    Because by default, a floating point value ('literal') in Java is a double. The above will not compile, it will produce the error "cannot convert from double to float".


    That might actually be a source of confusion; an integer literal is by default of type int and not long (in other words: the default type is not the most wide one that it can be). A floating point literal is by default of type double and not float (in other words: it IS the widest it can be). Ex:

    Java Code:
    long island = 5;  // assigning an int to a long; no problem
    int ernationalmanofmystery = 5;  // assigning an int to an int; no problem
    
    float ingpoint = 10.0; // ERROR: assigning a double to a float
    Last edited by gimbal2; 08-21-2014 at 05:18 PM. Reason: confusing typo, confusing gap in example
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Implicit and Explicit casting

    What else?
    Java Code:
    int eresting;
    double agent;
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Implicit and Explicit casting

    Quote Originally Posted by Tolls View Post
    What else?
    Java Code:
    int eresting;
    double agent;
    :)
    Java Code:
    int riguing;
    byte me;
    Tolls and JosAH like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Implicit and Explicit casting

    Thanks Tolls

    @gimbal2: Good info. BTW why gimbal2? where is the original, I mean gimbal?

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

    Default Re: Implicit and Explicit casting

    Oh he's still around somewhere. But I forgot his password.

    (that happened on the Oracle Java forums a long time ago).
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Implicit and Explicit casting

    Java Code:
    long itude;
    float ationdevice;
    short ening;
    double indemnity;
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Implicit and Explicit casting

    Heh, that reminds me of an old IOCCC entry:

    Java Code:
    char*lie;
    
    	double time, me= !0XFACE,
    
    	not; int rested,   get, out;
    
    	main(ly, die) char ly, **die ;{
    
    	    signed char lotte,
    
    
    dear; (char)lotte--;
    
    	for(get= !me;; not){
    
    	1 -  out & out ;lie;{
    
    	char lotte, my= dear,
    
    	**let= !!me *!not+ ++die;
    
    	    (char*)(lie=
    
    
    "The gloves are OFF this time, I detest you, snot\n\0sed GEEK!");
    
    	do {not= *lie++ & 0xF00L* !me;
    
    	#define love (char*)lie -
    
    	love 1s *!(not= atoi(let
    
    	[get -me?
    
    	    (char)lotte-
    
    
    (char)lotte: my- *love -
    	
    	'I'  -  *love -  'U' -
    
    	'I'  -  (long)  - 4 - 'U' ])- !!
    	
    	(time  =out=  'a'));} while( my - dear
    
    	&& 'I'-1l  -get-  'a'); break;}}
    
    	    (char)*lie++;
    
    
    (char)*lie++, (char)*lie++; hell:0, (char)*lie;
    
    	get *out* (short)ly   -0-'R'-  get- 'a'^rested;
    
    	do {auto*eroticism,
    
    	that; puts(*( out
    
    	    - 'c'
    	
    -('P'-'S') +die+ -2 ));}while(!"you're at it");
    
    
    for (*((char*)&lotte)^=
    
    	(char)lotte; (love ly) [(char)++lotte+
    
    	!!0xBABE];){ if ('I' -lie[ 2 +(char)lotte]){ 'I'-1l ***die; }
    
    	else{ if ('I' * get *out* ('I'-1l **die[ 2 ])) *((char*)&lotte) -=
    
    	'4' - ('I'-1l); not; for(get=!
    
    
    get; !out; (char)*lie  &  0xD0- !not) return!!
    
    	(char)lotte;}
    
    
    (char)lotte;
    
    	do{ not* putchar(lie [out
    
    	*!not* !!me +(char)lotte]);
    
    	not; for(;!'a';);}while(
    
    	    love (char*)lie);{
    
    
    register this; switch( (char)lie
    
    	[(char)lotte] -1s *!out) {
    
    	char*les, get= 0xFF, my; case' ':
    
    	*((char*)&lotte) += 15; !not +(char)*lie*'s';
    
    	this +1s+ not; default: 0xF +(char*)lie;}}}
    
    	get - !out;
    
    	if (not--)
    
    	goto hell;
    
    	    exit( (char)lotte);}
    kind regards,

    Jos (the world was more fun in C)
    jim829, DarrylBurke and AlexGraal like this.
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Implicit and Explicit casting

    He he, that code is genius :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Confusion in implicit conversion
    By dojob in forum New To Java
    Replies: 5
    Last Post: 08-05-2013, 01:52 PM
  2. Implicit objects
    By JavaJunkie in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-20-2009, 07:21 AM
  3. Replies: 6
    Last Post: 02-20-2009, 11:03 AM
  4. JSP implicit objects
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 11:12 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
  •