Free Starter Java Programming Lesson

First install a Compatible Java Development Kit and Eclipse

https://www.oracle.com/java/technologies/javase/jdk19-archive-downloads.html

https://www.eclipse.org/, Java EE version seems to work best for me these days

First open Eclipse after install, accept the default workspace or choose another location (this is where the example project will be created)

Go to File -> New -> Project

Select Java Project

Type TestProject in the Project Name, then hit Finish button

To the left there will be a TestProject under the Package Explorer view

Double clicking (Windows) on the TestProject folder will open and show the directory where there are two entries.

  • JRE System Library [JavaSE-19]
  • src

Double click on src folder if it is not already expanded. There should be a module-info.java, this is where we define required pieces of Java we need for compiling.

  1. Right click on src and select first option New -> Package
  2. Type in myTestPackage in the Name field.
  3. Click finish

Under the src folder there should now be a new package created (symbolized by a different icon) call myTestPackage

Right click on the package name and choose first option New -> Class

In the Name field type TestMain

Below there is an option under “Which method stubs would you like to create?”

Check the option box that is next to “public static void main(String[] args)”.

Now click Finish

Should have the following code showing

package myTestPackage;

public class TestMain {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

Java is an Object oriented language. An object1 is like a particular Red Car. Another object2 is like a particular Blue Car. Both objects can be of the same Class Car. Thus if the class Car has a color property. object1.color!=object2.color

One of the built in objects in Java is System.out, it is a PrintStream that is attached to the system out output. Basically an object that is of type Class PrintStream, and unlike a car that might have a move function and is painted a different color, PrintStream has a println function that allows developers to print messages to output.

object1.move(50); // might be how you move a red car 50 miles

object2.move(50); // might be how you move a blue car 50 miles

System.out.println(“Never Say Die!”); // is how you get the built in Java PrintStream object to output to the console or command prompt (if you are running it outside of eclipse).

We can add this line to our code to print to the console, we also remove the comment that was added TODO, “//” allows those two characters and everything after them on the same line to be viewed as a comment. Developers used // TODO [design thoughts] to remind them to fill out functionality. Many times it is useful to generate methods and classes but the developer does not always have time to finish them in the same sitting. Thus the developer can search the codebase later to update the code with functionality.

package myTestPackage;

public class TestMain {

	public static void main(String[] args) {
		System.out.println("Never Say Die!");
	}

}

Code should now look like this, a small asterisk will be beside the TestMain file name at the top indicating the file has changed and has not been saved. Hit Ctrl+S to save the file. Getting familiar with hot keys can vastly speed up development time.

Right click on TestMain.java in PackageExplorer on the left part of the screen. About 3/4s the way down there is an option Debug as. Select that and select Java Application.

That should print in the console (lower part of the screen) the following

Never Say Die!

If you Right click on the Console Window there is an option to clear, which will reset the window. This is useful for running again. Clear the console and then look for the green bug shaped icon on the second to top bar there is a little down arrow next to it and if you hover your mouse over the down arrow it will show the text “Debug TestMain”.

Clicking on the down arrow next to the Debug Icon will give a list, likely at the top with be “TestMain”, click that

Again that should print

Never Say Die!

Computers can be a lot like printing presses. Why are printing presses useful? They can reproduce the same message many more times than once.

Computers given a set of instructions can repeat that set of instructions. Programming in Java and other languages utilizes this feature of computers via a system called loops.

Loops are kind of like a Ferris wheel, you go up to the top add some numbers at the tops then go back to the bottom and then do it all over again. Computers can go through loops many times very swiftly.

A simple loop in Java relies on a primitive type integer that is defined by the keyword int. Keywords are important words to the compiler that helps it know what it is looking at. Another keyword is double. Double allows storing numbers with decimal places and integers do not. Integers are whole numbers.

We define primitive variables by first giving the keyword with their type and then following it by a specific variable name we get to choose then followed by optional assignment and a semi colon to instruct and running of the current line.

double ratio=0.5; // this is a valid assignment of a double to 1/2 or 0.5

int wholeNumber=3; // this is a valid assignment of an integer to 3

Now these variables can be changed much like x or y in Algebra.

The “+=” operator as a value to the current value

ratio+=0.25; // This will make the ratio 0.75

wholeNumber+=4; // This will make wholeNumber to 7

If we are talking about whole pies like American Apple Pies that are baked. We can use an integer. If we want to share 0.25 of our Apple Pie then we can use a double to represent the 0.75 Apple Pie left over.

There is a concept in Java and other languages call a For loop. For loops specify a range from numbers say 0 to 10 or 0 to 1000 and specify how swiftly to move over that range. A short hand way of saying +=1 is ++. Thus if make a variable

int wholeNumber=3;

wholeNumber++; // Will set the value to 4

With for loops many times the increment value is 1 and the range is many times 0 to 1 less than the size. So if I was writing a loop to run ten times I would right the following

for (int i=0; i<10; i++) {
      System.out.println("Never Say Die! iteration "+i);
}

Adding this after our original println statement we get

package myTestPackage;

public class TestMain {

	public static void main(String[] args) {
		System.out.println("Never Say Die!");
		for (int i=0; i<10; i++) {
		      System.out.println("Never Say Die! iteration "+i);
		}
	}

}

If we then click the little arrow next to the Debug Icon, and click on TestMain again it should give the following in the console:

Never Say Die!
Never Say Die! iteration 0
Never Say Die! iteration 1
Never Say Die! iteration 2
Never Say Die! iteration 3
Never Say Die! iteration 4
Never Say Die! iteration 5
Never Say Die! iteration 6
Never Say Die! iteration 7
Never Say Die! iteration 8
Never Say Die! iteration 9

One can note the initial iteration of the loop shows as 0 and the last iteration shows as 9. Many times this is useful in accessing Java Arrays and Arrays in other languages as many times an array that is 10 elements is accessed by

int [] array = new int[10];

array[0]++; // Increments element

array[9]++; // Increments the last element

As we are not using an array we might want to display the value in a more user friendly manner. User Experience matters and many times it is upsold to less non ideally.

Thus since we know the user might like the list with numbers 1 through 10 more we can change our code to show 1 through 10 instead.

package myTestPackage;

public class TestMain {

	public static void main(String[] args) {
		System.out.println("Never Say Die!");
		int displayValue;
		for (int i=0; i<10; i++) {
			  displayValue=i+1;
		      System.out.println(displayValue+" Never Say Die!");
		}
	}

}

Rerunning using the small arrow next to Debug Icon and clicking TestMain we get the following output to console

Never Say Die!
1 Never Say Die!
2 Never Say Die!
3 Never Say Die!
4 Never Say Die!
5 Never Say Die!
6 Never Say Die!
7 Never Say Die!
8 Never Say Die!
9 Never Say Die!
10 Never Say Die!

There are many great Java Lessons on the Web that go into a More In-depth review of how to start programming. I hope this tutorial has been helpful.

Published by techinfodebug

Flex and Java Developer, Christian, Art, Music, Video, and Vlogging

Leave a comment