Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Why isn't new used in the following.

Thread view: 
grocery_stocker - 13 Jul 2006 03:10 GMT
In the following:

public class toupper {

   private static void upper(){

    String a = "chad";
    System.out.println("Upper Case is:" + a.toUpperCase());

   }

   public static void main (String[] args) {
    upper();
   }
}

Why isn't the line
String a = new String();

Necessary? I thought this line might be omitted because for whatever
reasons I was thinking toUpperCase() was a static method. It isn't.
Because when I change the line:

System.out.println("Upper Case is:" + a.toUpperCase());

to

System.out.println("Upper Case is:" + String.toUpperCase());

I get the following:

$javac toupper.java
toupper.java:6: non-static method toUpperCase() cannot be referenced
from a static context
       System.out.println("Upper Case is:" + String.toUpperCase());
                                                   ^
1 error
John W. Kennedy - 13 Jul 2006 03:32 GMT
> In the following:
>
[quoted text clipped - 16 lines]
>
> Necessary?

Because you don't need to make a new String; you want to use the
existing string "chad".

Signature

John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
  -- Charles Williams.  "Taliessin through Logres: Prelude"

Stefan Ram - 13 Jul 2006 03:37 GMT
>Why isn't the line
>String a = new String();
>Necessary?

 The value of a string literal already is a reference to a
 string object that represents the same text as the text
 represented that is represented by the string literal.

>I thought this line might be omitted because for whatever
>reasons I was thinking toUpperCase() was a static method.

 I does not have to do with this property directly.

>I get the following:
>toupper.java:6: non-static method toUpperCase() cannot be referenced

 You also could have looked it up in the documentation:

http://download.java.net/jdk6/docs/api/java/lang/String.html
Stefan Ram - 13 Jul 2006 03:38 GMT
>Why isn't the line
>String a = new String();
>Necessary?

 The value of a string literal already is a reference to a
 string object that represents the same text as the text that
 is represented by the string literal.

>I thought this line might be omitted because for whatever
>reasons I was thinking toUpperCase() was a static method.

 It does not have to do with this property directly.

>I get the following:
>toupper.java:6: non-static method toUpperCase() cannot be referenced

 You also could have looked it up in the documentation:

http://download.java.net/jdk6/docs/api/java/lang/String.html
saimatam at yahoo dot com - 13 Jul 2006 03:46 GMT
String is a basic data type in Java language.
Accordingly, the following statement initializes
variable 'a' with the String "fred".
    String a = "fred";

Look at the following code fragment:
 Line 1:  String a = new String();
 Line 2:  a = "fred";

The String variable 'a' has been initialized
to a reference of empty String. In the next
line it is re-initialized to a reference of
a String 'fred'. So line # 1 is not necessary.

String is the class name and 'a' is an instance.

So String.toUppercase() refers to a static method
'toUpperCase' in the class 'String'.

If a method is not static it would need to know
'on which instance' it is supposed to act! So
if the method is not a static then it would need
to know its instance - in this case 'a' hence
a.toUpperCase();

Hope this helps.

-- Sai Matam.
Oliver Wong - 13 Jul 2006 17:18 GMT
[...]
> String a = "chad";
[...]
> Why isn't the line
> String a = new String();
>
> Necessary?

   It's syntactic sugar. String is an object type, but there's shortcuts in
the language to treating them like pseudo-primitives. It's an inconsistency
in the Java language for the sake of developer convenience.

   - Oliver
Luc The Perverse - 13 Jul 2006 19:57 GMT
> [...]
>> String a = "chad";
[quoted text clipped - 7 lines]
> in the language to treating them like pseudo-primitives. It's an
> inconsistency in the Java language for the sake of developer convenience.

Personally - I do actually find it very convenient.

Signature

LTP

for( Base i : allYourBase)
       i.AreBelongToUs();

jmcgill - 13 Jul 2006 21:07 GMT
>>> String a = "chad";
>> [...]
>>> String a = new String();
> Personally - I do actually find it very convenient.

It does behave slightly differently, due to the way immutables are
interned.  Probably of no real concern to anybody.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.