This Core Java Tutorial Will Introduce You to Basic Java Programming

bubblesortjavaWith a reported nine million developers using Java for the development of client-server web applications as well as standalone WORA applications. One of the reasons that Java is so popular is because of its simplicity as well as the inherent power of the language as a whole.

Core Java – or basic Java is often used to refer to basic and core functions of Java whilst advanced Java refers to J2SE, J2EE & J2ME java but there is essentially no difference between the concepts other than the fact that J2SE, J2EE & J2ME have more functions available to them.

This tutorial is designed to introduce the core Java concepts to you and to allow you to understand the core concepts of java programming so that you can begin to use the language to create your own awesome applications.

The core attributes of java

The goals of the programmers who wrote java included:

  • That Java should be a simple, object orientated language

  • That Java should be secure and robust platform

  • That Java should be portable and it should run on any machine

  • That Java should a high performance programming language

  • And finally that it should be a dynamic threaded language.

And having written a language that met all of these goals and requirements, Java is a simple language to learn that is secure and allows you to build robust stand alone applications that run on any machine which includes the java runtime environment.

Writing your first core Java application

To write your first java application you need to install the program onto your computer. Creating the right environment is not part of this tutorial but if you need help and information with regards to where to download java or how to install the program then Java for Absolute Beginners will show you how to get started.

Hello World Again.

Most tutorials are based on the now almost infamous “Hello World” program. For this tutorial we will go beyond “Hello World” to show you how the core principles of java function. So let’s take a look at a small java application that we will write for the purposes of this tutorial.

This code will be used to examine and explain the elements which make up a core java application or program.

Basic Java Syntax

First there are a few things you should notice about core java syntax. Java syntax is case sensitive. Class names must begin with a capital letter like HelloAgain in our example above. All method names should start with a lowercase letter. The file name must match the class name with the file extension .java thereafter, so our above file should be saved as HelloAgain.java. And all identifiers in core java must begin with a letter, underscore or $.

How to add comments to you code

Adding comments to your core java application is essential when you are writing a complicated application. Comments allow you to indicate what each part of the code is for and what it is supposed to do. All good programmers add comments to their code to either remind themselves of the function of that particular part of the code or to allow other programmers to follow that particular set of code.

Core Java supports multi line comment code. To add a single line comment, Java uses the // identifier like this:

To begin a multiline set of comments java uses the /* for the first line followed by the * for the subsequent lines and to end the multi line comment the */ syntax is used.

Now that we know how to refer to the various parts of a java application. Let’s talk about the variables we need to make the program work. In our example code we need a variable to count from four to ten to print each the statement. So we need to know how to define that variable in proper core java syntax.

Core Java Concepts – Working with Variables.

Variables are what give programs their basic power. By assigning values that can change based on user input or program instructions, programmers can write and execute complex code.

All programming languages have a certain set of recognized variables and core Java is no different in that regard. The variables available to us in core java include: variables assigned locally, variables that class variables, otherwise known as static variables and instance variables which are also known as non-static variables.

The syntax to define a variable is as follows:

You can assign various variables within one statement:

You can also assign values to the variables within the statement:

For more advanced variable declarations and statements, you can learn Java from scratch and be able to declare all the variables you may need for you own programs with this udemy course.

In our example we need to define a variable to count from four to ten so we therefore need an integer. We also don’t need this variable to be available for the whole program but just for that specific function so we declare the variable within the function so we are declaring a local variable. This is how we declare the variable for our HelloAgain application:

Core java also has a way of restricting the values of a variable by using the enum function which then declares that a variable has to equal one of the defined values determined by the programmer. For more about enum and other more advanced core Java programming functions, Java Fundamentals I and II, an online Java course containing Core Java Tutorials will teach you how to define and work with the various types of variables.

Now that we know a little about the basic syntax of java as well as what variables are used in core Java we need to understand a little about how the programming language actually functions.

Java as an object orientated language

If you take a look at the world around you, you will notice that we as humans define our world through the use of objects. Objects have properties which we all agree on and also functions which we all know about.

Let’s take a look at nature for example. We all know the object called tree. We also all agree that trees, as objects in our world possess certain characteristics. In this case trees have a root system, they have branches and they all have leaves – at some point. These are the characteristics of the trees. We also all know that trees provide the earth with oxygen, they grow, they provide shade. These are the functions of trees.

Object orientated programs work in the same manner. The program recognizes certain objects. When we use those objects, the programming language knows that that object has certain characteristics which it can access, as well as certain functions it can assign to that object.

Now that we can declare variable and we also basically know how object orientated programs work we need to take a look at how we manipulate variables within the various objects of the program.

Operators available in core Java.

There are essentially seven operators within core Java which allow us to modify values of variables. They are addition, subtraction, multiplication, division, modulus, increment and decrement. Take a look at the following statements:

For addition, a single plus sign is used. Addition causes the program to add the values left and right of the operator. So a + b would give you 30. For subtraction a single minus sign is used and the program subtracts the left hand value from the right so a-b would give you -10. For multiplication a * is used so a * b would give you a value of 200. For division a division sign is used so a/b would give you an answer of 0.5. The modulus function returns the remainder of a division operation. To return the modulus a percentage sign is used. In this case a % b would return a value of zero since there is no remainder. To increment the value of a variable, a double plus sign is used. So a++ would result in a value of 11. And finally to decrement the value of a variable a double minus sign is used so b—would results in a value of 19.

For our code example, since we want our code to count from four to ten and to print a statement each time it executes the code, we need to add one to our variable I by one for each statement performed. We could therefore express it as follows:

Or we could express it as:

In our code example we have used the i = i + 1 syntax.

There are also relational operators in core java which allow us to compare values rather than to manipulate them. The relational operators in core java include equal to, which is expressed as ==, not equal to expressed as !=, greater than expressed as >, less than expressed as <, greater than or equal to expressed as >= and less than or equal to expressed as <=.

In our code example, we need to keep comparing the value if i to check whether the statement has been performed ten times or not. We therefore need to use the less than or equal to operator to make sure the code stops when i is equal to ten.

We therefore express that as:

Java also includes bitwise operators and logical operators, but explanation of these operators exceed the scope of this tutorial. To learn more about the various operators available in core java and to learn how to apply them, a course like Programming Java for Beginners – The Ultimate Java Tutorial will teach you about all the variables you may need for your application.

Now that we understand how to assign and increment variables and how the language essentially works, we also need to be able to repeat statements within our HelloAgain application, to be able to print the statement ten times. This is achieved by the programming function known as loops.

Programming repetitive tasks using loops.

Core Java offers a number of loop functions and options for writing repetitive statements. The loops offered in core java include the while loop, the for loop and the do while loop. For our HelloAgain application we will need to use the while loop. The while loop allows the repetition of code while a condition is false. As soon as the condition becomes true the program exits the loop.

In our case we want our application to keep checking the value if i and while the value of I is less than or equal to ten we want our program to output of string. So we add a while function to our program as follows:

And then we add the statements we want our application to execute with brackets after the statement.

You now have a basic understanding of all the basic concepts that are needed for our core java HelloAgain application and how it works. Let’s look at the application again to recap the concepts covered in this tutorial:

First we have created an application called HelloAgain and created the main class for the program according to object orientated programming principles. We have added comments to our program using comment syntax to show what each section of code does. We have defined a local variable called i to keep track of how many times a statement has been performed. We have created a loop to repeat a section of code six times using i as our condition for the loop.

The output of our application would look like this:

I hope you have enjoyed this introduction to core java tutorial. Java is truly a platform independent powerful object oriented programming language which allows you to create applications which are both robust and secure as well as being fun to learn.