# Println(what); - "what" argument datatype

**URL:** https://discourse.processing.org/t/println-what-what-argument-datatype/23221
**Category:** Development
**Created:** [August 13, 2020, 3:44am UTC](https://discourse.processing.org/t/println-what-what-argument-datatype/23221 "2020-08-13T03:44:53Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Natalie](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/natalie/32/10239_2.png) [@Natalie](https://discourse.processing.org/u/Natalie)
#### Post date: [August 13, 2020, 3:44am UTC](https://discourse.processing.org/t/println-what-what-argument-datatype/23221/1 "2020-08-13T03:44:53Z")

</div>

I’m making a debug tool in which I can print out values on a gui every frame, and I want to be able to input any type of value—much like how println/print works.

After looking at [the reference page](https://processing.org/reference/println_.html), I noticed how it said that the datatype of the argument(s) is called “what”, and I don’t know exactly what this means. Below it states that the “what” refers to various other datatypes (i.e. Object, String, float, char, boolean, etc.), but I was curious as to how exactly println does this.

What I originally thought is that it does something like this, by overloading the function:

```auto
void println(String... p) { [...] }
void println(float... p) { [...] }
void println(char... p) { [...] }
void println(boolean... p) { [...] }

```

But I then realized that this is impossibly the way that it’s programmed, because not only can you input user-defined objects (which would not work using this method), but you can also input various _different_ datatypes like such:

```auto
println("text", 1.0, 'a', false, [...]);

```

Is it possible to replicate a system that can accept arguments like these so that I could implement this into my debugging tool?

Thanks!

---

<div class="post-metadata">

### Author: ![Natalie](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/natalie/32/10239_2.png) [@Natalie](https://discourse.processing.org/u/Natalie)
#### Post date: [August 13, 2020, 6:35am UTC](https://discourse.processing.org/t/println-what-what-argument-datatype/23221/2 "2020-08-13T06:35:23Z")

</div>

Update:  
I looked a little more and was able to find more in-depth [documentation for println](https://processing.github.io/processing-javadocs/core/processing/core/PApplet.html#println--)—in the [PApplet github page](https://processing.github.io/processing-javadocs/core/processing/core/PApplet.html). I have a lot more to look into haha

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [August 13, 2020, 6:53am UTC](https://discourse.processing.org/t/println-what-what-argument-datatype/23221/3 "2020-08-13T06:53:25Z")

</div>

What isn’t a datatype.

Example of data types: float, int, bool etc.

Println is a function which accepts parameters and uses console log to write to the console.

Anatomy of a function

Void name ( parameter){

}  
Void is the return type and this one specifies that there is nothing to return. It can however have a data type instead of void, in which case you must use `return` to return the data type specified.

The parameter field can be empty or it can contain one or more parameter.

In a class you can only have one instance of a function unless the parameters are different also called overloading.

So I cannot have two functions named doSomething();

But I can do this

Void doSomething( int a){

}

Void doSomething(float a){

}

Also changing void at the start does not count as overloading, only amending or adding parameters counts as overloading.

Overloading shouldn’t be confused with overriding which is a bit more advanced as you have to know about class inheritance first.

> **[Method Overloading in Java with examples](https://beginnersbook.com/2013/05/method-overloading/)**
>
> Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. It is

> **[Overriding in Java - GeeksforGeeks](https://www.geeksforgeeks.org/overriding-in-java/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

---

<div class="post-metadata">

### Author: ![Hapiel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hapiel/32/10226_2.png) [@Hapiel](https://discourse.processing.org/u/Hapiel)
#### Post date: [August 13, 2020, 10:36am UTC](https://discourse.processing.org/t/println-what-what-argument-datatype/23221/4 "2020-08-13T10:36:55Z")

</div>

So the println() function works by having multiple functions with different parameters, as described in the docs you linked to.  
For a different approach, I found this when googling: [https://stackoverflow.com/questions/2914695/how-can-you-write-a-function-that-accepts-multiple-types/2914730](https://stackoverflow.com/questions/2914695/how-can-you-write-a-function-that-accepts-multiple-types/2914730)

The resulting code work in processing:

```auto
void setup() {
   printing('a');
   printing("yourstring");
   printing(123);
}

<T> void printing(T stuff){
  println(stuff);
}

```

In the link above it is also explained how to make this work with arrays n stuff. I hope that helps!

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [August 13, 2020, 11:23am UTC](https://discourse.processing.org/t/println-what-what-argument-datatype/23221/5 "2020-08-13T11:23:20Z")

</div>

what is just the name of the parameter in the reference (print what). It’s more a word play really. An example for what is 3 or “Hello”.

So indeed different data type can be used.

> [@Natalie](#):
>
> Is it possible to replicate a system that can accept arguments like these so that I could implement this into my debugging tool?

I am not sure about your example of various _different_ datatypes.

But here is an example with an example function that accepts different parameters; in fact they are different functions with the same name but different parameters.

```auto

void setup() {
  size(700, 770);
}

void draw() {
  background(0); 

  float b1=0.34; 
  myText(b1); 

  String b2="Hallo";
  myText(b2);

  int b3=4; 
  myText(b3);

  myText(4, 7, 9, 12, 17);
}

//--------------------------------------------------------------

void myText(float a) {
  text(a, 100, 100);
}

void myText(int a) {
  text(a, 100, 200);
}

void myText(String a) {
  text(a, 100, 300);
}

void myText(int... a) {
  int x=100; 
  for (int i1 : a) { 
    text(i1, x, 400);
    x+=100;
  }
}
//

```

---

<div class="post-metadata">

### Author: ![Spaceman1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/spaceman1/32/9970_2.png) [@Spaceman1](https://discourse.processing.org/u/Spaceman1)
#### Post date: [August 13, 2020, 6:13pm UTC](https://discourse.processing.org/t/println-what-what-argument-datatype/23221/6 "2020-08-13T18:13:15Z")

</div>

> [@Natalie](#):
>
> But I then realized that this is impossibly the way that it’s programmed, because not only can you input user-defined objects (which would not work using this method),

It _is_ possible to design a function to accept user-defined objects with the typical overloading just by using the general datatype Object. The reference describes the arguments for a list of variables as an Object . I think this just means it is defined as a varying length list of variables, like in @Chrisir example just the data type is of type Object.

The real challenge is how the function should figure out what is the appropriate way to display the user-defined types. The println function doesn’t really display the value of the complex data types given to it

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [August 13, 2020, 8:06pm UTC](https://discourse.processing.org/t/println-what-what-argument-datatype/23221/7 "2020-08-13T20:06:22Z")

</div>

> [@Spaceman1](#):
>
> just by using the general datatype Object

I tried this below using try/catch expression.

With `Object... a` in the header of the function.

It would be more elegant if we had something like :

```auto
switch(typeOfObject(o1)) {
   case STRING_:
   ...
   break; 
   case INT_:
   ...
   break; 
   case FLOAT_:
   ...
   break; 
   .....
}//switch

```

> [@Spaceman1](#):
>
> The println function doesn’t really display the value of the complex data types given to it

true to an extent.

It shows PVector and also arrays quite well.

There is also `printArray()`.

Sometimes I write my own `println()` functions though.

Chrisir

```auto
void setup() {
  size(700, 770);
}

void draw() {
  background(0); 

  float b1=0.34; 
  myText(100, b1); 

  String b2="Hallo";
  myText( 200, b2);

  int b3=4; 
  myText(300, b3);

  myText(400, 4, 7, 9, 12, 17 );

  myText(500, 4, "7", 9.872, 12, "17", "Hello", 19 );
}

//--------------------------------------------------------------

void myText(float y1_, 
  Object... a ) {

  int x=100;

  for (Object o1 : a) {

    try {
      text((String) o1, x, y1_);
    } 
    catch(Exception e) {
      try {
        text((float) o1, x, y1_);
      }
      catch(Exception e2) {
        text((int) o1, x, y1_);
      }
    }
    x+=100;
  }//for
}
//

```

---

<div class="post-metadata">

### Author: ![Spaceman1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/spaceman1/32/9970_2.png) [@Spaceman1](https://discourse.processing.org/u/Spaceman1)
#### Post date: [August 13, 2020, 8:58pm UTC](https://discourse.processing.org/t/println-what-what-argument-datatype/23221/8 "2020-08-13T20:58:17Z")

</div>

Nice! I really like this idea for debugging, building a general-purpose println for complex data types. I just wonder if there would be any way to display all the variables in any class, sort of like printArray. I don’t know if its possible to access these variables without knowing the names, but it would be really helpful not to have to write new code in the printing function for each data type.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [August 13, 2020, 9:36pm UTC](https://discourse.processing.org/t/println-what-what-argument-datatype/23221/9 "2020-08-13T21:36:54Z")

</div>

processing itself has a debugger in its newer versions though

---

<div class="post-metadata">

### Author: ![Natalie](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/natalie/32/10239_2.png) [@Natalie](https://discourse.processing.org/u/Natalie)
#### Post date: [August 14, 2020, 5:33am UTC](https://discourse.processing.org/t/println-what-what-argument-datatype/23221/10 "2020-08-14T05:33:58Z")

</div>

Thank you, everybody for your help!  
I think Chrisir was pretty spot on for what I was looking for, so for my tool I’ll probably do something similar to that.

---

<div class="post-metadata">

### Author: ![KartikSoneji](https://avatars.discourse-cdn.com/v4/letter/k/e480ec/32.png) [@KartikSoneji](https://discourse.processing.org/u/KartikSoneji)
#### Post date: [August 15, 2020, 4:27pm UTC](https://discourse.processing.org/t/println-what-what-argument-datatype/23221/11 "2020-08-15T16:27:59Z")

</div>

> [@Spaceman1](#):
>
> I don’t know if its possible to access these variables without knowing the names

You might want to take a look at the [`java.lang.reflect` docs](https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/package-summary.html), specifically the [`Field` class](https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Field.html).  
There is also a tutorial: [Discovering Class Members (The Java™ Tutorials \> The Reflection API \> Classes)](https://docs.oracle.com/javase/tutorial/reflect/class/classMembers.html)

Here is a sample function that will print all the variables of an object:

```java
void debugPrint(Object o){
	System.out.println(o.getClass().getName() + "{");
	
	for(Field f:o.getClass().getDeclaredFields())
		try{
			f.setAccessible(true);
			System.out.printf(
				"\t%-10s %s %s\t= %s\n",
				Modifier.toString(f.getModifiers()),
				f.getType().getSimpleName(),
				f.getName(),
				f.get(o)
			);
		}
		catch(IllegalAccessException e){
			System.out.println(e);
		}
	
	System.out.println("}");
}

```

The output can get a bit messy, but it prints the type of the object modifiers, variable type, variable name and value.
