Tuesday, August 23, 2016

JAVA 4 - Oh I do declare ... variables.


Today we explore the world of Java programming a little more by looking at variables.

When I was a kid, reading my first book of computers, they explained that a computer was "a machine for processing data.


Variables are handles for data which was used in software.  There are multiple core types - although we're able to specify many more complex ones if we want to - although typically they're combinations of multiple of these data types.

Before we use a variable, we typically do something called declaration.  This is where we define what name we're using for the variable, what type of data item it is, and we might even perform "initialisation" which is setting it to an initial value.

Lets look at some types now ...

Integer

The integer type is declared by the int keyword as below,

int line_count = 0;

We used this last time...

  • int - declares it as an integer
  • line_count - is the name of the variable.  Whenever we use line_count, it'll substitute the value of the variable instead.
  • = 0 - this is the initialisation part.  It sets the value of line_count to zero.


Integers are whole numbers.  You never use fractions or decimals in integers.

Integers are great for counting the number of times you've been through it.  Everyone but parents counts in whole numbers.

If you had a mother like mine, then you might remember the fractal way they count - "you have until the count of three Michael!  One ... two ... two and a half ... two and three-quarters ...".

Double

The float data variable is a "double accuracy" floating point number, or decimal number.  An example is below,

double pi_val = 3.142;

Similar to our integer,

  • double - declares it as a double decimal number.
  • pi_val - is the name.
  • = 3.142 - initialises the value.


Strings

Strings are ways the computer languages handle text.  A string is considered to be a grouping of characters.  We've already been handling strings - "Hello, World" was a string in our first program.  We use the combination of "" to show we're handling data for a string.

So, let's declare a string here,

String person_name = "Mike Talks";

Here,

  • String - declares it as a string
  • person_name is the name of the variable.
  • = "Mike Talks" = initialises the string to contain my name.
Boolean

A Boolean is a very simple data type used to determine if something is true or false.

boolean IsThePopeCatholic = true;

Example

Find the coded example here.


This method declares several data types, and prints them out as below.




Extension material

Sometimes you need other forms of data variable.  Look up and read about

  • long
  • float
  • char

3 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  2. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete

  3. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more .
    java training in chennai | Best java training in chennai | java training | java training in chennai with placement

    ReplyDelete