First we will look at little bit more information and rules for Java and then we will get started with our first program. All of the information in this and following tutorials come from the book Java Programming: Guided Learning with Early Objects (978-1-4239-0162-4) and from what I learned in my Java class. I make absolutely no claims to be a professional and I should be taken with a grain of salt. That being said, let's get started.
First, let's learn about print statements. In java there are two types of print statements print and println and they are referenced this way: System.out.print("Hello World"); or System.out.println("Hello World");. The difference between print and println is that println will have a character return after the line is printed and print will not. Remember, java is case sensitive so be sure to capitalize those S's.
Next, we will talk about escape sequences. An escape sequence is used to print a character that has a secondary meaning within the Java language. A few examples include:
\n will print a new line
\t will print a tab
\\ will print a backslash
\" will print a double quote
\' will print a single qoute.
Next, we will talk about comments. Comments can be very useful to keep your program organized and there are three different types.
Line comments are noted by a // at the beginning of the line.
Block comments are noted by a /* at the beginning of the block and */ at the end of the block.
Javadoc comments are noted by a /** at the beginning of the block and a */ at the end of the block. We will talk about Javadoc comments later.
Next, we will discuss variables. Variables are a memory location, a place where a certain number, text or other item is stored. Naming conventions in java should be used when naming the variable. The variable should make sense and should be lower case, except when there are two or more words strung together. First, we must declare the variable by typing: datatype itemName;.
Datatype should be replaced with the datatype you will be using. There are numerous datatypes that you can choose from. The most popular are:
boolean (True or False)
float (Floating Point Numbers)
byte (Integer)
int (Integers)
char (Characters)
long (Long Integers)
double (Floating Point Numbers)
short (Integers)
After declaring your variable, you will need to initialize your variable. You do this by typing this: datatype itemName = value;, Remember replace datatype with the type that you need and replace value with what your are assigning it to.
We will be creating two simple Java programs today that will display output on the screen. We will also be using escape sequences.
For our first program we will be creating a face using text characters as shown here:
///// |o o| (| ^ |) |\_/ | ____Start by opening JEdit. We will start by using comments to enter our name and the reason for our program at the top of the program.
//Name
//Reason
Next, we will start by defining our class. Every program will start with a class and the class name should be capitalized and match the file name.
public class Tutorial1 {
The curly brace at the end of the statement means that we will be adding information inside of this class.
Next we will define our main method. Our definition will look like this in nearly every tutorial program that we write.
public static void main(String[] args) {
Now, we will type our program to create our output.
System.out.println(" ///// ");
System.out.println(" | o o | ");
System.out.println("(| ^ |)");
System.out.println(" |\\_/|");
System.out.println(" _____ ");
Now all we have to do is close our curly braces. Since we have two open braces we will need two close braces. Now we will save and take a look at our program. We will call our program Tutorial1.java and save it wherever you want but remember where you saved it because we will need this later.
Notice how JEdit changes the colors for certain words. This can help you when you have an error.
Now we will compile and run the program to see what it looks like. Open the command prompt to get started. First you will need to get into the file where you saved your program. Use can do this by typing cd then the filename, you will name to change files one level at a time until you get where your file is. Then you will type javac Tutorial1.java, if there are no errors it will look like nothing happened and the prompt will pop up again. If you have errors, go back and double check your spelling, capitalization, ;'s and the file and class names.
Now you are ready to run the program, we do this by typing java Tutorial1 and hitting enter. Your program should now run! Note: you may need to adjust your formatting to get the face to line up properly.
Congratulations! You have completed your first java program! We will do one more little program for this tutorial before we finish up. This tutorial is very similar to the last one but this time we will be printing a table that looks similar to the following:
Shape Number of Sides
=============================
Diamond 4
Rhombus 4
Square 4
Triangle 3
This program is very similar to the other program, add some comments, define your class, your main method and use println statements along with escape sequences to get everything entered. Now open the command prompt and run the program.
Congratulations! You have now written two Java programs. Have some fun with print line statements and escape sequences! Next time we will look at data types and the math class. In the meantime, be sure to leave me a comment with how you used your new java programming skills! You can view the completed programs and what they look like here.
Published by Brandee Teer
I am currently working as a Web Develop and pursuing Bachelor's in Web Development. I also operate my own freelance web design business. I am working on fixing my past mistakes and becoming a person my child... View profile
- Your First Java ProgramContains the code, and analysis to create your first Java program.
Programming Series - CPP Part 2 - Hello World ProgramIt's time to write your first C++ program. This is the program that almost every programmer starts with. It is known as the Hello World program. Although it is one of the mos...- Java Programming - a Brief IntroductionA brief introduction to Java programming and intro into a series of Java tutorials.
Hello World!Hello World! If I could change things, make you all get better, clean you up with a smog vacuum, filter out your water with robot dolphins, and grow soil-alchemizing beans every...- Visual Basic: Hello World Application, and Message Box OverviewAs this is the first fully-functional program you will complete, I will work you through from top to bottom, left to right. I will become progressively less detailed, as you will have already mastered the easy aspects...
- Java Programming Tutorials: The Online Give and Take
- A Short Introduction to Java Programming Language
- Beginner Java Programming: How to Read Java Characters
- Understanding Java
- How to Install Java JDK and Set the Environment Variables
- How to Declare Integer Variables in Java
- Types of Access Modifiers and Ways to Declare Methods in Java Programming for Begi...



