Page 1 of 1

Basic Java: Types, Variables, Operators Rate Topic: ***** 1 Votes

#1 cronus8992   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 29-October 09

Posted 10 November 2013 - 08:54 AM

About Me

I simply enjoy learning new things and believe knowledge is power. I recently picked up on Java about a month ago. I like giving back to people that help me out, so I figured why not write a tutorial for people while I am still a noob at the language. Maybe my prospective of it at a noob level will help people starting off in Java. So here is my first tutorial of many provided to this community.


In this tutorial we will start with the basics of Java, to hopefully help guide you through the stepping stones of doing useful things with Java.

Tutorial Index:
  • Java and the Computer

  • First Program and its Structures

  • Common Variable Types

  • Assignment of Variables

  • Basic Operators

  • String Concatenation(+)

  • Your First Problem

Java and the Computer

Posted Image

In this picture you will get a basic understanding of how information is processed. On the left we have all the inputs to the computer, which stores it into memory to be processed by the Central Processing Unit(CPU), the information is then put out to some kind of output which you see a list of on the left.

The CPU reads instructions differently then we do. When we add numbers together we thing x + y = z, but this is how the CPU will process that information:

//z = x + y                  //Read location x
                             //Read location y
                             //Write to location Z

This is important to remember because it will be the building blocks to how you will be assigning things in your programs.
Programming languages are easier to understand than the language the CPU reads. This is why we create the languages, but in the end all the languages we use has to be translated so the CPU can understand them.So why Java you ask?

  • "Most Popular" language
  • Runs on a "Virtual Machine" (JVM)
  • More Complex then some (eg. Python)
  • Simpler than others (eg. C++)

Posted Image

The image above shows how Java is compiled. You first write your Java code to a text editor, and it gets saved as a .java source code file.This source code is then compiled by a Java compiler, and saved as byte-codes also known as a .class file. It is then pasted through the JVM to the operating system you are currently running.

The way Java does this is what makes it popular. You do not have to write a different code for every operating system on the market. One code will work for any operating system since it is ran through the JVM.

Now that we have the boring parts down let us leap into the first program almost everyone writes and its structures!

First Program and its Structures

Hello World!...This has become the universal program for every programmer to start at. Almost every tutorial you find has you start with this program...and so shall I but with a little twist. :clap:/>

class Hello{
  public static void main(String[] args){
     //Program executes here
      System.out.println("Hello D.I.C's!");

Copy this and run it, you should get an output on your console like:
Hello D.I.C's!

No offense to anyone here :sarcasm:/>

Let's look deeper into the structure of this program:

  public static void main(String[] args){

Simple enough right? So the CLASSNAME in our program was Hello, and the STATEMENTS we chose to do was print a message to the system.

So lets look at outputs and get some more sense on what we just accomplished.

System.out.println(some string); outputs to the console



Make sense? Ok then let's move onto Variables.

Common Variable Types

A variable is how we store data we want saved into the memory to be processed.


boolean: True value. (True and Fasle)

int: Integer. (0, 1, -47)

double: Real Numbers. (3.14, 1.0, -2.0)

String: Text. ("hello", "Example")

These are the basic variable types you will be using throughout most of your programs. If you are interested in learning the other types simply Google for them and you will find a list of all types available to you in Java.

Assignment of Variables

So now that you know the variable types, let me show you how you assign them a value to be used inside your program. When you assign a value to a variable, it will call the variable and pull the value you stored in the memory for that variable.

A variable is a named location that stores a value of ONE particular type.


String foo;  //String is the type foo is the name you created.

Use = to give variable a value.


String foo;
foo = "IAP 6.092"; //So you assign type String to foo, the assign it the string value IAP 6.092

You can also combine a variable with the declaration(the foo = "IAP 6.092 above").


double badPI = 3.14;
boolean isJanuary = true;

So in this program as an example:

class Hello{
  public static void main(String[] arg){
      String foo = "IAP 6.092";
      foo = "Something Different";

So here you would get:

IAP 6.092
Something Different

As your output.


Ok so we have done a lot of reading so we will keep this one simple. Operators are symbols that help perform arithmetic of variables. Here are the operators you can use:

Assignment: =
Addition: +
Subtraction: -
Multiplication: *
Division: /

Order of Operations is simple if you have taken any basic math classes(which I assume you have if you are getting into this stuff).

Follows standard math rules:
  • Parenthesis
  • Multiplication and Division
  • Addition and Subtraction

Make sure to follow these rules or you will get a logical error in your calculations. This means you will not receive an error when run, but you will not get the output you are looking for. These errors are the hardest to find.

This is an example of operators in java:

class doMath{
  public static void main(String[] arg){
     double score = 1.0 + 2.0 * 3.0;
     score = score/2.0;

The output of this would be:


String Concatenation(+)

This involves adding string together. It is fairly simple here are some examples

String text = "Hello " + "D.I.C's!"; //Hello D.I.C's!
text = text + "Community" + 6; //Hello D.I.C's! Community 6

Simple right? Ok so I think you know enough now to work out your first assignment.


Gravity Calculator: Compute the position of a falling object x(t) = 0.5 * a(t2) + Vi(t) + xi

a Acceleration(m/s2) value = -9.81
t Time (s) value = 10
vi initial velocity (m/s) value = 0
xi initial position value = 0

Modify the given code to compute the postion of an object falling for 10 seconds , outputting the position in meters.

class GravityCalculator {
    public static void main(String[] arguments) {
        double gravity = -9.81;  // Earth's gravity in m/s^2
        double initialVelocity = 0.0;
        double fallingTime = 10.0;
        double initialPosition = 0.0;
        double finalPosition = 0.0;
        System.out.println("The object's position after " + fallingTime +
                " seconds is " + finalPosition + " m.");

Hint: the correct value is -490.5 m. Java may output more digits after the decimal depending on how you are compiling it, but that is not important right now.

Answer is provided below. Good luck and I hope this tutorial has helped you.


This post has been edited by cronus8992: 13 November 2013 - 03:49 PM

Is This A Good Question/Topic? 2
  • +

Replies To: Basic Java: Types, Variables, Operators

#2 leblazko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-February 14

Posted 03 March 2014 - 10:31 PM

Nice job, i'll be alert for updates :)
Was This Post Helpful? 0
  • +
  • -

#3 Earniemax   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-March 14

Posted 20 March 2014 - 01:06 PM

View Postleblazko, on 03 March 2014 - 10:31 PM, said:

Nice job, i'll be alert for updates :)/>

Hello leblazko, thanks so much!!….this really clicked with me!! I loved your approach; your format was simple and easy to follow and the functionality of the material presented was very interactive and captivating. Great Job, PLEASE post more… this..
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon

Reputation: 12680
  • View blog
  • Posts: 45,861
  • Joined: 27-December 08

Posted 20 March 2014 - 01:20 PM

The author of this tutorial is cronus8992, not leblazko. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1