• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Question from Jaworski mock exam

 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just taking the Jaworski mok exam and I keep having this problem. There is this one question where you are writing the string "test" out to a file. It asks you how many bytes are written. The answer is 4 bytes. How do you get that?! Seems to me since a char is 2 bytes and there are 4 chars, wouldn't it be 8? This question keeps bugging me.
Matt
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know what the exact question is,but it depends on whether you are using a byte stream or character oriented stream.
If you use one of the byte streams classes, the output is written as bytes and the write() method takes a int as argument for byte streams.
so if "test" were written to a file using a bytestream class,4 bytes would be needed
 
Matt DeLacey
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in fact, it WAS a byte stream, but could you explain to me HOW exactly you can write 4 chars in 4 bytes? This confuses me.
Matt
P.S. Thanks for your help.
 
poorna k
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as per the API the write(int b) method of InputStream class
"Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored. "
The subclasses of InputStream /OutputStream write to the stream as bytes.
A character is stored as 16 bits in Unicode character set.All
the characters in the ASCII character set are represented as a subset in the Unicode character set convention by ignoring the first 8 significant bits in the Unicode representation.
In the ascii convention,characters can be represented as integers also,
e.g the character 'A' can be represented as 065 in decimal
Since automatic type promotion is allowed in java from char to int,your write() method would convert the char argument to int,and as per the above quote, write only the 8 low-order bits to the stream
coming back to your qn when you invoke
write('t'),
't' can be represented as 116 in decimal.so type promotion occurs from char to int.116 can be represented within the lower order 8 bits,and the 8 high
-order bits are zeros.So you will not
be losing any information from the high-order bits.
If you use other characters from the unicode character set(e.g characters from other languages such as spanish)
you would need to use the subclasses of Reader / Writer ,so that all the 16 bits can be output to the stream
I hope this is clear.
[This message has been edited by poorna k (edited October 18, 2000).]
[This message has been edited by poorna k (edited October 18, 2000).]
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic