Results 1 to 5 of 5
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Directly package name?

    Hi,

    In the following code:

    public String sort(String s){

    char[] content = s.toCharArray();
    java.util.Arrays.sort(content);
    return new String(content);

    }

    Is there any way to write java.util.Arrays.sort(content in a different manner?

    They have written the package name only directly. Looks weird, huh?

    Please clarify.

  2. #2
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Directly package name?

    Hint: imports.

  3. #3
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: Directly package name?

    Thanks!

    One more question:

    Why the return type is like this :"return new String(content);"


    Why they have mentioned "new" . Can anyone please explain the whole statement viz "return new String(content);" ?

    Thanks

  4. #4
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: Directly package name?

    Also tell me the other way to do the same.
    Quote Originally Posted by Jack_Tauson_Sr View Post
    Thanks!

    One more question:

    Why the return type is like this :"return new String(content);"


    Why they have mentioned "new" . Can anyone please explain the whole statement viz "return new String(content);" ?

    Thanks

  5. #5
    huntack is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: Directly package name?

    Addressing a couple of things here:

    First of all, imports are a great thing! However, you'll have a hard time to tell the compiler which sort method you want to use, as the one that is (or will be) executing is also called 'sort'.
    Second, the method declaration says that you will be returning a String object. Since content is an array of chars, you have to convert them to a String in order to return it (or else the compiler will complain).

    Basically, "new String(content)" means that "Hey, I'm creating an object! Its type will be String with 'content' parameter!". When creating a String object, one of its' constructors accept a char array, which you pass along. To put it in plain English, its like "here's a String object that I just made from a char array".

Similar Threads

  1. Replies: 3
    Last Post: 03-27-2012, 09:27 AM
  2. Reading Directly from a URL [help]
    By Lil_Aziz1 in forum Networking
    Replies: 4
    Last Post: 01-03-2010, 05:27 PM
  3. Replies: 7
    Last Post: 11-09-2009, 07:51 AM
  4. Can I call directly the *.class
    By doanhanam in forum New To Java
    Replies: 1
    Last Post: 11-16-2008, 07:24 PM
  5. How to draw directly on an SWT Control
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 07:51 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
  •