This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

velmurugan notes (arrays)

 
kamal jaisingh
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i read in velmurugan notes abt arrays ,
tht when an array of 5 Strings is created , total 6 objects
r created.
i am a little confused on this point.
is it tht 5 String objects and 1 array object is created , ie
6 objects???
can anyone xplain is this right , or theres somthin else to
this

regards
Kamal
 
Wong KK
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right.
e.g.
String[] a = {"abc", "cde"};
Then 3 objects are created.
 
kamal jaisingh
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx a lot for ur prompt reply
i got it
regards
KAmal
 
Indika Perera
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, another doubt from same set of Notes.
Following is from Chapter 15 of Velmurugan's notes.
* Reader contains these methods for reading characters and arrays of characters:
abstract int read() throws IOException
int read(char cbuf[]) throws IOException
int read(char cbuf[], int offset, int length) throws IOException
InputStream defines the same methods but for reading bytes and arrays of bytes:
abstract int read() throws IOException
int read(byte cbuf[]) throws IOException
int read(byte cbuf[], int offset, int length) throws IOException
* Writer and OutputStream are similarly parallel. Writer defines these methods for writing characters and arrays of characters:
abstract int write(int c) throws IOException
int write(char cbuf[])throws IOException
int write(char cbuf[], int offset, int length) throws IOException
And OutputStream defines the same methods but for bytes:
abstract int write(int c) throws IOException
int write(byte cbuf[]) throws IOException
int write(byte cbuf[], int offset, int length) throws IOException

What I notice is , int read() in Reader is not abstract as per Java 1.2 source code, but int read (cbut[], offset, length) is abstract. Similarly for writer int write(int) not abstract but int write (cbuf[], offset, length)is.
Can somebody please clear my doubt.
 
niraj singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
The Reader class is a superclass of the BufferedReader and other such classes. The read(char[] cbuff...) method is abstract in the Reader class, as it is just a template to be defined in a subclass where a buffer is implemented. The method is not abstract in the BufferedReader class. Same applies for InputStream and Writer classes.
 
Indika Perera
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Niraj,
Thanks for the response. I agree with what you're saying.
But, my doubts are whether, read() in Reader and write(int c) in Writer are abstract, as mentioned in above set of notes. I noticed that this is not correct, while going through java source code.
 
vkswamy venkatachalam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Indika
Reader is a public Abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int) and close().
int read()
Read a single character.
int read(char[] cbuf)
Read characters into an array.
abstract int read(char[] cbuf, int off, int len)
Read characters into a portion of an array.
Writer is public abstract class which extends Object
for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close().
void write(char[] cbuf)
Write an array of characters.
abstract void write(char[] cbuf, int off, int len)
Write a portion of an array of characters.
void write(int c)
Write a single character.
void write(String str)
Write a string.
void write(String str, int off, int len)
Write a portion of a string.
In short,read(char[] cbuf, int off, int len) in abstract Reader class and write(char[] cbuf, int off, int len) is absract in abstract writer class.Hope this will clear .
with regds,
vkswami.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic