Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to output the java data type to the console?

I'm trying to debug a program I inherited. This program contains Strings, array lists and collections, lots of casting between types, and I need to do some String manipulations (substring, etc.)

The data look like Strings when printed to the console (e.g., it's a line of text, like Johnson, John or Chicago Region), but my code is erroring out with various index out of range errors, suggesting that my code to cast to String isn't working.

I'd like to try to figure out what data types are coming into and leaving my methods to verify that the program is acting as expected. Is there any way to find a field type in Java? In a perfect world, I could generate console output at every step that would give me the data value and whether it's a String, array list, or collection. Can that be done?

like image 922
dwwilson66 Avatar asked May 03 '12 19:05

dwwilson66


People also ask

How do you print to console in Java?

print(): print() method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the end of the text at the console. The next printing takes place from just here.

Which method is used to write console output in Java?

Console writer() method in Java with Examples The writer() method of Console class in Java is used to retrieves the unique PrintWriter object which is associated with the console.

How do I save console output to string Java?

If you create a PrintStream connected to a ByteArrayOutputStream , then you can capture the output as a String . Example: // Create a stream to hold the output ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); // IMPORTANT: Save the old System. out!

How do you store console output in variables?

If you want to do this to an object that has been already logged (one time thing), chrome console offers a good solution. Hover over the printed object in the console, right click, then click on "Store as Global Variable". Chrome will assign it to a temporary var name for you which you can use in the console.


2 Answers

Given an instance of any object, you can call it's getClass() method to get an instance of the Class object that describe the type of the object.

Using the Class object, you can easily print it's type name:

Integer number=Integer.valueOf(15);
System.out.println(number.getClass().getName());

This print to console the fully qualified name of the class, which for the example is:

java.lang.Integer

If you want a more concise output, you can use instead:

Integer number=Integer.valueOf(15);
System.out.println(number.getClass().getSimpleName());

getSimpleName() give you only the name of the class:

Integer

Printing the type of primitive variables is a bit more complex: see this SO question for details.

like image 138
Andrea Parodi Avatar answered Oct 02 '22 13:10

Andrea Parodi


For any object x, you could print x.getClass().

like image 8
Keith Randall Avatar answered Oct 02 '22 12:10

Keith Randall