aspose file tools*
The moose likes Beginning Java and the fly likes byte array initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "byte array initialization" Watch "byte array initialization" New topic
Author

byte array initialization

Ideal Cana
Greenhorn

Joined: Jan 30, 2002
Posts: 3
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

Joined: Nov 30, 2001
Posts: 102
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

Joined: Jul 22, 2000
Posts: 9044
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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 ]

Dave
Ideal Cana
Greenhorn

Joined: Jan 30, 2002
Posts: 3
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

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Jan 30, 2002
Posts: 3
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: byte array initialization