Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

byte array initialization

 
Ideal Cana
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can I initialize a byte array like this?
byte array[]=new array[]{255,255,255,0}; the compliler says cast from int to byte is needed. I don't understand why it needs cast.
btw, anyone would tell me how to install and show an IP address in the program? (not network programming, just input, store and output the IP address)
thanks
ideal
 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For ur first Question,
the values u have specified for ur byte array are beyond the max limit of what a byte can hold
Byte has 8 bits of storage and can store from
-128 to 127
This wud work ....
byte[] array=new byte[]{127,-128,0};
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ideal:
byte array[]=new array[]{255,255,255,0}; the compliler says cast from int to byte is needed. I don't understand why it needs cast.

255 won't fit into a byte. Try
byte array[]=new array[]{127,127,127,0};

Please read http://www.javaranch.com/name.jsp and change your name to conform. We'd like you to continue to post here at JavaRanch. Thanks.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ideal
Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please re-register under an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!

As for your question:
The literal 255 is an int, in order to store it in a byte variable you have to cast it to a byte. But when you do that you'll lose some of the info because 255 is larger than a byte (-128 to 127).
As for your second question:
Input the ip how? From the command line as an argument to main? or as an input when prompted for it?
As far as storing and printing the ip - where do you want it stored (disc, text file, database, ...) and to what do you want it printed (printer, screen, dialogbox, ...). Do you have any code so far? post it so we can see where you're at. Sorry for so many questions but you need to give some more detail to get any meaningful help.
Also, this sounds like a school asignment so you might not get complete answers but little nudges in the right direction.
[ January 31, 2002: Message edited by: Dave Vick ]
 
Ideal Cana
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks the responses. I changed my name to "maple", is that fine?
Dave, my task is to accept a destination IP address from the command line, then simulate the procedure to look up the next hop to this destination in a routing table. I should difine this routing table with the given contents. for example:
routing table:
mask destination nexthop...
255.255.255.0 192.168.1.0 192.168.2.1
I don't have idea how to define this table.
maple
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maple,
Did you READ the naming policy?? It says that your display name should have at least 2 (as in two) separate names, with a space in between. Each name should be more than one letter. The name should not be obviously fictitious (such as "Maple"}. It says that we REALLY want you to use your real name.
Please try again.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "next hop"? Is there a pre-defined routing or are you talking about the nodes in the IP?
 
Ideal Cana
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the name policy. Believe it or not, my nick name is Ideal.
yes, the contents of the routing table is pre-difined. Now I need to show this table with Java in computer.
I'm thinking to use String and StringTokenizer. Any suggestion? btw, nexthop is just a column name of this table, its value is also IP address.
Ideal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic