Why isn't new used in the following.

G

grocery_stocker

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
 
J

John W. Kennedy

grocery_stocker said:
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?

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

Stefan Ram

grocery_stocker said:
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
 
S

Stefan Ram

grocery_stocker said:
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
 
S

saimatam at yahoo dot com

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.
 
L

Luc The Perverse

Oliver Wong said:
[...]
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.


Personally - I do actually find it very convenient.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top