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

2 comments:


  1. 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

  2. Channels for online games and interesting bets.

    G club Online investment sites that everyone can easily play each day. Full with bets that will give you more returns. You can play it all. Complete with simple bets to make good money. Can play this way. Enjoy and experience casino games that you will like. Betting is fun. Bet on gambling with good luck. Make more money with just like or want to gamble to make good money to play it. To play anywhere, there are more gambling games.

    The player to use the site. The player will enjoy. There are bets that will give you more returns. Do not have to be complicated or want to play the gambling luck where? With a simple gambling. Make more money. Get the good stuff. Gamblers are allowed to enjoy the gambling game is the number one. Full of great gambling. To play very easily. No more hassle, time, fun, ease of investment, you will like to make more money. Ready to play with fun, bet your own money every day, always good. Gclub มือถือ

    ReplyDelete