aspose file tools*
The moose likes Beginning Java and the fly likes Is This program wrong according to Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is This program wrong according to Java?" Watch "Is This program wrong according to Java?" New topic
Author

Is This program wrong according to Java?

Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167


Is This program wrong according to Java?

Varuna


Varuna Seneviratna
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

What happens when you compile it?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
C:\Java\Books\CoreJavaVolumnIFundamentals\Chap02\Welcome1.java:12: variable greeting might not have been initialized
greeting[0]=new String("Welcome to Core Java");
^
1 error

Tool completed with exit code 1
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

So initialize it.
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
How to initialize it?

is it

But it doesn't work the error is below



Welcome1.java:10: incompatible types
found : java.lang.String
required: java.lang.String[]
String[] greeting="";
^
1 error




Varuna
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

This is how to initialize a String variable:


You want an array of Strings. Have a look at the Java Tutorial on Arrays (and bookmark the tutorial, good info there).
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You want to initialize it with a new, empty String array of length 3. What would be the syntax for that?

By the way, never, ever, ever use "new String(...)". It's totally unnecessary. Just

greeting[0]="Welcome to Core Java";


is fine.


[Jess in Action][AskingGoodQuestions]
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167



The above program without using new generates the error



Welcome1.java:10: incompatible types
found : java.lang.String
required: java.lang.String[]
String[] greeting="";
^
1 error


Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Originally posted by Varuna Seneviratna:
The above program without using new generates the error


That's because your syntax is wrong. Did you read the link I posted? Especially the part "Creating, Initializing, and Accessing an Array"?
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167



This gives four errors



Welcome1.java:12: array required, but java.lang.String found
greeting[0]="Welcome to Core Java";
^
Welcome1.java:13: array required, but java.lang.String found
greeting[1]="by Cay Horstman";
^
Welcome1.java:14: array required, but java.lang.String found
greeting[2]="and Gary Cornell";
^
Welcome1.java:16: foreach not applicable to expression type
for(String g:greeting)
^
4 errors






The only program that works is


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Which is just what Joe hinted at.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Originally posted by Varuna Seneviratna:
The only program that works is


Yes. It uses the correct syntax to create and initialize an array.
Why do you expect to use a String variable like a String array variable? They're two different things.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11419
    
  16

when you have

you are saying "give me a reference to something that can hold ONE String.
it's like saying "give me an egg.

When you have

you are saying "in the first empty spot in the egg carton, put a egg". But there IS NO empty spot. There is no egg carton - you didn't create one.

So, when you say

NOW your saying "Give me space to put THREE strings" - a carton to hold three strings. NOW you can say "in the first spot, put THIS string".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
In the first posting I declared was an array String variable.


Then


Then what I tried was to create an String object and assign that to the greeting array variable's 0th position.What I think now is because the greeting[] is not referred to an String type object so it does not have a position to include the value.
Initially what I thought was since greeting is declared as an String array variable it must have the ability to refer to an String type object, the position provided.
My conclusion is I don't know If I am right,The Array variable declared Then required number of array null objects must be created and then values assigned.Am I right?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Originally posted by Varuna Seneviratna:
The Array variable declared Then required number of array null objects must be created and then values assigned.Am I right?


I think you have the idea but your terminology is rough:
Declare an array variable.
Create an array.
Assign values to the array.

In your original code, it is the second step that was missing.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11419
    
  16

Originally posted by Varuna Seneviratna:
In the first posting I declared was an array String variable.




Not quite. What you declared was a REFERENCE to a string array. no actual array has been created.

to be clear - your variables for Objects (and an Array is an object) are REFERENCES to the object itself. it's like a pointer, or an address card. it tell the JVM where the real object is.

Then there is the object itself. the reference and the object have to be connected.

So, when you say

You're saying "give me a slip of paper where i can write down the address of an array that holds string".

you need the ' = new String[3]' part to actually create the array of the right size, and link the reference with the object.
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
Originally posted by fred rosenberger:


Not quite. What you declared was a REFERENCE to a string array. no actual array has been created.



To Be exact What I declared was a String type Reference Array Variable?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Originally posted by Varuna Seneviratna:

To Be exact What I declared was a String type Reference Array Variable?


I would say "a reference to a String array" or a "variable of type String array".
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
String[] myArray;

creates a variable that can hold a reference TO an array of String

it does not create an array of String.


------------------------
Bob
SCJP - 86% - June 11, 2009
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is This program wrong according to Java?