Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Iam trying to create an array of integers using bytecode instructions

http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings

Any ideas how to do it?

share|improve this question
    
Did you look at the anewarray or newarray instructions? –  templatetypedef Jun 8 '13 at 0:53
    
Yes but the problem is i do not know how to syntax it for example it doesn't say how to put the size of the array. –  Tony Jun 8 '13 at 0:57
    
@Tony. You may write actual java code, compile and then use javap (docs.oracle.com/javase/7/docs/technotes/tools/windows/…) to get the bytecode. –  Jayan Jun 8 '13 at 1:13

1 Answer 1

The size of the array is popped off the stack, so you need to push it onto the stack first.

To create a 1000 element array for instance, you could do something like

sipush 1000
newarray int

Edit: I just noticed that there's a standard lib function to fill arrays. That makes things much easier.

sipush 1000
newarray int
dup
iconst_1
invokestatic java/util/Arrays fill ([II)V
share|improve this answer
    
ok thanks. And if you want for example to initialise the array with the number 1 after the creation what i will have to do? –  Tony Jun 8 '13 at 2:00
    
You mean initialize every element of the array to 1? You'd have to write a loop. –  Antimony Jun 8 '13 at 5:16
    
Can you show me some code using instructions of asm to understand how to do it? –  Tony Jun 8 '13 at 11:53
    
@Tony I just posted an example of how to do it with Arrays.fill. –  Antimony Jun 8 '13 at 16:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.