• Post Reply Bookmark Topic Watch Topic
  • New Topic

var_dump() in java  RSS feed

 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to all
I just want to is there any method/function to retrieve data type in Java?

For example: var_dump(); function in php. That function is sooooooooooooooo helpful



output will
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so.

Now..i know nothing about PHP, but it looks like it is not strongly typed. Java is. so you should know already what type a variable in java is by looking at the code.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the getClass method on individual objects to get the type.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ibrahim yener wrote:I just want to is there any method/function to retrieve data type in Java?
For example: var_dump(); function in php. That function is sooooooooooooooo helpful

Well, it doesn't appear to output just the data type; it also outputs the contents. And in Java that's usually done with the toString() method (in the case of arrays, you use Arrays.toString() or Arrays.deepToString()).

Question: Why do you think you need to know the type? It's NOT a very common requirement in Java, because the language is statically typed and it's not a scripting language.

Winston
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although I agree with Winston, but still if you need to figure out the data type , how about using the instanceof keyword ?
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone who answered my question.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always get suspicious when I see instanceof; I suspect there is an alternative way to do it, which would be more object‑oriented.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I always get suspicious when I see instanceof; I suspect there is an alternative way to do it, which would be more object‑oriented.

While I definitely agree with your general point, on the rare occasions that I need to do type checking, I will always use instanceof if I can, specifically because it is a type check (as opposed to a class check). Furthermore, it handles nulls and it's result is recognised by the compiler, viz:does not produce a warning.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!