Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java array and String problem

 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all i have 2 doubts

(1) we know that arrays in Java are created using the new operator
But i have seen this Now my question is how come we store values in array when it is not created in memory?

(2)
My question is why ? is coming for both 600 and 858 and even if i put no like 900 or 957 it gives ? as output

Thanks in advance
 
Angus Comber
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am really new to Java but I can see lots of problems with your question.

Firstly, when you say:
Now my question is how come we store values in array when it is not created in memory?

You are confused here I think.

When you say new <something> it means you are storing the object on the heap. You probably should do a search for heap to work out what this is.

Primitives in Java are stored on the stack. This is a different area of memory to the heap - but it is still stored in memory. You should definitely get stack and heap clear in your mind - very important computer science concepts.

In your code you are trying to form a String from an array of int's. You need to also revise types. If you want to make a String from an int you need to convert the int to a String type. Java is a strongly typed language - see here - http://en.wikipedia.org/wiki/Strong_typing

 
Jacek Garlinski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rameshwar Soni wrote:Now my question is how come we store values in array when it is not created in memory?


As far as i know, arrays are always created in memory ;) especially on the heap ;)

Rameshwar Soni wrote:
(2)
My question is why ? is coming for both 600 and 858 and even if i put no like 900 or 957 it gives ? as output

Thanks in advance

I'm not sure if I understand you well but,
"a" - is an array of integers (primitives), one of String's constructor is String(byte[] bytes, int offset, int length) - so as first argument it takes array of bytes.
When you pass your array, its values are casted to bytes and then for each value ascii representation is taken - so 65 is letter A, and 66 is letter B.

 
Jacek Garlinski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angus Comber wrote:
Primitives in Java are stored on the stack. This is a different area of memory to the heap - but it is still stored in memory. You should definitely get stack and heap clear in your mind - very important computer science concepts.

Instance variables and objects live on the heap.
Local variables live on the stack.
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacek Garlinski wrote:one of String's constructor is String(byte[] bytes, int offset, int length) - so as first argument it takes array of bytes.
When you pass your array, its values are casted to bytes and then for each value ascii representation is taken - so 65 is letter A, and 66 is letter B.

String also has an constructor which takes an integer array so there is no need to convert from int to bytes

 
Jacek Garlinski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rameshwar Soni wrote:
Jacek Garlinski wrote:one of String's constructor is String(byte[] bytes, int offset, int length) - so as first argument it takes array of bytes.
When you pass your array, its values are casted to bytes and then for each value ascii representation is taken - so 65 is letter A, and 66 is letter B.

String also has an constructor which takes an integer array so there is no need to convert from int to bytes


you're right, i looked at 1.4 doc.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for the "AB??" - the system console on both Windows and Linux is very, very limited in what characters it can display. Usually it can only handle ASCII characters, with values between 32 and 127. Anything else cannot be printed, and will show up as ?. The following is a better way of displaying Strings that cannot be properly printed:
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone ...........And ya Rob special thanks for clearing the doubt of "?" in output
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are getting all confused. There are several discussions going on here, most of which don't relate to the original question.

Back to the original question 600 % 256 is 88 and 858 % 256 is 90, which correspond to X and Z if you look in this Unicode page. The corresponding values for the ints are 600 = ɘ and 858 = ͚, obviously because you are passing an int[] to that String constructor.

I think the reason you are getting ? is because you are using a Windows/DOS command line which does not support those characters.
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Campbell, why there is a need to perform mod 256 to the given numbers passed ?
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because casting an int to a byte removes all but the lowest-order 8 bits. That is equivalent to the remainder when divided by 256. If you pass the int[] to the String constructor, there is no need to consider the % operator.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when you use an int[] to create a String it doesn't convert those ints to bytes but to chars instead:
The contents of the subarray are converted to chars;
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and remember a char is not actually a character, but an integer.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but bytes have nothing to do with this.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I got confused because somebody said byte[] earlier in the thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic