• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java array and String problem

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone ...........And ya Rob special thanks for clearing the doubt of "?" in output
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Campbell, why there is a need to perform mod 256 to the given numbers passed ?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and remember a char is not actually a character, but an integer.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but bytes have nothing to do with this.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I got confused because somebody said byte[] earlier in the thread.
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic