can not convert long ?

V

vertigo

Hello
Sorry for such silly question but i can not convert long to int
or int to string:

File f = new File(fullname);
String len=f.length().toString();//1
int len=f.length().intValue;//2

in both cases i receive durring compilation:
"long cannot be dereferenced"
Why ?

Thanx
Michal
 
C

Christophe Vanfleteren

vertigo said:
Hello
Sorry for such silly question but i can not convert long to int
or int to string:

File f = new File(fullname);
String len=f.length().toString();//1
int len=f.length().intValue;//2

in both cases i receive durring compilation:
"long cannot be dereferenced"
Why ?

Thanx
Michal

long is a primitive, not an Object, so it has no methods. java.lang.Long
(notice the capital L) is the Object wrapper for long.

You should also be aware, that if you convert a long to an int, you'll lose
precision (a long is 64 bits while an int is 32bits).

You can convert the long to an int like this:

int length = (int)f.length();

But why would you need an int? If you want the size in KB or MB, just divide
the length by 1024 or 1024*1024 and use that.

And ask these kind of questions in c.l.j.help in the future, followup set.
 
L

Liz

vertigo said:
Hello
Sorry for such silly question but i can not convert long to int
or int to string:

File f = new File(fullname);
String len=f.length().toString();//1
int len=f.length().intValue;//2

in both cases i receive durring compilation:
"long cannot be dereferenced"
Why ?

Thanx
Michal

First you have two variables with the same name.
Second, the following does work. Long is a class that
contains a long.

File f = new File(fullname);
String len1 = new Long(f.length()).toString(); //1
int len2 = new Long(f.length()).intValue(); //2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top