Running on Java 17+35-2724 (Preview)
Home of The JavaSpecialists' Newsletter

2.17 Printing the indexes array

Juppies 2 by Dr Heinz M. Kabutz

We want to show the indexes before and after shuffling. The easiest way is to print this to the console. What on earth is a console? Well, a very long time ago, computer systems were just letters and numbers on a screen. We had no beautiful graphical user interfaces with millions of colours and 10 million pixels. When I was in my 2nd year of university, that's what we had. 10 students at a time would connect via text terminals to a single 80386 machine running IBM's AIX. Single CPU of course. We had a graphics course. Go figure. Since our screens could only show text, we had to print our graphics assignments to the shared dot-matrix printer. When we say that we want to print some output, that would mean sending it to our Command Prompt on Windows, or our Terminal if we are using Mac OS X. In NetBeans we will see the output at the bottom of the IDE in the "Output" tab.

To print information to the standard output, we use the shortcut "soutv" followed by the tab button. This then picks the closest variable (either local variable, parameter or field) and generates the following code for us:

  System.out.println("variable = " + variable);

Very nice!

(The soutv shortcut is precisely the same as IntelliJ's which makes it easy swapping between NetBeans and IntelliJ)

Note: NetBeans is a platform and a framework that we can use to create rich client applications. For example, JVisualVM is a free performance profiler that is built on top of NetBeans platform. NetBeans is not only an IDE for writing code. Because it is open source, we can use it for our rich GUI applications. IntelliJ is only an IDE - which it does remarkably well.

We now run the code and see strange output like indexes - [I@5d624da6. What is that all about? This is the default behavior if we print an array to the console. The [I means it is an int array, and the funny hexadecimal number after the "@" looks like a memory location, but it is not. This is the hash code of the object. We will learn about this in a later lesson. Obviously this did not give us the output that we were hoping for. We thought it would print [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].

When we run the test, we notice that the output is always the same shuffled order of [4, 8, 9, 6, 3, 5, 2, 1, 7, 0]. What's with that? Turns out we must have some mistake in our shuffle() method that causes it to not be very "random".

Comments

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Table of Contents

We hope you enjoyed this tutorial. If you did, you will also enjoy our courses. We suggest you start with Extreme Java - Advanced Java, followed by Extreme Java - Concurrency Performance for Java 8.



About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack 21

Superpack 21 Our entire Java Specialists Training in one huge bundle more...

Free Java Course

Free Juppies 2 Course
Juppies 2 - a course for complete beginners more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...

Java Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.