wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java array and String problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java array and String problem" Watch "Java array and String problem" New topic
Author

Java array and String problem

Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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

Joined: Jul 16, 2011
Posts: 90
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

Joined: Aug 08, 2011
Posts: 8
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.


Please correct my English.
Jacek Garlinski
Greenhorn

Joined: Aug 08, 2011
Posts: 8
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

Joined: Feb 03, 2011
Posts: 247
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

Joined: Aug 08, 2011
Posts: 8
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Thanks everyone ...........And ya Rob special thanks for clearing the doubt of "?" in output
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
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

Joined: Feb 03, 2011
Posts: 247
hi Campbell, why there is a need to perform mod 256 to the given numbers passed ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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

Joined: Oct 13, 2005
Posts: 38046
    
  22
. . . and remember a char is not actually a character, but an integer.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Yes, but bytes have nothing to do with this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
Yes, I got confused because somebody said byte[] earlier in the thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java array and String problem
 
Similar Threads
Problem in Arrays passing.
Arrays doubt
array doubt
What is the input file?
Beginner