Results 1 to 5 of 5
Thread: Directly package name?
- 01-12-2013, 09:45 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 76
- Rep Power
- 0
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.
- 01-12-2013, 12:07 PM #2
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
Re: Directly package name?
Hint: imports.
- 01-19-2013, 10:04 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 76
- Rep Power
- 0
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
- 01-19-2013, 10:07 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 76
- Rep Power
- 0
- 01-19-2013, 10:44 PM #5
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
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
-
Eclipse package classes are not visible - deleting package completely - checkstyle
By compauer in forum EclipseReplies: 3Last Post: 03-27-2012, 09:27 AM -
Reading Directly from a URL [help]
By Lil_Aziz1 in forum NetworkingReplies: 4Last Post: 01-03-2010, 05:27 PM -
The declared package does not match the expected package
By oneforall in forum EclipseReplies: 7Last Post: 11-09-2009, 07:51 AM -
Can I call directly the *.class
By doanhanam in forum New To JavaReplies: 1Last Post: 11-16-2008, 07:24 PM -
How to draw directly on an SWT Control
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks