-1

    int i = 71;
    System.out.println(i) // gives 71
    System.out.write(i) // gives G

why different output is received? Both are byte streams then why the output is different?

  • 2
    This is explained by the javadocs for `println(int)` and `write(int)`. – Stephen C Feb 21 '19 at 14:57
  • Guess what the unicode code point of the letter 'G' is. – Henry Feb 21 '19 at 14:58
  • @StephenC its not a duplicate question, other questions are dealing with print() method of Printwriter class and this one deals with PrintStream class and also argument is int type. – Rajesh K. Pandey Feb 21 '19 at 15:05
  • The questions are very similar, and the answers is essentially the same. That is sufficient for this to be marked a duplicate. Ask yourself: by reading the dup links / chains can you understand why you are getting a strange value from `write`? – Stephen C Feb 21 '19 at 15:11
  • But to be honest, I don't think you should have asked this in the first place. The answer can be readily found by reading the javadocs. If you didn't read them, that means you didn't do enough research. – Stephen C Feb 21 '19 at 15:14
  • @StephenC To be honest, there is hardly any questions for which answers cannot be found by doing enough research, then what is the whole point of such kind of forums?? Javadoc is integrated with eclipse so yes, that was the first thing that i checked but I was confused as both functions should behave the same but was not behaving and i needed an urgent answer, so i asked this question.So i was knowing what is happening but not sure "why' its happening. "Enough Research" not sure, but "reasonable research" – Rajesh K. Pandey Feb 21 '19 at 16:06
  • Umm ... have you *read* the javadoc yet. Not "looked at" ... but actually *read* them? If you are confused about how methods are supposed to work, you need to actually read the javadoc. And ... IMO ... it is reasonable of us to expect you to do a reasonable amount of research before asking; i.e. reading the javadocs AND searching for existing Q&A's. – Stephen C Feb 21 '19 at 22:49
  • Concerning *"... i needed an urgent answer ..."*. 1) Your urgency is not our concern (e.g. https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest). 2) Reading the javadoc will get you an answer faster! – Stephen C Feb 21 '19 at 22:53
  • Concerning whether duplicates need to be exact, read https://meta.stackoverflow.com/a/262858/139985 – Stephen C Feb 22 '19 at 02:14

1 Answers1

1

write(int b) interprets the number as byte. Capital G is byte 71 in the Ascii-Table.

Sebastian
  • 1,642
  • 13
  • 26
  • u mean to say write(int b) interprets method argument 'b' as byte and println(int b) interprets method argument 'b' as int? Both are ByteStreams though? – Rajesh K. Pandey Feb 21 '19 at 15:03