aspose file tools*
The moose likes Java in General and the fly likes int can not be dereferenced means what Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "int can not be dereferenced means what" Watch "int can not be dereferenced means what" New topic
Author

int can not be dereferenced means what

vivek gaur
Greenhorn

Joined: Dec 30, 2005
Posts: 21
in the below program i want to convert int to String type using .toString().Can't i do like that . if not then how do i do it?
The entire program is ok if i print day as int , but while converting to String it says 'int cannot be dereferenced'
then how do i do?



import java.util.*;
import java.util.*;

class democal
{
public static void main(String[] args)
{
Calendar cal = new GregorianCalendar();
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int justmonth = cal.get(Calendar.MONTH) ;

//for(int i=6;i>=1;i--)
for(int i=1;i<=6;i++)
{
day = cal.get(Calendar.DATE) - i;

System.out.println("ii: " +day);
// System.out.println("ii: " +day.toString());
String ss=day.toString();

}
//Calendar cal = new GregorianCalendar();



}
}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
No you cannot call a method on a primitive. Just use the name of the primitive.
vivek gaur
Greenhorn

Joined: Dec 30, 2005
Posts: 21
then how do i do .
please advice
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
To print it out just use the name of the primitive.

To convert the primitive to it's String value use String.valueOf
vivek gaur
Greenhorn

Joined: Dec 30, 2005
Posts: 21
thank u man ,
i got it .
Jeremy Tartaglia
Ranch Hand

Joined: Mar 11, 2004
Posts: 62
There are two types of data in Java: intrinsics and objects.

An intrinsic is a built in data type which is a single element. Ints, Floats, Doubles, Chars, Bytes, Shorts, and Longs are all intrinsics. They have no methods or properties assoicated with them, since they are a single element. They "point" to nothing, if you want to think of it like that.

An object is a class derived from java.lang.Object, which is every class written in Java. (This raises the question of what to call java.lang.Object, but let's ignore that little wrinkle. ) All Objects have certain methods and properties associated with them, like toString and hashCode. When you set a variable to an object, what you get is a pointer, more recently called a reference. (Sorry hardcore Java gurus, I'm an old ASM/C guy at heart, so they're pointers to me, not references. ) It "points" to an area in memory where the information about the class is stored. Predefined properties, abstract methods, the whole nine yards.

When Java says "x can't be dereferenced", it means you're trying to tell it that an intrinsic is an object. If you absolutely MUST get an int into a String, this code should work fine:



There are other ways, but this is probably the most concise.
Srivatsan santhanam
Greenhorn

Joined: Jan 04, 2006
Posts: 23
on the fly simply append "" to int and you get a String !!

like i+"";

-Srivats


Java Objects passed by Reference ?? -> you are a failure !!
David Ashby
Greenhorn

Joined: Mar 20, 2009
Posts: 1
I have a script to enable a user to search a text file, I am trying to convert it to use it in another project however I am a little out of my depth.

I would be grateful if someone could take a look at it and see if they can solve the issue.



The line that seems to be causing me the proble is

I cant work out how to construct the code

Thanks in advance for any help!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Srivatsan santhanam wrote:on the fly simply append "" to int and you get a String !!

like i+"";

-Srivats

Please don't. Really. It's less efficient.

If you do i + "", the compiler turns it into the following code:
That will create a new StringBuilder, add the int, add the empty string and then turns it into a String. Ignoring the "", that is two objects. The thing is, though, appending the int will call the same method that turns an int into a String. So in the end, you create one object for nothing: a simple String.valueOf(i) or Integer.toString(i) will be just as fast (if not faster), without the StringBuilder object.

So next time you want to turn an int into a String, PLEASE use String.valueOf(i) or Integer.toString(i).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
David Ashby, welcome to JavaRanch

Please start a new thread; your question appears unrelated to the original question. You are unlikely to get a useful answer here.

[Pedantic mode]Jeremy Tartaglia, you are right about two sorts of data types, but they are called primitives, not intrinsics, and they should all be spelt with small letters throughout. There are in fact objects (also called references types) with the same names you have quoted, with initial capital letters.
You can also find booleans, which are primitives. Note that the names of the primitive types (boolean, byte, char, double, float, int, long and short) are all keywords.[/Pedantic mode]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int can not be dereferenced means what