Article View: borland.public.jbuilder.java.language
Article #15970Re: Integer class
From: Chris Hill
Date: Mon, 12 Apr 1999 00:00
Date: Mon, 12 Apr 1999 00:00
88 lines
2837 bytes
2837 bytes
On Sun, 11 Apr 1999 17:17:27 -0700, "Jason C. Leach"
<leachjc@hotmail.com> wrote:
>hi,
>
>Ya, need to read a byte from a file (binary format) and compair that
>byte against the HEXidecimal character/value '0x0a'.
>So the byte I read I do: if (myInt.intValue() == 0x0a) //0x0a is a
>LINE FEED char.
>{ bla bla bal }
>
>It should work OK. I am also going to need to write that int I just
>read back to a different output file. Will writing the int value work
>OK?
Yes, if you call FileOutputStream.write(int) a byte will be written to
the stream.
>I supose I don't really need to wrap it in an integer object. I just
>don't like declairing global variables. Perhaps this is just a habbet
>from C++. Thus by wraping it in an Int object, I can pass it by
>refrence, and save the functions return for error checking or a status
>report.
>
>What do you think? Far less ifficient to do it this way in Java? I
>don't mind changing the formant if its less efficient and uncommon to do
>it this way in Java.
Since you are only using one byte of the int for the data you read,
you can use the rest for error handling. Notice that
FileInputStream.read() does this. It returns -1 if the end of the
file is reached.
I'm not sure you can do what you want with the Integer class. Method
parameters which are classes are passed by reference, but you can't
assign a value to the parameter within the method and cause the
variable that was passed to change to reference the new value. You
can change the internal state of the object referenced by the
parameter, but Integer doesn't allow you to change its value.
Here is an example:
// begin Para.java
class Para
{
public void go(Integer a, int b)
{
a = new Integer(b);
}
public void go2(MyInt a, int b)
{
a.setVal(b);
}
public static void main(String params[])
{
Para p = new Para();
Integer m = new Integer(89);
p.go(m,101);
System.out.println(m.toString());
MyInt m2 = new MyInt(67);
p.go2(m2,555);
System.out.println(Integer.toString(m2.getVal()));
}
}
class MyInt
{
int val;
MyInt(int v)
{
val = v;
}
void setVal(int v)
{
val = v;
}
int getVal()
{
return val;
}
Chris Hill
Chris244@aol.com
Technical Details
Message-ID:
<37114da0.2332890198@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3710E522.7A438C7E@hotmail.com> <3710fa40.2311545937@forums.borland.com> <37113B97.9D2F41A9@hotmail.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,48b0db82994ba0f7 X-Google-Attributes: gidf74b3,public From: Chris244@aol.com (Chris Hill) Subject: Re: Integer class Date: 1999/04/12 Message-ID: <37114da0.2332890198@forums.borland.com> X-Deja-AN: 465311394 References: <3710E522.7A438C7E@hotmail.com> <3710fa40.2311545937@forums.borland.com> <37113B97.9D2F41A9@hotmail.com> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.language