Results 1 to 8 of 8
  1. #1
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default internet adress classes

    Hi!
    I just wanted to send an e-mail, but was confused with 2 things:
    what is the difference between classes
    Java Code:
    InetAdress
    and
    Java Code:
    InternetAdress
    .
    Why does addRecipients method not applicable for member
    Java Code:
    InetAdress
    . InetAdress stores the e-mail sending arguments, why its not applicable for ?

    And why its cause exception , when i compiled:
    Java Code:
        private ArrayList<InternetAddress> recepients = new ArrayList<InternetAddress>();
    message.addRecipients(Message.RecipientType.TO, (InternetAddress[]) this.recepients.toArray());
    exception:
    Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljavax.mail.internet.InternetAddress;
    Last edited by voipp; 10-18-2013 at 09:46 AM.

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

    Default Re: internet adress classes

    You want to use the other toArray() method:
    Java Code:
    recepients.toArray(new InternetAddress[recepients.size()]);
    (Note the above may have typos).
    You can also then remove the cast.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: internet adress classes

    Quote Originally Posted by Tolls View Post
    You want to use the other toArray() method:
    Java Code:
    recepients.toArray(new InternetAddress[recepients.size()]);
    (Note the above may have typos).
    You can also then remove the cast.
    What do you mean The other toArray(), what is the difference

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

    Default Re: internet adress classes

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: internet adress classes

    Im new to Java, so i dont understand why i should write
    Java Code:
    recepients.toArray(new InternetAddress[recepients.size()]);
    instead of
    Java Code:
    (InternetAddress[]) this.recepients.toArray();
    In both cases we receive arrays of InternetAddress

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

    Default Re: internet adress classes

    No you don't.
    In the second case you receive an Object[] (see the API I linked to above). The compiler does not know that those Objects are InternetAddress objects.
    In the second case the compiler knows that it is an InternetAddress[] back, because that is what you've supplied to the toArray() method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: internet adress classes

    Quote Originally Posted by Tolls View Post
    No you don't.
    In the second case you receive an Object[] (see the API I linked to above). The compiler does not know that those Objects are InternetAddress objects.
    In the second case the compiler knows that it is an InternetAddress[] back, because that is what you've supplied to the toArray() method.
    So in Java i can not cast arrays to arrays ?

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

    Default Re: internet adress classes

    In the same way that you can't force a String to be an Integer by casting it - they're simply not compatible object types. You don't CAST arrays, you HAVE a type that is 'array of Object'. That each index of that array of Object then actually contains references to InetAddress objects is completely beside the point.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Pinging an IP adress that is taken from a TextField.
    By MR bruto in forum New To Java
    Replies: 5
    Last Post: 07-05-2013, 11:36 AM
  2. Classes Help!!! two classes with object question.
    By stuckonjava in forum New To Java
    Replies: 16
    Last Post: 02-10-2012, 01:39 AM
  3. One row help with Locking Java to MAC Adress
    By Darkbound in forum New To Java
    Replies: 1
    Last Post: 04-28-2011, 07:42 PM
  4. Get Ip adress?
    By Godsent in forum Networking
    Replies: 2
    Last Post: 07-11-2009, 02:15 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
  •