File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is This program wrong according to Java?

 
Varuna Seneviratna
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is This program wrong according to Java?

Varuna
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you compile it?
 
Varuna Seneviratna
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So initialize it.
 
Varuna Seneviratna
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Varuna Seneviratna
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Pie
Posts: 20399
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is just what Joe hinted at.
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12024
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Varuna Seneviratna
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12024
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 320
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String[] myArray;

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

it does not create an array of String.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic